我有以下代码:
Dim string_conectare As String = "Data Source=|DataDirectory|\Database1.sdf"
Dim conexiune As SqlConnection
conexiune = New SqlConnection(string_conectare)
conexiune.Open()
If conexiune.State = ConnectionState.Open Then
MsgBox("OK")
Else
MsgBox("not ok")
End If正如你所看到的,我想打开一个到数据库的连接,但是每次我想测试它的时候,我都会得到这个错误:
A network-related or instance-specific error occurred while establishing a
connection to SQL Server. The server was not found or was not accessible.
Verify that the instance name is correct and that SQL Server is
configured to allow remote connections. (provider: SQL Network Interfaces,
error: 26 - Error Locating Server/Instance Specified)我挣扎了两个多小时,所以请帮帮我!
稍后编辑:
我试过了:
Dim string_conectare As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database1.sdf;Persist Security Info=True"
Dim conexiune As OleDbConnection
conexiune = New OleDbConnection(string_conectare)
conexiune.Open()
If conexiune.State = ConnectionState.Open Then
MsgBox("OK")
Else
MsgBox("not ok")
End If但是它抛出了这个错误:
Unrecognized database format发布于 2012-06-06 06:51:22
下面是我用来连接数据库的相同代码的摘录:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim conn As MySqlConnection
'Connect to the database using these credentials
conn = New MySqlConnection
conn.ConnectionString = "server=your server site (generally long url); user id=login id for mysql user; password=self explanatory; database=name of the DB you're trying to reach"
'Try and connect (conn.open)
Try
conn.Open()
Catch myerror As MySqlException 'If it fails do this... (i.e. no internet connection, etc.)
MsgBox("Error connecting to database. Check your internet connection.", MsgBoxStyle.Critical)
End Try
'MySQL query (where to call for information)
Dim myAdapter As New MySqlDataAdapter
'Tell where to find the file with the emails/passes stored
Dim sqlquery = "SELECT * FROM the database you selected above WHERE Email = '" & txtEmail.Text & "' AND Password = '" & txtPassword.Text & "'"
Dim myCommand As New MySqlCommand
myCommand.Connection = conn
myCommand.CommandText = sqlquery
'Start query
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader
If myData.HasRows = 0 Then
MsgBox("Invalid email address or password.", MsgBoxStyle.Critical)
Else
MsgBox("Logged in as " & txtEmail.Text & ".", MsgBoxStyle.Information)
Me.Close()
End If
End Sub试试看。
请确保将资源MySQL.Data添加到项目中,并使用以下命令调用它:
Imports MySQL.Data.MySQLClient还有!确保在创建数据库时启用了外部数据库访问。如果你不这样做,那么VB程序将无法访问它,它将限制只访问您的网站主机。
发布于 2012-06-07 03:04:09
Jet OLEDB提供程序用于Access数据库。它不能处理sdf文件。尝试使用正确的连接字符串。
您可以通过此网站获取数据库的正确连接字符串:www.ConnectionStrings.com
发布于 2015-04-16 17:27:13
应用程序是否构建在共享存储上,并且您正在尝试从本地计算机运行它?如果包含应用程序的服务器无法访问数据库,则可能会出现问题
https://stackoverflow.com/questions/10904007
复制相似问题