日期:2012-05-28  浏览次数:20642 次

 

Namespace Tonton.DAL
  '//----------------------------------------
  '//《数据链接层》 之 《数据连接与命令行》
  '//----------------------------------------
  '//作者:张少棠 (Tonton)
  '//时间:2005年8月29日
  '//邮编:tonton@yeah.net
  '//主页:http://www.tonton.cn
  '//博客:http://blog.tonton.cn
  '//----------------------------------------

  '//----------------------------------------
  '//例子:
  '//----------------------------------------
  'Dim Conn As New Tonton.DAL.Connection
  'Dim Cmd As Tonton.DAL.Command
  '
  '  Try
  '    '//打开ACCESS数据库,也可以用连接字符串并采用Open方法,
  '    '//Conn.Open("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("db.mdb"))
  '    '//当然,如果知道是用ACCESS,你会用上面这个麻烦的方法吗?
  '    '//如果是SQL SERVER的话,可以用 Conn.OpenSqlServer 方法。
  '    '
  '    Conn.OpenAccess(Server.MapPath("db.mdb"))

  '    '//添加记录
  '    Cmd = Conn.Execute("Insert Into [Item]([Value]) VALUES (?)")
  '    Cmd.AddParam("添加的内容")
  '    Cmd.Update()

  '    '//删除记录
  '    Cmd = Conn.Execute("Delete From [Item] WHERE Id=?")
  '    Cmd.AddParam(6)      '//要删除的记录号
  '    Cmd.Update()

  '    '//更新记录
  '    Cmd = Conn.Execute("Update [Item] Set [Value]=? WHERE Id=?")
  '    Cmd.AddParam("新的内容")
  '    Cmd.AddParam(5)
  '    Cmd.Update()

  '    '//读取记录
  '    Cmd = Conn.Execute("Select * From [Item]")
  '    If Cmd.Read Then
  '      Response.Write(Cmd("Value"))
  '    Else
  '      Response.Write("OK")
  '    End If

  '  Catch ex As Exception
  '    '//出错处理
  '    Response.Write(ex)
  '  Finally
  '    '关闭连接
  '    Conn.Close()
  '    Cmd = Nothing
  '  End Try
  '//----------------------------------------
  '//例子结束
  '//----------------------------------------


  '//----------------------------------------
  '//类定义开始
  '//----------------------------------------

  '数据连接类型枚举
  Public Enum ConnectionType As Integer
    OleDb = 1
    SqlServer = 2
    'Oracle = 3
    'MySql = 4
  End Enum

  '连接字符串构造器类
  Public Class ConnectStringBuilder
    Public Shared Function JetOleDb(ByVal DataBasePath As String, Optional ByVal PassWord As String = "") As String
      If DataBasePath <> "" Then
        JetOleDb = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataBasePath & ";"
        If PassWord <> "" Then
          JetOleDb &= "User ID='admin';Password=;Jet OLEDB:Database Password=" & PassWord
        End If
      End If
    End Function

    Public Shared Function SqlOleDb(Optional ByVal HostName As String = "localhost", Optional ByVal Catalog As String = "", Optional ByVal UserName As String = "sa", Optional ByVal PassWord As String = "") As String
      SqlOleDb = "Provider=SQLOLEDB.1;Persist Security Info=False;Data Source=" & HostName & ";Password=" & PassWord & ";User ID=" & UserName & ";"
      If Catalog <> "" Then SqlOleDb &= "Initial Catalog=" & Catalog & ";"
    End Function

    Public Shared Function SqlClient(Optional ByVal HostName As String = "localhost", Optional ByVal Catalog As String = "", Optional ByVal UserName As String = "sa", Optional ByVal PassWord As String = "") As String
      SqlClient = "Persist Security Info=False;Data Source=" & HostName & ";Password=" & PassWord & ";User ID=" & UserName & ";"
      If Catalog <> "" Then SqlClient &= "Initial Catalog=" & Catalog & ";"
    End Function

    Public Shared Function Dsn(ByVal DsnName As String) As String
      Return "DSN=" & DsnName
    End Function
  End Class

  '连接对象类
  Public Class Connection
    Private _dbConn As IDbConnection
    Private _ConnStr As String
    Private _dbType As ConnectionType = ConnectionType.OleDb

    Public Sub New(Optional ByVal ConnectType