SQLDataReader 查找每一行的值

发布于 2024-10-02 04:38:07 字数 976 浏览 0 评论 0原文

我曾经使用数据集而不是 sqldatareaders 我曾经能够做这样的事情

If dataset.tables(0).Rows(0)(1).ToString()) = "N" Then
   lbl.Text = dataset.tables(0).Rows(0)(2).ToString()) 
Else
   'Do Nothing
End If

这显然不适用于 sqldatareaders。

我有代码来查看 SQLDatareader 是否有任何行,但想知道是否有办法获取每行的值

我猜这是可能的,我环顾四周但似乎找不到任何

Dim conn As SqlConnection = New SqlConnection("server='h'; user id='w'; password='w'; database='w'; pooling='false'")  

conn.Open()  

Dim query As New SqlCommand("DECLARE @investor varchar(10), @sql varchar(1000) Select @investor = 69836 select @sql = 'SELECT * FROM OPENQUERY(db,''SELECT * FROM table WHERE investor = ''''' + @investor + ''''''')' EXEC(@sql)", conn) 

Dim oDR As SqlDataReader = query.ExecuteReader()

If oDR.HasRows or dataset.tables(0).Rows(0)(1).ToString()) = "N" Then
   lbl.Text = dataset.tables(0).Rows(0)(2).ToString())
Else
   'Do Nothing
End If

东西我现在拥有的代码显然不起作用

有什么想法吗?

谢谢

I used to use datasets instead of sqldatareaders and I used to be able to do something like this

If dataset.tables(0).Rows(0)(1).ToString()) = "N" Then
   lbl.Text = dataset.tables(0).Rows(0)(2).ToString()) 
Else
   'Do Nothing
End If

This obviously doesn't work with sqldatareaders.

I have code to see if the SQLDatareader has any rows but was wondering if there was a way to get the value of each row

I'm guessing this is possible and i've had a look around but can't seem to find anything

Dim conn As SqlConnection = New SqlConnection("server='h'; user id='w'; password='w'; database='w'; pooling='false'")  

conn.Open()  

Dim query As New SqlCommand("DECLARE @investor varchar(10), @sql varchar(1000) Select @investor = 69836 select @sql = 'SELECT * FROM OPENQUERY(db,''SELECT * FROM table WHERE investor = ''''' + @investor + ''''''')' EXEC(@sql)", conn) 

Dim oDR As SqlDataReader = query.ExecuteReader()

If oDR.HasRows or dataset.tables(0).Rows(0)(1).ToString()) = "N" Then
   lbl.Text = dataset.tables(0).Rows(0)(2).ToString())
Else
   'Do Nothing
End If

That is the code I have at the moment which obviously doesn't work

Any ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路黄昏 2024-10-09 04:38:07

当您使用数据读取器时,您必须自己逐步浏览每一行。使用 HasRows 是一个好的开始,因为它会告诉您返回的结果集是否为空。

要迭代结果集,您应该使用 Read() 方法。如果您位于一行,它将返回 true;当您移过最后一行时,它将返回 false。

我的 Vb 很差,所以我会给你一个 C# 的例子:

if (oDR.HasRows && oDR.Read()) 
{
  if (oDR.GetString(0) == "N")
  {
    lbl.Text = oDr.GetString(1);
  }  
}

在这里,我首先检查我们是否有一个包含数据的结果集,然后尝试移动到第一行。如果成功,我将读取第一列的字符串值并将其与“N”进行比较。如果该值等于“N”,我将 lbl 变量的 Text 属性设置为第二列的字符串值。

这应该相当于您的数据集算法。我建议您阅读 MSDN SqlDataReader 文档。它非常好,示例代码很有用。

When you use the data reader you have to step through each row yourself. Using HasRows is a good start, for it will tell you if the returned result set is empty.

To iterate through the result set you should use the Read() method. It will return true if you are at a row and false when you have moved past the last row.

My Vb is poor so I will give you an example in C# instead:

if (oDR.HasRows && oDR.Read()) 
{
  if (oDR.GetString(0) == "N")
  {
    lbl.Text = oDr.GetString(1);
  }  
}

Here I first check that we have a result set with data and then try to move to the first row. If this succeeds I then read the string value of the first column and compare it to "N". If the value is equal to "N" I set the Text property of the lbl variable to the string value of the second column.

This should be equivalent to your algorithm with the dataset. I recommend that you read the MSDN documentation for the SqlDataReader. It is quite good and the example code is useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文