使用ExecuteScalar执行聚合函数查询返回值总是0的问题

 1     public int CheckUserName(string username) 
2 {
3 DBClass db = new DBClass();
4 int a;
5 using (SqlConnection conn = db.GetConn())
6 {
7 SqlCommand cmd = new SqlCommand("select count(*) from tb_Member111 where id = '" + username + "'", conn);
8 a = Convert.ToInt32(cmd.ExecuteScalar());
9 conn.Close(); //关闭后再返回,否则返回值永远是0
10 return a;
11 }
12 }


把SqlConnection关闭再返回才是关键!!

你可能感兴趣的:(execute)