简体   繁体   English

在VB.NET中从SQL Server执行存储过程?

[英]Executing a Stored Procedure from SQL Server in VB.NET?

How would I execute a stored procedure in SQL server through VB.NET? 如何通过VB.NET在SQL Server中执行存储过程?

for a normal SQL command I would normally use: 对于通常的SQL命令,我通常会使用:

Command = New SQLCommand("Select * From Table Where Column ='Value' ",Connection)
Command.ExecuteNonQuery '( Or scalar depending ). 

Is it the same but you select the stored procedure? 一样吗,但是您选择了存储过程?

Dim Command As New SQLCommand("usp_MyStoredProc", connection)
Command.CommandType = CommandType.StoredProcedure
Command.ExecuteNonQuery()

At the top of your .vb file: 在.vb文件的顶部:

Imports System.data.sqlclient

Within your code: 在您的代码中:

'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

Now that the basics are out of the way, 既然基础已不复存在,

I highly recommend abstracting the actual SqlCommand Execution out into a function. 我强烈建议将实际的SqlCommand执行抽象为一个函数。

Here is a generic function that I use, in some form, on various projects: 这是我在各种项目中以某种形式使用的通用函数:

    ''' <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

One you have that, your SQL Execution + reading code is very simple: 您就有一个,您的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)))
 '----------------------------------------------------------------------'

SOURCE 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM