我将连接VB.NET连接到Sql Server 2008数据库时出现问题。我已经尝试创建连接字符串,但它仍然无法附加和检测数据库查询。它显示的错误如此
这是我使用的代码:
Public Class Authentification
Private Sub Authentification_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buka()
End Sub
Private Sub btlogin_Click(sender As System.Object, e As System.EventArgs) Handles btlogin.Click
Dim Sql, user, pass As String
user = txtusername.Text
pass = txtpassword.Text
koneksi.Close()
Call buka()
Sql = "SELECT * FROM tAdmin WHERE username = '" + user + "' AND password='" + pass + "'"
cmd = New SqlCommand(Sql, koneksi)
baca = cmd.ExecuteReader()
If baca.Hasrows = True Then
MenuUtama.Show()
Me.Hide()
Else
MessageBox.Show("Username atau password salah", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtusername.Focus()
End If
txtusername.Text = ""
txtpassword.Text = ""
baca.Close()
cmd.Dispose()
End Sub
End Class
这是模块buka的代码:
Module Module1
Public koneksi As SqlConnection
Public data As DataSet
Public baca As SqlDataReader
Public adaptor As SqlDataAdapter
Public cmd As SqlCommand
Public ass As DataTable
Public str, sql As String
Public Sub buka()
str = "Data Source=DON-PC\SQLEXPRESS;Initial Catalog=dbFutsal;Persist Security Info=True;User ID=sa;Password=******"
koneksi = New SqlConnection(str)
Try
If koneksi.State = ConnectionState.Closed Then
koneksi.Open()
End If
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical, "Error")
End Try
End Sub
End Module
有人可以告诉我,我错了吗?先谢谢
答案 0 :(得分:0)
请检查您是否拥有登录用户" sa"在您的数据库中。同时尝试删除"坚持安全信息"来自连接字符串。
答案 1 :(得分:0)
首先,公开发布您的密码可能不是一个好主意。
但是,让我们保持简单。试试这个,看它是否连接。如果它不起作用,请发布错误消息。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim conDB As New SqlClient.SqlConnection
Dim strConnectionString As String = "Data Source=ServerName;" & _
"Database=DatabaseName;" & _
"User Id=UserName;" & _
"Password=Password;" & _
"Connect Timeout=90;"
Debug.WriteLine(strConnectionString)
conDB.ConnectionString = strConnectionString
Try
conDB.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
' Do something
End Sub
答案 2 :(得分:0)
此外,如果这与其上的SQL数据库不同,请尝试关闭防火墙并再次连接(在服务器和客户端上)。
答案 3 :(得分:0)
我唯一可以建议的是确保您运行SQL Server服务。
转到控制面板,服务,按字母顺序排序并查找SQL Server。启动所有尚未启动的服务。
答案 4 :(得分:0)
如果它是本地数据库(如前所述),请尝试Data Source=(local);
而不是Data Source=DON-PC\SQLEXPRESS;