C# 统计sql表中的记录并将其显示在文本框中
我知道这很简单,但我无法思考。显示表中记录的计数并将其显示在文本框中C# 统计sql表中的记录并将其显示在文本框中,c#,sql,winforms,C#,Sql,Winforms,我知道这很简单,但我无法思考。显示表中记录的计数并将其显示在文本框中 private void gMapControl1_Load_1(object sender, EventArgs e) { SqlConnection conDatabase = new SqlConnection(constring); conDatabase.Open(); DataTable db = new DataTable(); SqlDataAdapter sda = new S
private void gMapControl1_Load_1(object sender, EventArgs e)
{
SqlConnection conDatabase = new SqlConnection(constring);
conDatabase.Open();
DataTable db = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(
"select count(site) from [ICPS].[dbo].[excel GPS postcode]",
conDatabase);
sda.Fill(db);
textBox29.Text = ;
SqlDataAdapter sda = new SqlDataAdapter(
"select count(site) from [ICPS].[dbo].[excel GPS postcode] "
+ "where Region = '1'",
conDatabase);
sda.Fill(db);
textBox30.Text = ;
}
您只需要使用
SqlCommand.ExecuteScalar
而不是像这样使用SqlDataAdapter
:
SqlCommand com = new SqlCommand("select count(site) from [ICPS].[dbo].[excel GPS postcode]", conDatabase);
object count = com.ExecuteScalar();
if(count != null) textBox29.Text = count.ToString();
//The same for textBox30
更多关于
请注意,我发布的代码只是为了使用
ExecuteScalar
,这取决于代码风格
当使用ADO.NET
时,您可能需要使用一些using
语句,或者重用您的命令,…
以您喜欢的方式执行。当您希望sql命令只返回一个值时,请使用命令对象中的
string cmdText1 = "select count(site) from [ICPS].[dbo].[excel GPS postcode]";
using(SqlConnection conDatabase = new SqlConnection(constring))
using(SqlCommand cmd = new SqlCommand(cmdText, conDatabase))
{
conDatabase.Open();
int numRec = Convert.ToInt32(cmd.ExecuteScalar());
textBox29.Text = numRec.ToString();
}
MSDN说
执行查询,并返回查询中第一行的第一列
查询返回的结果集。添加了其他列或行
忽略
但是我注意到您试图从两个不同的查询中读取记录计数。
因此,您的代码也可以这样编写,以避免往返数据库
string cmdText = "select count(site) from [ICPS].[dbo].[excel GPS postcode];" +
"select count(site) from [ICPS].[dbo].[excel GPS postcode] " +
"where Region = '1'", ;
using(SqlConnection conDatabase = new SqlConnection(constring))
using(SqlCommand cmd = new SqlCommand(cmdText, conDatabase))
{
conDatabase.Open();
using(SqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
int numRec1 = Convert.ToInt32(reader[0]);
reader.NextResult();
reader.Read();
int numRec2 = Convert.ToInt32(reader[0]);
textBox29.Text = numRec1.ToString();
textBox30.Text = numRec2.ToString();
}
}
通过这种方式,我利用了SQL Server执行两个或多个用分号分隔的命令的能力 要从数据表中获取值,请使用行[columnName]
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
cmd.CommandText = "select count(site) from [ICPS].[dbo].[excel GPS postcode]";
Int32 count = (Int32) cmd.ExecuteScalar();
textBox29.Text = count.ToString();
}
SqlDataAdapter sda = new SqlDataAdapter(
"select count(site) As siteCount from [ICPS].[dbo].[excel GPS postcode]",
conDatabase);
sda.Fill(db);
if(db !=null && db.Rows.Count > 0)
textBox29.Text =db["siteCount"].tostring();
else
textBox29.Text ="0";
然而,正如King在这里建议的那样,如果您必须只获取一行和一列,那么为什么要使用datatable呢?请使用ExecuteScalar
方法
执行查询,并返回查询返回的结果集中第一行的第一列。其他列或行将被忽略-
谢谢大家。。很抱歉反应太晚。。在您的帮助下,问题已经解决。
SqlCommand com = new SqlCommand("select count(site) from [ICPS].[dbo].[excel GPS postcode]", conDatabase);
object siteCount = com.ExecuteScalar();
if(siteCount != null) textBox29.Text = siteCount.ToString();