6
A
回答
9
從MSDN
要執行的存儲過程返回行編程方式使用命令對象
Dim sqlConnection1 As New SqlConnection("Your Connection String")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "StoredProcedureName"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = sqlConnection1
sqlConnection1.Open()
reader = cmd.ExecuteReader()
' Data is accessible through the DataReader object here.
' Use Read method (true/false) to see if reader has records and advance to next record
' You can use a While loop for multiple records (While reader.Read() ... End While)
If reader.Read() Then
someVar = reader(0)
someVar2 = reader(1)
someVar3 = reader("NamedField")
End If
sqlConnection1.Close()
36
在您的.vb文件的頂部:
Imports System.data.sqlclient
在你的代碼:
'Setup SQL Command
Dim CMD as new sqlCommand("StoredProcedureName")
CMD.parameters("@Parameter1", sqlDBType.Int).value = Param_1_value
Dim connection As New SqlConnection(connectionString)
CMD.Connection = connection
CMD.CommandType = CommandType.StoredProcedure
Dim adapter As New SqlDataAdapter(CMD)
adapter.SelectCommand.CommandTimeout = 300
'Fill the dataset
Dim DS as DataSet
adapter.Fill(ds)
connection.Close()
'Now, read through your data:
For Each DR as DataRow in DS.Tables(0).rows
Msgbox("The value in Column ""ColumnName1"": " & cstr(DR("ColumnName1")))
next
現在基本都閃開,
我強烈建議將實際的SqlCommand執行抽象爲一個函數。
這裏是我使用的,以某種形式,對各個項目的通用功能:
''' <summary>Executes a SqlCommand on the Main DB Connection. Usage: Dim ds As DataSet = ExecuteCMD(CMD)</summary>'''
''' <param name="CMD">The command type will be determined based upon whether or not the commandText has a space in it. If it has a space, it is a Text command ("select ... from .."),'''
''' otherwise if there is just one token, it's a stored procedure command</param>''''
Function ExecuteCMD(ByRef CMD As SqlCommand) As DataSet
Dim connectionString As String = ConfigurationManager.ConnectionStrings("main").ConnectionString
Dim ds As New DataSet()
Try
Dim connection As New SqlConnection(connectionString)
CMD.Connection = connection
'Assume that it's a stored procedure command type if there is no space in the command text. Example: "sp_Select_Customer" vs. "select * from Customers"
If CMD.CommandText.Contains(" ") Then
CMD.CommandType = CommandType.Text
Else
CMD.CommandType = CommandType.StoredProcedure
End If
Dim adapter As New SqlDataAdapter(CMD)
adapter.SelectCommand.CommandTimeout = 300
'fill the dataset
adapter.Fill(ds)
connection.Close()
Catch ex As Exception
' The connection failed. Display an error message.
Throw New Exception("Database Error: " & ex.Message)
End Try
Return ds
End Function
一旦你的,你的SQL執行+閱讀代碼非常簡單:
'----------------------------------------------------------------------'
Dim CMD As New SqlCommand("GetProductName")
CMD.Parameters.Add("@productID", SqlDbType.Int).Value = ProductID
Dim DR As DataRow = ExecuteCMD(CMD).Tables(0).Rows(0)
MsgBox("Product Name: " & cstr(DR(0)))
'----------------------------------------------------------------------'
+4
+1用於推薦到抽象函數。 – mg1075 2013-02-27 01:39:48
2
最簡單的方法?有用。 :)
Dim queryString As String = "Stor_Proc_Name " & data1 & "," & data2
Try
Using connection As New SqlConnection(ConnStrg)
connection.Open()
Dim command As New SqlCommand(queryString, connection)
Dim reader As SqlDataReader = command.ExecuteReader()
Dim DTResults As New DataTable
DTResults.Load(reader)
MsgBox(DTResults.Rows(0)(0).ToString)
End Using
Catch ex As Exception
MessageBox.Show("Error while executing .. " & ex.Message, "")
Finally
End Try
0
我的存儲過程需要2個參數,我需要我的函數返回一個DataTable這裏就是100%的工作代碼
請確保您的程序返回一些行
Public Shared Function Get_BillDetails(AccountNumber As String) As DataTable
Try
Connection.Connect()
debug.print("Look up account number " & AccountNumber)
Dim DP As New SqlDataAdapter("EXEC SP_GET_ACCOUNT_PAYABLES_GROUP '" & AccountNumber & "' , '" & 08/28/2013 &"'", connection.Con)
Dim DST As New DataSet
DP.Fill(DST)
Return DST.Tables(0)
Catch ex As Exception
Return Nothing
End Try
End Function
0
Dim sqlConnection1 As New SqlConnection("Your Connection String")
Dim cmd As New SqlCommand
cmd.CommandText = "StoredProcedureName"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = sqlConnection1
sqlConnection1.Open()
Dim adapter As System.Data.SqlClient.SqlDataAdapter
Dim dsdetailwk As New DataSet
Try
adapter = New System.Data.SqlClient.SqlDataAdapter
adapter.SelectCommand = cmd
adapter.Fill(dsdetailwk, "delivery")
Catch Err As System.Exception
End Try
sqlConnection1.Close()
datagridview1.DataSource = dsdetailwk.Tables(0)
相關問題
- 1. 從存儲過程執行SQL Server代理作業,並返回作業結果
- 2. C# - 批處理執行存儲過程
- 3. 執行多個Windows cmd命令並存儲處理結果
- 4. 執行SQL存儲過程
- 5. 如何對SQL Server CTE的結果執行存儲過程
- 6. 使用存儲過程在SQL Server中執行結果
- 7. 如何讓sql server存儲過程結果在其他存儲過程中進行處理?
- 8. 如何從存儲過程執行存儲過程時禁用查詢結果?
- 9. 將存儲過程的結果附加到較早執行的存儲過程
- 10. 執行存儲過程並行方式
- 11. 從SQL Server存儲過程執行Oracle存儲過程
- 12. SQL存儲過程和錯誤處理
- 13. LINQ to SQL:存儲過程結果
- 14. 存儲過程中的SQL結果集
- 15. SQL彙總存儲過程的結果
- 16. MySQL存儲過程動態sql結果
- 17. sql存儲過程清除結果集
- 18. 返回存儲過程的結果sql
- 19. 從存儲過程合併結果集
- 20. 使用SQL代理在SQL Server 2012中執行存儲過程
- 21. SQL存儲過程執行命令
- 22. 無法執行SQL Server存儲過程
- 23. SQL:存儲過程執行錯誤
- 24. C#&SQL Server:執行存儲過程
- 25. T-SQL動態執行存儲過程
- 26. PHP和SQL Server存儲過程執行
- 27. 在sql profiler中執行存儲過程
- 28. 在SQL Server中執行存儲過程
- 29. SQL Server複製 - 存儲過程執行
- 30. SQL Server 2005存儲過程執行
如果您使用的是2008年,可以返回一個表格。 – 2011-05-13 12:36:09
@Doug - OP不需要2008年他所要求的。存儲過程的結果可以被客戶端應用程序捕獲爲一個數據集*(我沒有足夠好的.net背景,但在Delphi中可以)*。 – 2011-05-13 12:43:59
@Lieven,你是對的。 OP不需要* 2008來完成他們所要求的。 – 2011-05-13 12:46:44