SqlCommandBuilder.DeriveParameters設置儲存過程參數
之前我一直都用手寫的方法,手動添加存儲過程參數,實在的麻煩無與,這兩天突然在書上看到這個方法,而且在網上查了,對性能也沒有太大影響,所以就開始動手使用!網上多是使用C#的實例,但我又是vb.net的忠實fans,所以就准備把他改在vb.Net,沒想在這個過程還碰到了很多問題,經過無數次的調試終於搞定,寫出來供大家享用,從此的我們的數據層就更加強壯了呀!
數據層: mydata.vb

Option Strict Off

Option Explicit On

Imports system.data

Imports system.data.sqlclIEnt

Imports Microsoft.VisualBasic

Imports System.XML

Imports System.IO



Public Class mydataClass mydata

Private connectionstring As String

Dim cn As SqlConnection


Public Sub New()Sub New()

MyBase.New()

End Sub


Public Sub open()Sub open()

connectionstring = System.Configuration.ConfigurationManager.APPSettings("connectionstring").ToString.Trim

cn = New SqlConnection(connectionstring)

If cn.State = ConnectionState.Open Then

cn.Close()

End If

cn.Open()

End Sub


Public Sub closed()Sub closed()

cn.Close()

End Sub


'''''' <summary>

'''''' 根據存儲過程與參數值返回sqlcommand對象

'''''' </summary>

'''''' <param name="spname">存儲過程名</param>

'''''' <param name="spvalue">參數數組</param>

'''''' <returns></returns>

'''''' <remarks></remarks>


Public Function sqlcom()Function sqlcom(ByVal spname As String, ByVal spvalue As Object()) As SqlCommand

Dim com As SqlCommand = New SqlCommand(spname, cn)

com.CommandType = CommandType.StoredProcedure

Me.open()

SqlCommandBuilder.DeriveParameters(com) ''將存儲過程參數同給com對象,通過com.Parameters返回

Me.closed()

Dim spc As SqlParameter() = New SqlParameter(com.Parameters.Count - 1) {}

''返回數組,記住減1因為後面會產生一個為nothing的參數

com.Parameters.CopyTo(spc, 0) ''復制數組,從0開始

com.Parameters.Clear() ''復制完以後一定要清空,不然不能添加新參數

For j As Integer = 0 To spc.Length - 1

spc(j).Value = spvalue(j).ToString ''賦值參數,記住會產生一個@return_value的多余參數為apc(0)

com.Parameters.Add(spc(j))

Next

com.Dispose()

Return com

End Function

'''''' <summary>

'''''' 根據存儲過程返回表

'''''' </summary>

'''''' <param name="spname">存儲過程</param>

'''''' <param name="spvalue">值數組</param>

'''''' <returns></returns>

'''''' <remarks></remarks>


Public Function getTable()Function getTable(ByVal spname As String, ByVal spvalue As Object()) As DataTable

Me.open()

Dim com As SqlCommand = Me.sqlcom(spname, spvalue)

Dim sdt As SqlDataAdapter = New SqlDataAdapter(com)

Dim ds As DataSet = New DataSet

sdt.Fill(ds)

Dim dt As DataTable = ds.Tables(0)

Me.closed()

Return dt

End Function

End Class

業務層:

Imports Microsoft.VisualBasic

Imports data

Imports System.Data


Namespace perNamespace per


Public Class perClass per

Inherits data

Dim _spname As String


Public Property searchid()Property searchid() As String

Get

Return VIEwState("searchid")

End Get

Set(ByVal value As String)

VIEwState("searchid") = value

End Set

End Property


Public Property spname()Property spname() As String

Get

Return _spname

End Get

Set(ByVal value As String)

_spname = value

End Set

End Property


Public Function get_table()Function get_table() As DataTable

Dim spvalue As Object() = New Object() {0, searchid} ''此時賦值一定要寫一個整數0,後面是我們的參數

Dim dt As DataTable = MyBase.getTable(spname, spvalue)

Return dt

End Function


End Class

End Namespace
表示層:

Imports System

Imports System.Data

Imports System.Web

Imports System.Web.UI.WebControls

Imports AJax

Imports data

Imports System.IO

Imports per


Partial Class perindexClass perindex

Inherits System.Web.UI.Page

Dim searchid As String


Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then

bind_gridvIEw()

End If

End Sub


Public Sub bind_gridview()Sub bind_gridvIEw()

If searchid Is Nothing Or searchid = String.Empty Then

searchid = 0

End If

Dim myper As per.per = New per.per

myper.spname = "bind_per"

myper.searchid = searchid

Dim dt As DataTable = myper.get_table()

pergridview.DataSource = dt.DefaultVIEw

pergridvIEw.DataBind()

End Sub

End Class
這裡面我主要是產生一個sqlcommand對象供大家調用,擴展大家可以自己隨意了!水平有限,就寫到這裡!!