【问题标题】:sql server + vb 6.0sql server + vb 6.0
【发布时间】:2011-07-25 19:44:42
【问题描述】:

我在 vb 6.0 中创建了一个数据库项目。我在 sql server 2000 中创建了我的数据库。我正在使用 adodc 和 odbc 连接到数据库。

我想将我的 sql server 保存在一个系统中,并将我的 vb 6.0 应用程序保存在另一个系统中,并且我想从网络访问 sql 数据库。

请帮助我。谢谢。

【问题讨论】:

  • 您面临的问题是什么?只要您为此编写代码,您提到的内容绝对可以实现。
  • 您的意思是 OLEDB 而不是 ODBC?也许看到connectionstrings.com/sql-server#p6
  • 在 VB6 和 SQL Server 2000 中编写新应用程序的奖励积分。
  • 我仍然在 VB6 中使用 SQL Server 2000 和/或 2005 编写新应用程序。企业环境仍然在这里使用它。 :)
  • 我仍然维护我们的旧 VB6 项目

标签: sql-server vb6 sql-server-2000


【解决方案1】:

这是编写了多少客户端应用程序:在桌面系统上运行的 VB6 程序,以及在另一个系统上运行的 SQL Server,通常在 Windows Server 操作系统上。

只要您在 VB6 程序中引用了 Microsoft ADO,并且正确设置了连接字符串,就不会有问题。

如果这没有为您指明正确的方向,请在您的问题中添加更多细节。

【讨论】:

    【解决方案2】:

    这是我用来通过 ODBC DSN 连接访问 MS SQL 数据库的类 cDatabase:

    Option Explicit
    
    Private m_eDBCursorType As ADODB.CursorTypeEnum  'Cursor (Dynamic, Forward Only, Keyset, Static)
    Private m_eDBLockType As ADODB.LockTypeEnum    'Locks (BatchOptimistic,Optimistic,Pessimistic, Read Only)
    Private m_eDBOptions As ADODB.CommandTypeEnum 'DB Options
    Private m_sDSNName As String
    Private m_sSQLUserID As String
    Private m_sSQLPassword As String
    Private cn As ADODB.Connection
    
    Private Sub Class_Initialize()
        m_eDBCursorType = adOpenForwardOnly
        m_eDBLockType = adLockReadOnly
        m_eDBOptions = adCmdText
    End Sub
    
    Private Function ConnectionString() As String
    
        ConnectionString = "DSN=" & m_sDSNName & "" & _
                                ";UID=" & m_sSQLUserID & _
                                ";PWD=" & m_sSQLPassword & ";"
    
    End Function
    
    Private Sub GetCN()
    On Error GoTo GetCN_Error
    
    If cn.State = 0 Then
    StartCN:
        Set cn = New ADODB.Connection
        cn.Open ConnectionString
    
        With cn
            .CommandTimeout = 0
            .CursorLocation = adUseClient
        End With
    End If
    
    
    On Error GoTo 0
    Exit Sub
    
    GetCN_Error:
    If Err.Number = 91 Then
        Resume StartCN
    Else
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetCN of Module modDatabaseConnections"
    End If
    
    End Sub
    
    Public Function GetRS(sSQL As String) As ADODB.Recordset
    
    Dim eRS As ADODB.Recordset
    
    On Error GoTo GetRS_Error
    
    TryAgain:
    
    If Len(Trim(sSQL)) > 0 Then
        Call GetCN
    
        Set eRS = New ADODB.Recordset       'Creates record set
        eRS.Open sSQL, cn, m_eDBCursorType, m_eDBLockType, m_eDBOptions
        Set GetRS = eRS
    
    Else
        MsgBox "You have to submit a SQL String"
    End If
    
    On Error GoTo 0
    Exit Function
    
    GetRS_Error:
    If Err.Number = 91 Then
        Call GetCN
        GoTo TryAgain
    ElseIf Err.Number = -2147217900 Then
        Exit Function
    Else
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetRS of Module" & vbCrLf & vbCrLf & "SQL - " & sSQL
    End If
    
    End Function
    
    Public Property Get DBOptions() As ADODB.CommandTypeEnum
    
        DBOptions = m_eDBOptions
    
    End Property
    
    Public Property Let DBOptions(ByVal eDBOptions As ADODB.CommandTypeEnum)
    
        m_eDBOptions = eDBOptions
    
    End Property
    
    Public Property Get DBCursorType() As ADODB.CursorTypeEnum
    
        DBCursorType = m_eDBCursorType
    
    End Property
    
    Public Property Let DBCursorType(ByVal eDBCursorType As ADODB.CursorTypeEnum)
    
        m_eDBCursorType = eDBCursorType
    
    End Property
    
    Public Property Get DBLockType() As ADODB.LockTypeEnum
    
        DBLockType = m_eDBLockType
    
    End Property
    
    Public Property Let DBLockType(ByVal eDBLockType As ADODB.LockTypeEnum)
    
        m_eDBLockType = eDBLockType
    
    End Property
    
    Public Property Get DSNName() As String
    
        DSNName = m_sDSNName
    
    End Property
    
    Public Property Let DSNName(ByVal sDSNName As String)
    
        m_sDSNName = sDSNName
    
    End Property
    
    Public Property Get SQLUserID() As String
    
        SQLUserID = m_sSQLUserID
    
    End Property
    
    Public Property Let SQLUserID(ByVal sSQLUserID As String)
    
        m_sSQLUserID = sSQLUserID
    
    End Property
    
    Public Property Get SQLPassword() As String
    
        SQLPassword = m_sSQLPassword
    
    End Property
    
    Public Property Let SQLPassword(ByVal sSQLPassword As String)
    
        m_sSQLPassword = sSQLPassword
    
    End Property
    

    这是我实例化和调用它的方式:

    Private Sub Command1_Click()
    Dim rs As ADODB.Recordset
    Dim DB As cDatabase
    Dim l As Long
    
    Set rs = New ADODB.Recordset
    Set DB = New cDatabase
    
        With DB
            .DBCursorType = adOpenForwardOnly
            .DBLockType = adLockReadOnly
            .DBOptions = adCmdText
            .DSNName = "Your_DSN_Name"
            .SQLUserID = "Your_SQL_Login_Name"
            .SQLPassword = "Your_SQL_Login_Password"
            Set rs = .GetRS("Select Field1 FROM Table1")
        End With
    
        If rs.RecordCount <= 0 Then Goto Exit_Sub
    
        For l = 1 To rs.RecordCount
            Debug.Print rs(0).Value
            rs.MoveNext
        Next l
    
    Exit_Sub:
    rs.Close
    Set rs = Nothing
    Set DB = Nothing
    End Sub
    

    【讨论】:

    • 还要确保添加对 Microsoft ActiveX 数据对象 2.X 库的引用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多