vb.net无法连接到sql server

kiz8lqtg  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(414)

我正在尝试使用vb.net建立一个登录数据库。我正在使用此代码连接,但它不起作用!:

Public Class Form1
    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim rd As SqlDataReader

        con.ConnectionString = "Data Source=localhost; Initial Catalog=user; Integrated Security= true"
        cmd.Connection = con
        con.Open()
        cmd.CommandText = "select login, password from auth where login= '" & TextBox1.Text & "' and password = '" & TextBox2.Text & "' "

        rd = cmd.ExecuteReader
        If rd.HasRows Then
            Welcome.Show()
        Else
            MessageBox.Show("Login Failed", "error")
        End If
    End Sub
End Class

我在sql server上有一个名为“user”的数据库,在user中有一个名为“dbo.auth”的表。当我单击label2时,visualbasic说“con cannot be opened”,我正在使用mysql server workbench。我有什么办法可以解决这个问题吗?服务器也在我的本地网络上运行。

u91tlkcl

u91tlkcl1#

这对你有用吗?

Imports System.Data.SqlClient
Public Class LoginForm1
    Dim conn As SqlConnection
    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        conn = New SqlConnection
        conn.ConnectionString = "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"
        Try
            conn.Open()
            MsgBox("Connected!")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
     End Sub

此外,我认为你应该书签下面的链接。
https://www.connectionstrings.com/

相关问题