VB6.0与DataControl数据库编程

VB6.0与DataControl数据库编程,vb6,Vb6,你能帮我访问数据库吗。。。我一直在读一些教程,但我不知道从哪里开始做这个。我使用DataControl访问数据库。首先,当单击“搜索员工”按钮时,程序将提示输入ID号,然后搜索更多信息并将其显示在文本框中。我知道如何设置文本框的属性,以便在我的输出中显示数据库的值,而无需单击搜索员工按钮,但我想先单击按钮搜索员工。我是VB6的初学者。请帮帮我!我现在需要这个项目。好的,我有一些时间可以利用,现在,首先添加对Microsoft ActiveX Data Objects 2.X库的引用。: 表格1代

你能帮我访问数据库吗。。。我一直在读一些教程,但我不知道从哪里开始做这个。我使用DataControl访问数据库。首先,当单击“搜索员工”按钮时,程序将提示输入ID号,然后搜索更多信息并将其显示在文本框中。我知道如何设置文本框的属性,以便在我的输出中显示数据库的值,而无需单击搜索员工按钮,但我想先单击按钮搜索员工。我是VB6的初学者。请帮帮我!我现在需要这个项目。

好的,我有一些时间可以利用,现在,首先添加对Microsoft ActiveX Data Objects 2.X库的引用。

表格1代码:

Option Explicit
''Add the following items to your form and name them as indicated:
''Four(4) text boxes - Named: tbIDNumber, tbName, tbAddress, and tbContactName.
''One(1) Command button - Named Command1

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 Name, Address, ContactNumber FROM YourTableName WHERE IDNumber = '" & tbIDNumber.Text & "'")
    End With

    If rs.RecordCount > 0 Then
        tbName.Text = rs(0).Value & ""
        tbAddress.Text = rs(1).Value & ""
        tbContactName.Text = rs(2).Value & ""
    End If

Exit_Sub:
rs.Close
Set rs = Nothing
Set DB = Nothing
End Sub
将类模块对象添加到您的项目中,并将其命名为CDATA Base。然后将以下代码复制到其中:

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 & ";"
''If you are using MS Access as your back end you will need to change the connection string to the following:
     ''ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _

''If you are using a DNS-Less connection to SQL Server, then you will need to change the connection string to the following:
     ''ConnectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=" & m_sSQLUserID & ";Password=" & m_sSQLPassword & ";"

''You can find more Connection Strings at http://connectionstrings.com/

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

这应该可以解决问题。

到目前为止您尝试了哪些代码?感谢您的回复。请你帮个忙好吗?请给我一些教程的网站,在那里我可以学习这个VB6。事实上,我已经浏览了很多网站,但不幸的是,我没有找到我需要的教程。我需要简单的数据输入、搜索、编辑、删除和查看使用DataControl而不是Adodc的程序。我找到了www.vb6.us,在那里我通过修改文本框和DataControl的属性学会了连接,也就是说,我数据库的值会自动出现在文本框中,这不是我的意思。我有一个文本框,这是IDNumber(索引)的必填字段,还有一个命令按钮,每当您单击它时,输入文本框的IDNumber将在数据库中查找其进一步信息(姓名、地址、联系人号码),并将分别显示在文本框中。请帮帮我。。。等待你的答复。谢谢你!给你,还有。我认为您最好使用ADO访问数据,看看这个,因为它将说明如何通过ADO访问数据。感谢加载代码,但我认为这是为那些中级程序员准备的。你能帮我用VB6做个程序吗?我想掌握你的密码。请帮帮我!我是一名初级程序员。我如何与您联系?我需要你的帮助,我在Skype上有一个帐户,这是我的Skype名字:aaronarela。。。。还有我的gmail eadd:aerohn。arela@gmail.comI刚刚通过名为Stream file的网站向您发送了一个示例项目文件。您需要编辑代码以包含DSN的名称以及用户名和密码。顺便说一句,这是文件的附件,以防你没有收到电子邮件。我希望这有帮助!这是一个在Command1_Click Events中实例化的事件,先生,你能给我一个使用ADODB的非常简单的代码吗?我以前使用DataControl,但我没有这类代码的背景知识。求你了,先生!非常感谢你的努力。。。