Sql 将查询值赋给变量
帮助,我使用SQL Server作为我的数据库,我的后端是VB.NET 我要分配此查询的值:Sql 将查询值赋给变量,sql,vb.net,variables,assign,Sql,Vb.net,Variables,Assign,帮助,我使用SQL Server作为我的数据库,我的后端是VB.NET 我要分配此查询的值: SELECT sum(productPrice) from cartTbl ,然后将该值赋给名为totalPrice的文本框 如何执行此操作?提前谢谢你 如果使用 然后可以调用此方法来获取价格 Dim prodPrice = GetProductPrice() 如果使用,则应使用ExecuteScalar() 然后可以调用此方法来获取价格 Dim prodPrice = GetProductPric
SELECT sum(productPrice) from cartTbl
,然后将该值赋给名为totalPrice
的文本框
如何执行此操作?提前谢谢你 如果使用 然后可以调用此方法来获取价格
Dim prodPrice = GetProductPrice()
如果使用,则应使用
ExecuteScalar()
然后可以调用此方法来获取价格
Dim prodPrice = GetProductPrice()
你可以用
SELECT @var1=sum(productPrice) from cartTbl
你可以用
SELECT @var1=sum(productPrice) from cartTbl
为计算列使用别名
SELECT sum(productPrice) as prod_sum
from cartTbl
然后你可以这样读
While dr.Read()
totalPrice.Text = dr("prod_sum")
End While
为计算列使用别名
SELECT sum(productPrice) as prod_sum
from cartTbl
然后你可以这样读
While dr.Read()
totalPrice.Text = dr("prod_sum")
End While
它就这么简单,但请阅读一些基本信息
它就这么简单,但请阅读一些基本信息
要扩展已经说过的内容,您可以使用以下方法使其更加灵活:
Private Sub Test()
'Get/set connection string
Me.TextBox1.Text = Me.SQLExecuteScalar(ConnectionString, "SELECT sum(productPrice) FROM cartTbl")
End Sub
Public Shared Function SQLExecuteScalar(ByVal ConnectionString As String, ByVal Query As String) As String
Dim Result As String = Nothing
Dim Exc As Exception = Nothing
Using Conn As New SqlClient.SqlConnection(ConnectionString)
Try
'Open the connection
Conn.Open()
'Create the SQLCommand
Using Cmd As New SqlClient.SqlCommand(Query, Conn)
'Create an Object to receive the result
Dim Obj As Object = Cmd.ExecuteScalar
If (Obj IsNot Nothing) AndAlso (Obj IsNot DBNull.Value) Then
'If Obj is not NULL
Result = Obj.ToString
End If
End Using
Catch ex As Exception
'Save error so we can (if needed) close the connection
Exc = ex
Finally
'Check if connection is closed
If Not Conn.State = ConnectionState.Closed Then
Conn.Close()
End If
End Try
End Using
'Check if any errors where found
If Exc IsNot Nothing Then
Throw Exc
End If
Return Result
End Function
要扩展已经说过的内容,您可以使用以下方法使其更加灵活:
Private Sub Test()
'Get/set connection string
Me.TextBox1.Text = Me.SQLExecuteScalar(ConnectionString, "SELECT sum(productPrice) FROM cartTbl")
End Sub
Public Shared Function SQLExecuteScalar(ByVal ConnectionString As String, ByVal Query As String) As String
Dim Result As String = Nothing
Dim Exc As Exception = Nothing
Using Conn As New SqlClient.SqlConnection(ConnectionString)
Try
'Open the connection
Conn.Open()
'Create the SQLCommand
Using Cmd As New SqlClient.SqlCommand(Query, Conn)
'Create an Object to receive the result
Dim Obj As Object = Cmd.ExecuteScalar
If (Obj IsNot Nothing) AndAlso (Obj IsNot DBNull.Value) Then
'If Obj is not NULL
Result = Obj.ToString
End If
End Using
Catch ex As Exception
'Save error so we can (if needed) close the connection
Exc = ex
Finally
'Check if connection is closed
If Not Conn.State = ConnectionState.Closed Then
Conn.Close()
End If
End Try
End Using
'Check if any errors where found
If Exc IsNot Nothing Then
Throw Exc
End If
Return Result
End Function
@洛伦佐萨拉曼特,没问题。看看这个关于接受答案的链接@LorenzoSalamante,没问题。看看这个链接,了解接受答案是如何工作的