连接到 wampserver MySQL 在 PHP 中有效但在 vb.NET 中无效
Connecting to wampserver MySQL works in PHP but not in vb.NET
我有一个本地 wampserver 运行 MySQL。我已通过 PHP:
成功连接到它
<?php
$servername = "localhost";
$username = "pyramid";
$password = "pyramid";
try {
$conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
这非常有效,我得到了结果 "Connected Successfully"。
然而,当我在 vb.net 中使用此代码尝试相同的操作时:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim ConnectionString As String
Dim SQLCon As SqlConnection
ConnectionString = "Server=localhost;Database=test;Uid=pyramid;Pwd=pyramid"
SQLCon = New SqlConnection(ConnectionString)
Try
SQLCon.Open()
Catch ex As Exception
If ex.InnerException IsNot Nothing Then
MessageBox.Show(ex.InnerException.Message)
Else
MessageBox.Show(ex.Message)
End If
Finally
If SQLCon.State = ConnectionState.Open Then SQLCon.Close()
End Try
End Sub
End Class
经过 5 到 10 秒的漫长等待,我得到 "The system cannot find the file specified"
两者具有完全相同的用户名、密码服务器和数据库名称,但仍然vb.NET拒绝连接。
我已经尝试了几乎所有我能想到的方法,并且用尽了我的选择。你能说点什么吗?
您需要下载 .NET Framework (http://dev.mysql.com/downloads/connector/net/) 的 MySQL 连接器,然后在您的项目中引用 .DLL。
Imports MySqL.Data.MySqlClient
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim ConnectionString As String
Dim SQLCon As MySqlConnection
ConnectionString = "Server=localhost;Database=test;Uid=pyramid;Pwd=pyramid"
SQLCon = New MySqlConnection(ConnectionString)
Try
SQLCon.Open()
Catch ex As Exception
If ex.InnerException IsNot Nothing Then
MessageBox.Show(ex.InnerException.Message)
Else
MessageBox.Show(ex.Message)
End If
Finally
If SQLCon.State = ConnectionState.Open Then SQLCon.Close()
End Try
End Sub
End Class
我有一个本地 wampserver 运行 MySQL。我已通过 PHP:
成功连接到它<?php
$servername = "localhost";
$username = "pyramid";
$password = "pyramid";
try {
$conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
这非常有效,我得到了结果 "Connected Successfully"。
然而,当我在 vb.net 中使用此代码尝试相同的操作时:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim ConnectionString As String
Dim SQLCon As SqlConnection
ConnectionString = "Server=localhost;Database=test;Uid=pyramid;Pwd=pyramid"
SQLCon = New SqlConnection(ConnectionString)
Try
SQLCon.Open()
Catch ex As Exception
If ex.InnerException IsNot Nothing Then
MessageBox.Show(ex.InnerException.Message)
Else
MessageBox.Show(ex.Message)
End If
Finally
If SQLCon.State = ConnectionState.Open Then SQLCon.Close()
End Try
End Sub
End Class
经过 5 到 10 秒的漫长等待,我得到 "The system cannot find the file specified"
两者具有完全相同的用户名、密码服务器和数据库名称,但仍然vb.NET拒绝连接。
我已经尝试了几乎所有我能想到的方法,并且用尽了我的选择。你能说点什么吗?
您需要下载 .NET Framework (http://dev.mysql.com/downloads/connector/net/) 的 MySQL 连接器,然后在您的项目中引用 .DLL。
Imports MySqL.Data.MySqlClient
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim ConnectionString As String
Dim SQLCon As MySqlConnection
ConnectionString = "Server=localhost;Database=test;Uid=pyramid;Pwd=pyramid"
SQLCon = New MySqlConnection(ConnectionString)
Try
SQLCon.Open()
Catch ex As Exception
If ex.InnerException IsNot Nothing Then
MessageBox.Show(ex.InnerException.Message)
Else
MessageBox.Show(ex.Message)
End If
Finally
If SQLCon.State = ConnectionState.Open Then SQLCon.Close()
End Try
End Sub
End Class