如题,我的sql语句可写成"select count(*) as RecNum from mytable where age = 30"
或"select count(*) from mytable where age = 30",现在不知道该如何把结果赋值给变量int n;
请教!!!!!!!!!!1我已经试了好几个方法都不行,请指教!!!!!!!!!!
或"select count(*) from mytable where age = 30",现在不知道该如何把结果赋值给变量int n;
请教!!!!!!!!!!1我已经试了好几个方法都不行,请指教!!!!!!!!!!
代码贴出来看下
string sqlSelect = "select count(*) from mytable where age = 30";
SqlConnection cn = new SqlConnection(strcon);
cn.Open();
SqlCommand cm = new SqlCommand(sqlSelect1, cn);
SqlDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
int cnt = Convert.ToInt16(dr["cnt"]);
}
dr.Close();
cn.Close();
我用的是别的方法,一点点试出来的,不容易啊,跟大家分享一下:
string sqlSelect = "select count(*) from mytable where age = 30";
SqlConnection cn = new SqlConnection(strcon);
cn.Open();
SqlCommand cm = new SqlCommand(sqlSelect, cn);
object obj = cm.ExecuteOracleScalar();
int cnt = Convert.ToInt32(obj.ToString());
cn.Close();真是耗了好些功夫,谢谢大家关注!