使用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关闭再返回才是关键!!

posted on 2012-02-12 12:09  LitDev  阅读(1148)  评论(0编辑  收藏  举报