附录一代码
代码1DB SQLServer操作类
-----------------------------------------------------------------------------------------------------------------------
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Imports System.Collections
Imports System
Imports System.Configuration
Namespace ConnSqlServer
'通用数据库类
Public Class db
Private connStr As String
Public str As String
'默认构造函数
Public Sub New()
connStr = "Data Source=(local);Initial Catalog=DB;Integrated Security=True"
End Sub
'带参数的构造函数
Public Sub New(ByVal Str As String)
Try
Me.connStr = Str
Catch ex As Exception
Throw ex
End Try
End Sub
'Return SqlConnencion
Function ReturnConn() As SqlConnection
Dim Conn As SqlConnection
Conn = New SqlConnection(connStr)
Conn.Open()
Return Conn
End Function
'Dispose SqlConnection
Sub Dispose(ByVal Conn As SqlConnection)
If (Conn IsNot DBNull.Value) Then
Conn.Close()
Conn.Dispose()
End If
GC.Collect()
End Sub
'Create SqlCommand 带两个参数
Public Function CreateCmd(ByVal SQL As String, ByVal conn As SqlConnection) As SqlCommand
Dim cmd As SqlCommand
cmd = New SqlCommand(SQL, conn)
Return cmd
End Function
'Create SqlCommand 带一个参数
Public Function CreateCmd(ByVal SQL As String) As SqlCommand
Dim Conn As SqlConnection
Conn = New SqlConnection(connStr)
Conn.Open()
Dim cmd As SqlCommand
cmd = New SqlCommand(SQL, Conn)
Return cmd
End Function
''' <summary>
''' 执行没有返回值的SQL语句
''' </summary>
''' <param name="SQL"></param>
''' <remarks></remarks>
Public Function RunSql(ByVal SQL As String) As String
Dim Message As String = ""
Dim Conn As SqlConnection
Conn = New SqlConnection(connStr)
Conn.Open()
Dim cmd As SqlCommand
cmd = CreateCmd(SQL, Conn)
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
Throw New Exception(SQL)
Message += ex.Message
End Try
Dispose(Conn)
Return Message
End Function
''' <summary>
''' 执行SQL返回DataReader
''' </summary>
''' <param name="SQL"></param>
''' <returns>SqlDataReader</returns>
''' <remarks></remarks>
Public Function DataReader(ByVal SQL As String) As SqlDataReader
Dim Conn As SqlConnection
Conn = New SqlConnection(connStr)
Conn.Open()
Dim cmd As SqlCommand
cmd = CreateCmd(SQL, Conn)
Dim dr As SqlDataReader
Try
dr = cmd.ExecuteReader(CommandBehavior.Default)
Catch ex As Exception
Throw New Exception(SQL)
Finally
'Conn.Close()
'Conn = Nothing
'cmd = Nothing
'dr = Nothing
End Try
Return dr
End Function
'执行SQL返回Adapter
Public Function Adapter(ByVal SQL As String) As SqlDataAdapter
Dim Conn As SqlConnection
Conn = New SqlConnection(connStr)
Conn.Open()
Dim adp As SqlDataAdapter
adp = New SqlDataAdapter(SQL, Conn)
Return adp
End Function
''' <summary>
''' 执行SQL返回DataSet
''' </summary>
''' <param name="SQL"></param>
''' <param name="ds"></param>
''' <returns>DataSet</returns>
''' <remarks></remarks>
Public Function ReturnDataSet(ByVal SQL As String, ByRef ds As DataSet) As DataSet
Dim Conn As SqlConnection
Conn = New SqlConnection(connStr)
Conn.Open()
Dim adp As SqlDataAdapter
adp = New SqlDataAdapter(SQL, Conn)
Try
adp.Fill(ds)
Catch ex As Exception
<< 上一页 [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] ... 下一页 >>