简体   繁体   English

选择准备语句行数

[英]Select prepared-statement row count

I may be late to the game here but I am forcing myself into prepared statments for reasons everybody is aware of by now for future development. 我可能在这里玩游戏迟到了,但出于每个人现在都知道未来发展的原因,我不得不强迫自己准备好陈述。 Thankfully it's been smooth sailing so far! 幸运的是,到目前为止航行顺利! However, i am having a hard time with the following. 但是,我在以下方面遇到了困难。 Below is my VB.NET code snippet: 以下是我的VB.NET代码段:

Dim strConnect As String = "server!"  
Dim cmd As New SqlCommand
Dim cn as SqlConnection = New SqlConnection(strConnect)

Dim str As String = ""
Dim row As SqlDataReader 
cmd = New SqlCommand("SELECT top 10 TypeOfDeliciousPie FROM FamilyLuncheon WHERE MomsSecretSauce  like '%' + @sstring + '%'", cn)
cmd.Parameters.Add("@sstring", SqlDbType.NVarChar).Value = sstring
cn.Open()
row = cmd.ExecuteReader()

While row.Read()
    str += """" + row(TypeOfDeliciousPie ) + ""","
End While
cn.close()

Now, the problem I am having is I need a count of total rows prior to looping though the dataset. 现在,我遇到的问题是,在遍历数据集之前,我需要统计总行数。 In short, I'm building a simple json string manually with the returned data and for the jquery to work properly, I need a total row count. 简而言之,我正在使用返回的数据手动构建一个简单的json字符串,为了使jQuery正常运行,我需要总行数。

How best should I go about getting a total row count from a SELECT prepared statement? 如何最好地从SELECT准备好的语句中获取总行数? Before I could simply load it into a datatable and simply table.Rows.count but I can't seem to find something similar. 在我可以简单地将其加载到数据表和table.Rows.count但我似乎找不到类似的东西。 My apologizes if this has previously been covered. 如果以前已解决此问题,我深表歉意。 I've searched here and google for a proper solution for the past couple hours. 在过去的几个小时内,我已经在这里和Google上搜索了适当的解决方案。

Your help is much appreciated! 非常感谢您的帮助!

You can still use a DataTable when using a SqlCommand for the query, either using a SqlDataAdapter , or using DataTable.Load , eg 使用SqlCommand进行查询时,仍然可以使用DataTable ,或者使用SqlDataAdapter ,或者使用DataTable.Load ,例如

Dim dt As DataTable = New DataTable()
dt.Load(cmd.ExecuteReader())

And then use dt.Rows.Count like you are used to. 然后像dt.Rows.Count一样使用dt.Rows.Count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM