OptionStrictOff OptionExplicitOn Imports system.data Imports system.data.sqlclient Imports Microsoft.VisualBasic Imports System.Xml Imports System.IO PublicClass mydataClass mydata Private connectionstring AsString Dim cn As SqlConnection PublicSub New()SubNew() MyBase.New() End Sub PublicSub open()Sub open() connectionstring = System.Configuration.ConfigurationManager.AppSettings("connectionstring").ToString.Trim cn =New SqlConnection(connectionstring) If cn.State = ConnectionState.Open Then cn.Close() EndIf cn.Open() End Sub PublicSub closed()Sub closed() cn.Close() End Sub ''' <summary> ''' 根据存储过程与参数值返回sqlcommand对象 ''' </summary> ''' <param name="spname">存储过程名</param> ''' <param name="spvalue">参数数组</param> ''' <returns></returns> ''' <remarks></remarks> PublicFunction sqlcom()Function sqlcom(ByVal spname AsString, ByVal spvalue AsObject()) As SqlCommand Dim com As SqlCommand =New SqlCommand(spname, cn) com.CommandType = CommandType.StoredProcedure Me.open() SqlCommandBuilder.DeriveParameters(com) '将存储过程参数同给com对象,通过com.Parameters返回 Me.closed() DimspcAs SqlParameter() =New SqlParameter(com.Parameters.Count -1) {} '返回数组,记住减1因为后面会产生一个为nothing的参数 com.Parameters.CopyTo(spc, 0) '复制数组,从0开始 com.Parameters.Clear() '复制完以后一定要清空,不然不能添加新参数 For j AsInteger=0Tospc.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> PublicFunction getTable()Function getTable(ByVal spname AsString, ByVal spvalue AsObject()) 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 PublicClass perClass per Inherits data Dim _spname AsString PublicProperty searchid()Property searchid() AsString Get Return ViewState("searchid") EndGet Set(ByVal value AsString) ViewState("searchid") = value EndSet End Property PublicProperty spname()Property spname() AsString Get Return _spname EndGet Set(ByVal value AsString) _spname = value EndSet End Property PublicFunction get_table()Function get_table() As DataTable Dim spvalue AsObject() =NewObject() {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 PartialClass perindexClass perindex Inherits System.Web.UI.Page Dim searchid AsString ProtectedSub Page_Load()Sub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load IfNot IsPostBack Then bind_gridview() EndIf End Sub PublicSub bind_gridview()Sub bind_gridview() If searchid IsNothingOr searchid =String.Empty Then searchid =0 EndIf 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