计数表中的记录... [英] Counting records in a table...

查看:45
本文介绍了计数表中的记录...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算一个表中的记录数.我试图这样-
SqlDataAdapter da =新的SqlDataAdapter();
字符串选择="从tblRegNo;中选择count(*)作为数字;
da.SelectCommand =新的SqlCommand(select,con);

然后我需要将此数字分配给一个整数变量,然后可以使用消息框查看数据库表中的记录数.
请帮我.我是C#编程的初学者.

I need to count the number of records in a table.I tried like this -
SqlDataAdapter da = new SqlDataAdapter();
string select = "select count(*) as number from tblRegNo;
da.SelectCommand = new SqlCommand(select, con);

Then I need to assign this number to a integer variable.Then I can use a messagebox to view the number of records in the database table.
Please help me. I am a beginner for C# programming.

推荐答案

查询计数后使用:

SqlCommand cmd =新的SqlCommand(select,con);
int Newvalue = Convert.ToInt32(cmd.ExecuteScalar());
After Query Of Count Use:

SqlCommand cmd = new SqlCommand(select, con);
int Newvalue = Convert.ToInt32(cmd.ExecuteScalar());




试试下面的代码

Hi,

Try the below code

string query = "select count(*) as number from tblRegNo";
SqlCommand cmd= new SqlCommand(query,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
int noOfRecords=0;
If(ds.Table[0].Rows.Count>0)
{
   noOfRecords = Convert.ToInt32(ds.Table[0].Rows[0]["number"].ToString());
}


string select = "select* from tblRegNo";
SqlDataAdapter da = new SqlDataAdapter(select, con);
DataTable dt = new DataTable();
da.fill(dt);

var count= dt.Rows.Count.ToString();
MessageBox.Show(count);


这篇关于计数表中的记录...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆