C#中数据库查询出多少条记录,输出数量(int型的)
展开全部
方法一:使用while(read()())方法
SqlConnection conn = new SqlConnection(getConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from 表名", conn);
SqlDataReader sdrr = cmd.ExecuteReader();
while (sdrr.Read())
{
MessageBox.Show(string.Format("数据库里共有{0}条记录", sdrr[0]), "提示");
}
方法二:使用cmd.ExecuteScalar()方法
SqlConnection conn = new SqlConnection(getConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from 表名", conn);
MessageBox.Show("数据库里共有"+Convert.ToInt32(cmd.ExecuteScalar())+"条地址","提示");
SqlConnection conn = new SqlConnection(getConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from 表名", conn);
SqlDataReader sdrr = cmd.ExecuteReader();
while (sdrr.Read())
{
MessageBox.Show(string.Format("数据库里共有{0}条记录", sdrr[0]), "提示");
}
方法二:使用cmd.ExecuteScalar()方法
SqlConnection conn = new SqlConnection(getConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from 表名", conn);
MessageBox.Show("数据库里共有"+Convert.ToInt32(cmd.ExecuteScalar())+"条地址","提示");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询