如何使用 VB.NET 从数据库中统计记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34209007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:38:16  来源:igfitidea点击:

How to count records from database using VB.NET ?

sqlasp.netvb.netwebforms

提问by Emi

I have the code below:

我有下面的代码:

 Dim ds As New DataSet
 Dim sda As System.Data.SqlClient.SqlDataAdapter
 Dim sSQL As String
 Dim strCon As String

 sSQL = " .... MY QUERY HERE .... "

 strCon = appBase.dbConnString

 sda = New System.Data.SqlClient.SqlDataAdapter(sSQL, strCon)
 sda.Fill(ds, "MY TABLE FROM DB")


 dgRecordsContent.DataSource = ds.Tables("MY TABLE FROM DB")
 dgRecordsContent.DataBind()

 dgRecordsContent.Visible = True

 dbConn.Close()

How can I programatically count the number of rows from the datagrid that I'm showing the values in?

如何以编程方式计算数据网格中显示值的行数?

采纳答案by PHeiberg

Provided that the DataTablebeing filled doesn't already contain any rows, you can get the count using:

假设DataTable被填充的内容不包含任何行,您可以使用以下方法获取计数:

int count = sda.Fill(ds, "MY TABLE FROM DB")

Otherwise you can access the rows in the DataTableusing:

否则,您可以访问使用中的行DataTable

int count = ds.Tables("MY TABLE FROM DB").Rows.Count

回答by PHeiberg

 Dim ds As New DataSet
 Dim sda As System.Data.SqlClient.SqlDataAdapter
 Dim sSQL As String
 Dim strCon As String

 sSQL = " .... MY QUERY HERE .... "

 strCon = appBase.dbConnString

 sda = New System.Data.SqlClient.SqlDataAdapter(sSQL, strCon)
 Dim num_rows=sda.Fill(ds, "MY TABLE FROM DB")

 MessageBox.Show(num_rows.ToString())  

 dgRecordsContent.DataSource = ds.Tables("MY TABLE FROM DB")
 dgRecordsContent.DataBind()

 dgRecordsContent.Visible = True

 dbConn.Close()  

Please Check This Edit @Emi

请检查此编辑@Emi

回答by PHeiberg

you can use ds.Tables[0].Rows.Count

您可以使用 ds.Tables[0].Rows.Count