WEB开发网
开发学院数据库MSSQL Server 连接SQL SERVER公共类 阅读

连接SQL SERVER公共类

 2007-11-11 10:26:26 来源:WEB开发网   
核心提示:'* Option Explicit On'Option Strict On '//*** Imports System.Data.SqlClient '//*** '//Begin defined namespace Namespace Sunerp.CommClass.Com

  '*****************************************************************************************************

Option Explicit On 

'Option Strict On

'//***************************************************************************************************

Imports System.Data.SqlClient


'//***************************************************************************************************

'//Begin defined namespace

Namespace Sunerp.CommClass.Comm


  '//Defined Structure PublicApplicationVal

  '//Structure descriptoin

  '//本结构存取 数据库服务器名(Dbserver)、数据库用户ID(UserId)、

  '//数据库用户密码(UserPWD)、数据库名(Dbase),为联接数据库提供参数。

  '//为了满足某些简单查询的需要,提供了扩展的变量 常用表名(DBTable)、

  '//查询字段名(QueryFieldName)、查询字段值


  '//Begin define Structure PublicApplicationVal

  Public Structure PublicApplicationVal

    ' Public Dbserver, UserId UserPWD Dbase DBTable Account.

    Public Dbserver As String

    Public UserId As String

    Public UserPWD As String

    Public Dbase As String

    Public DBTable As String

    Public QueryFieldVale As String

    Public QueryFieldName As String

  End Structure

  '//End define Structure PublicApplicationVal

  '//***************************************************************************************************  '//***************************************************************************************************

  '//Begin defined Class

  Public Class DBaseBindComm

    ''***********************************************************************

    ''//**定义连接sql server(WINDOWS平台上强大的数据库平台) Database的通用函数**//

    '//本函数创建一个到指定数据库的连接对象 SqlConnection 

    '//本函数有1个参数,IPublicApplication 为公共数据结构 PublicApplicationVal,


    '//Begin define Function LinkedSqlServer

    Public Overloads Function LinkedSqlServer() As System.Data.SqlClient.SqlConnection


      Dim strconn As String

      Dim conn As New SqlConnection()

      strconn = "server=" & "erp" & _

           ";user id=" & "erpsa" & _

           ";password=" & "erpsa" & _

           ";database=" & "erpdata" & ";"


      Try

        conn = New SqlConnection(strconn)

        conn.Open()

        StateVal = True

        LinkedSqlServer = conn

      Catch ex As Exception

        ' MsgBox(ex.ToString)

        StateVal = False

        LinkedSqlServer = Nothing

        Exit Function

      End Try

    End Function



    ''End defined LinkedSqlServer

    ''***********************************************************************


    ''***********************************************************************

    ''//**定义连接sql server(WINDOWS平台上强大的数据库平台) Database的通用函数**//

    '//本函数创建一个到指定数据库的连接对象 SqlConnection 

    '//本函数有1个参数,IPublicApplication 为公共数据结构 PublicApplicationVal,


    '//Begin define Function LinkedSqlServer

    Public Overloads Function LinkedSqlServer( _

         ByVal IPublicApplication As PublicApplicationVal) _

         As System.Data.SqlClient.SqlConnection


      Dim strconn As String

      Dim conn As New SqlConnection()

      strconn = "server=" & IPublicApplication.Dbserver & _

           ";user id=" & IPublicApplication.UserId & _

           ";password=" & IPublicApplication.UserPWD & _

           ";database=" & IPublicApplication.Dbase & ";"

Tags:连接 SQL SERVER

编辑录入:coldstar [复制链接] [打 印]
赞助商链接