C# sql查询数据库返回单个值方法

static string sqlServer= "server=.;database=DBsql;user id=sa;pwd=sa;";

#region 返回单个值
public static void refdange()
{
//实例化连接通道
SqlConnection conn = new SqlConnection(sqlServer);
//准备执行命令的字符串
string sqlStr = "select count(*) from work";
//准备执行命令
SqlCommand sqlcmd = new SqlCommand(sqlStr, conn);
//打开通道
conn.Open();
//执行命令并返回单个值
object objstr = sqlcmd.ExecuteScalar();
//关闭通道
conn.Close();
Console.WriteLine(objstr.ToString());
}
#endregion

posted @ 2021-09-25 18:01  zxh91989  阅读(994)  评论(0编辑  收藏  举报