以下是我的全名 DataAccessLayer
Imports System.Data
Imports System.Data.SqlClient
Public Class DataAccessLayer
Public cmd As New SqlCommand
Dim conn As New SqlConnection
Dim da As New SqlDataAdapter
Dim dt As New DataTable
Dim param As New SqlParameter
Dim paramlist As New ArrayList
Public Sub connectToDB()
cmd = New SqlCommand
conn = New SqlConnection
conn.ConnectionString = "Data Source=10.10.30.104\CGHMSSQL;Initial Catalog=dbPractice;User ID=sa;Password=12345"
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
End Sub
Public Function getDataTable(ByVal storedProc As String) As DataTable
connectToDB()
da = New SqlDataAdapter
dt = New DataTable
cmd.CommandText = storedProc
dt.Clear()
setParameter(cmd)
da.SelectCommand = cmd
da.Fill(dt)
Return dt
End Function
Public Sub executeQuery(ByVal storedProc As String)
connectToDB()
cmd.CommandText = storedProc
setParameter(cmd)
cmd.ExecuteNonQuery()
End Sub
Public Sub setParameter(ByVal cmd As SqlCommand)
For Each param As SqlParameter In paramlist
cmd.Parameters.Add(param)
Next
End Sub
Public Sub addParameter(ByVal parameterName As String, ByVal dbType As DbType, ByVal value As Object)
param.ParameterName = parameterName
param.DbType = dbType
param.Direction = ParameterDirection.Input
param.Value = If((value Is Nothing), DBNull.Value, value)
paramlist.Add(param)
End Sub
Public Sub clearParameters()
paramlist.Clear()
cmd.Parameters.Clear()
End Sub
End Class