static OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(@"~/App_Data/db1.mdb"));public static int sqlCount(string sqlString)
    {
            conn.Open();
            OleDbCommand com = new OleDbCommand(sqlString, conn);
            int count = Convert.ToInt32(com.ExecuteScalar());
          
            return count;
          
    }
这是我写在.cs中的类。sql语句: 
protected void Button1_Click(object sender, EventArgs e) 
    { 
        int i = DataAccess.sqlCount("select count(*) from WORDS"); 
        Response.Write(i.ToString()); 
    }

解决方案 »

  1.   

    用conn.Close()就可以。另外,好像用
    using ()
    {}
    也可以
      

  2.   

    count括号里最好不加*号
    最好写0或1,效率好点
      

  3.   

    public static int sqlCount(string sqlString) 
        { 
                conn.Open(); 
                OleDbCommand com = new OleDbCommand(sqlString, conn); 
                int count = Convert.ToInt32(com.ExecuteScalar()); 
              conn.close();
                return count; 
              
        } 
    这是我写在.cs中的类。sql语句: 
    protected void Button1_Click(object sender, EventArgs e) 
        { try
          {
            int i = DataAccess.sqlCount("select count(id) from WORDS"); 
    Response.Write(i.ToString()); 
         }
    catch (Exception ex)
            {
    Response.Write(ex.Message); 
            }
            finally
            {
                ConClose();
            } 
            
        }
    这样就可以了