连接SQL SERVER的公共类 (转)

本文介绍了一个用于连接 SQL Server 数据库的公共类,包括创建 SqlConnection 对象的方法及解析组织机构 ID 的功能。
连接SQL SERVER的公共类 (转)[@more@]

'*****************************************************************************************************
'//开始日期:2002年5月27日
'//结束日期:2002年5月27日
'*****************************************************************************************************
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 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 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 & ";"

  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
  ''***********************************************************************


  ''定义类DBaseBindComm的ReadOnly属性State(创建连接的状态)
  ''True(创建成功),False(创建失败)
  Private StateVal As Boolean
  Public ReadOnly Property State() As Boolean
  Get
  Return StateVal
  End Get
  End Property

  End Class

  Public Class OrganComm

  '//Defined Structure  PublicOrganIDStructure
  '//Structure descriptoin
  '//本结构存取 企业组织机构Id
  '//顶层(DeptTopID)、一级(DeptMidId),二级ID(DeptLowId) 

  '//Begin define Structure PublicOrganIDStructure
  Public Structure PublicOrganIDStructure
  Public DeptTopID As String
  Public DeptMidId As String
  Public DeptLowId As String
  End Structure
  '//End define Structure PublicOrganIDStructure
  '//***************************************************************************************************  ''***********************************************************************
  ''//**定义连接SQL Server Database的通用函数**//
  '//本函数创建一个到指定数据库的连接对象 SqlConnection
  '//本函数有1个参数,IPublicApplication 为公共数据结构 PublicApplicationVal,

  '//Begin define Function IParseDeptOrgid
  Public Function IParseDeptOrgid(ByVal ideptOrgid As String) As PublicOrganIDStructure
  Dim OrganID As New PublicOrganIDStructure()
  Dim Orgid As String

  IDeptOrgid = Trim(IDeptOrgid)
  Orgid = microsoft.VisualBasic.Left(Trim(IDeptOrgid), 12)

  OrganID.DeptTopID = Microsoft.VisualBasic.Left(Orgid, 6)
  OrganID.DeptMidId = Microsoft.VisualBasic.Right(Orgid, 6)
  OrganID.DeptLowId = Microsoft.VisualBasic.Right(IDeptOrgid, 6)

  IParseDeptOrgid = OrganID
  End Function
  '//End define Function IParseDeptOrgid

  End Class


End Namespace
'***************************************************************************************************


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-991675/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-991675/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值