分类: 数据库开发技术
2010-07-17 17:53:06
有好多解决方案。其中的一种方法。首先需要下载安装(http://dev.mysql.com/downloads/connector/net/5.0.html) MySQL Connector/Net,这是一个MySQL的ADO.NET驱动。安装完后就可以很方便地操作MySQL数据库了。
连接代码很简单
VB.NET
'引入MySQL
Imports MySql.Data.MySqlClient
Private Sub connectBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connectBtn.Click
Dim connStr As String
Dim conn As MySqlConnection
If Not conn Is Nothing Then conn.Close()
connStr = String.Format(“server={0};user id={1}; password={2}; database=mysql; pooling=false”, _
server.Text, userid.Text, password.Text)
Try
conn = New MySqlConnection(connStr)
conn.Open()
GetDatabases()
Catch ex As MySqlException
MessageBox.Show(“Error connecting to the server: “ + ex.Message)
End Try
End Sub
//#################################
C#
using MySql.Data.MySqlClient;
private void connectBtn_Click(object sender, System.EventArgs e)
{
if (conn != null)
conn.Close();
string connStr = String.Format(“server={0};user id={1}; password={2}; database=mysql; pooling=false”,
server.Text, userid.Text, password.Text );
try
{
conn = new MySqlConnection( connStr );
conn.Open();
// GetDatabases();
}
catch (MySqlException ex)
{
MessageBox.Show( “Error connecting to the server: “ + ex.Message );
}
}