Module MdlCommon
Public txtSQL As String '存放SQL語句
Public DBSet As DataSet '查詢得到的記錄集
Public ErrorMsg As String '存放錯誤信息
Public Function ExecuteSQL(ByVal strSQL As String, ByRef errMsg As String) As DataSet
Dim cnn As SqlClIEnt.SqlConnection
Dim cmd As New SqlClIEnt.SqlCommand()
Dim adpt As SqlClIEnt.SqlDataAdapter
Dim rst As New DataSet()
Dim SplitSQL() As String
errMsg = ""
Try
SplitSQL = Split(strSQL)
cnn = New SqlClIEnt.SqlConnection("data source=(local);initial catalog=urp;user id=sa;pwd=1234")
If InStr("INSERT,DELETE,UPDATE", UCase$(SplitSQL(0))) Then
cmd.Connection = cnn
cmd.Connection.Open()
cmd.CommandText = strSQL
cmd.ExecuteNonQuery()
Else
adpt = New SqlClIEnt.SqlDataAdapter(strSQL, cnn)
adpt.Fill(rst)
ExecuteSQL = rst
End If
Catch ex As Exception
errMsg = ex.Message
Finally
rst = Nothing
cnn = Nothing
End Try
End Function End Module
調用時在ASP.Net後台寫上txtSQL="SELECT ....FROM........";
DBSet=ExecuteSQL(txtSQL,ErrorMsg)