在vb.net中连接登录表单(adodb.connection用法)

时间:2014-03-17 14:59:39

标签: mysql sql vb.net

我在登录表单中有此代码,但我不知道adodb.connection的用法,请任何人帮我修复它。我不知道为什么adodb这个词有一个曲折的错误线。

Imports System.Collections.ObjectModel
Imports System.Data
Imports System.Data.SqlClient
 Public Class LoginForm1
Dim rs_login As New adodb.Recordset
Dim cn_login As New adodb.Connection
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles   OK.Click
    rs_login = cn_login.Execute("select * from dbo.studentinfo where [Username] = '" & UsernameTextBox.Text & "' And [Password] = '" & PasswordTextBox.Text & "'")
    If rs_login.RecordCount = 0 Then
        MsgBox("Invalid Username!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
        Exit Sub
    Else
        rs_login = cn_login.Execute("select * from dbo.USERPASS where [Username] = '" & UsernameTextBox.Text & "' And [Password] = '" & PasswordTextBox.Text & "'")
        If rs_login.RecordCount = 0 Then
            MsgBox("Invalid Username", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
            Exit Sub
        Else
            user.Show()
            Me.Close()
        End If
    End If
End Sub

Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
    Me.Close()
    Home.Show()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    SI.Show()
    Me.Close()
End Sub

Private Sub LoginForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    With cn_login
        .CursorLocation = ADODB.CursorLocationEnum.adUseClient
        .Provider = "SQLOLEDB.1"
        .CommandTimeout = 0
        Dim con As New SqlConnection With {.ConnectionString = "Server=Danica-pc; database=SI;user=dandan;pwd=danica;"}
        .Open()

    End With
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您正在使用为VBA编写的数据库访问代码。可能是MS Access。在VB.NET中,这种方式非常不同。您将使用此Imports语句:

Imports System.Data.SqlClient 

在VB.NET中你会做这样的事情

Const StandardSecurityConnection As String = _
    "Server=Danica-pc;Database=SI;User Id=dandan;Password=danica;"
Const TrustedConnection As String = _
    "Server=Danica-pc;Database=SI;Trusted_Connection=True;"

Using conn As New SqlConnection(StandardSecurityConnection) 'Or TrustedConnection
    Dim sql As String = _
        "SELECT * FROM dbo.studentinfo WHERE Username = @usr AND Password = @pwd"
    Using command As New SqlCommand(sql, conn)
        command.Parameters.AddWithValue("@usr", UsernameTextBox.Text)
        command.Parameters.AddWithValue("@pwd", PasswordTextBox.Text)
        conn.Open()
        Using dr As SqlDataReader = command.ExecuteReader()
            Dim userCol AS Integer = dr.GetOrdinal("Username")
            Dim pwdCol AS Integer = dr.GetOrdinal("Password")
            While dr.Read()
                ConSole.WriteLine("User = {0}, Password = {1}", 
                    dr.GetString(userCol), dr.GetString(pwdCol))
            End While
        End Using
    End Using
End Using