您的位置:首页 > 产品设计 > UI/UE

SqlCommandBuilder.DeriveParameters设置储存过程参数

2007-12-19 10:59 591 查看
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对象供大家调用,扩展大家可以自己随意了!水平有限,就写到这里!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: