0

我需要帮助ReceiptNO从数据库表中检索列并将其保存到TextBoxorLabel以供参考。

代码:

Dim da2 As New SqlDataAdapter
da2.SelectCommand = New SqlCommand("SELECT RecepitNO FROM Receipt WHERE (PaidFor=@PaidFor AND RegNO=@RegNO)")
da2.SelectCommand.Parameters.Add("@paidFor", SqlDbType.VarChar).Value = cbMonth.Text
da2.SelectCommand.Parameters.Add("@RegNO", SqlDbType.Int).Value = lblRegNO.Text

cn.Open()
da2.Update(ds.Tables("Receipt"))
'da2.SelectCommand.ExecuteNonQuery()
da2.SelectCommand.ExecuteReader()
cn.Close()
4

1 回答 1

0

您需要使用SqlDataReader,然后启动循环以读取返回的值
此示例将在 ReceiptNO 是文本字段的情况下工作

cn.Open()
Dim reader = da2.SelectCommand.ExecuteReader()
while reader.Read()
    textBox1.Text = reader("ReceiptNO").ToString()
End While

或者,如果您确定查询返回零或仅返回一条记录,并且您只对 ReceiptNO 字段感兴趣,那么您可以使用 ExecuteScalar

Dim cmd = New SqlCommand("SELECT RecepitNO FROM Receipt WHERE (PaidFor=@PaidFor AND RegNO=@RegNO)")
cmd.Connection = cn
cmd.Parameters.Add("@paidFor", SqlDbType.VarChar).Value = cbMonth.Text
cmd.Parameters.Add("@RegNO", SqlDbType.Int).Value = lblRegNO.Text
cn.Open()
Dim result = cmd.ExecuteScalar()
if result IsNot Nothing Then
    textBox1.Text = result.ToString()
End If

这里是 ExecuteScalar 上的 MSDN 文档

于 2013-04-18T07:41:49.980 回答