程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> asp.net(vb.net)連接sql server 2000數據庫的連接模塊(module)

asp.net(vb.net)連接sql server 2000數據庫的連接模塊(module)

編輯:.NET實例教程

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)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved