日期:2014-05-19  浏览次数:20881 次

sql 参数返回问题 请帮忙 ~~
问题   如何将SQL语句中的查询结果直接返回到程序设计语言   ?

比如   select   count(*)   as   num   from   tablea;
sql执行结果为15
此时   C#中有变量   int   i  
如何将   15   直接赋值给i   ?
最好是直接赋值,   其他方法也可以  

谢谢

------解决方案--------------------
SqlConnection cn = new SqlConnection( "Server=.;uid=sa;pwd=sa;database=你的数据库 ")
SqlCommand cmd = new SqlCommand( "select count(*) as num from tablea ", cn);
cn.Open();
int i = Convert.ToInt32(cmd.ExecuteScalar().ToString());
cn.Close();