gpt4 book ai didi

vb.net - 从 sql 查询中获取单个值到文本框中

转载 作者:行者123 更新时间:2023-12-04 02:10:11 24 4
gpt4 key购买 nike

我的 sql 查询的结果是一个单一的值,我想在标签中显示它,但不知道如何显示。我有一个创建数据集的解决方案,将查询结果放入该数据集并通过行 (0)、列 (0) 获取它,但我想有一种更聪明的方法。

Private myquery As String

Sub New(ByVal query As String)
myquery = query
End Sub
Public Sub query_Send()
Dim myconn As New SqlConnection("Data Source=DB;Integrated Security=TRUE")
Dim mycmd As SqlCommand = New SqlCommand(Me.myQuery, myconn)
Dim myTable As New DataTable
Dim previousConnectionState As ConnectionState = myconn.State
Try
If myconn.State = ConnectionState.Closed Then
myconn.Open()
Dim myDA As New SqlDataAdapter(mycmd)
myDA.Fill(myTable)
tb.text = **...**
End If
If previousConnectionState = ConnectionState.Closed Then
myconn.Close()
End If
End Try
End Sub

最佳答案

您可以使用 SqlCommand.ExecuteScalar Method

Dim querystr as String = "select value from MyTable where ID=5"
Dim mycmd As New SqlCommand(querystr, connection)
dim value as Object = mycmd.ExecuteScalar()

您可以放入文本框的。 ExecuteScalar 返回结果集第一行的第一个值(相当于 row(0).column(0) )当没有记录返回时 valuenothing.

关于vb.net - 从 sql 查询中获取单个值到文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18878656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com