MySQL 是一个真正的多用户、多线程SQL数据库服务器。SQL(结构化查询语言)是世界上最流行的和标准化的数据库语言。 MySQL 是以一个客户机/服务器结构的实现,它由一个服务器守护程序 mysqld 和很多不同的客户程序和库组成。不过这东西在*nix的世界比较流行,在windows的世界尤其是企业应用,还是MS的SQL Server占主导地位。MS的Visual Studio更是不可能原生支持了。
最近手头上有个小东西要做,其中需要涉及到MySQL数据库。以前从没做过基于MySQL的东西,上网一搜,原来有好多解决方案。下面提供其中的一种方法。剩下的有兴趣的自己去研究了。首先需要安装MySQL Connector/Net,这是一个MySQL的ADO.NET驱动。安装完后就可以很方便地操作MySQL数据库了。
字串1
连接代码很简单
VB.NET 字串5
‘引入MySQL 字串6
Imports MySql.Data.MySqlClient 字串1
字串8
Private Sub connectBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connectBtn.Click 字串3
Dim connStr As String 字串6
Dim conn As MySqlConnection 字串2
字串6
If Not conn Is Nothing Then conn.Close() 字串2
connStr = String.Format(“server={0};user id={1}; password={2}; database=mysql; pooling=false”, _字串3
server.Text, userid.Text, password.Text) 字串9
字串5
Try字串7
conn = New MySqlConnection(connStr) 字串5
conn.Open() 字串8
字串1
GetDatabases() 字串3
Catch ex As MySqlException 字串3
MessageBox.Show(“Error connecting to the server: “ + ex.Message) 字串6
End Try 字串9
End Sub 字串2
C#
字串7
using MySql.Data.MySqlClient; 字串1
字串4
private void connectBtn_Click(object sender, System.EventArgs e)字串1
{ 字串4
if (conn != null)字串7
conn.Close(); 字串8
字串2
string connStr = String.Format(“server={0};user id={1}; password={2}; database=mysql; pooling=false”,字串9
server.Text, userid.Text, password.Text );字串8
字串2
try 字串1
{ 字串5
conn = new MySqlConnection( connStr ); 字串2
conn.Open();字串3
字串7
// GetDatabases(); 字串7
} 字串1
字串6
catch (MySqlException ex) 字串6
{ 字串5
MessageBox.Show( “Error connecting to the server: “ + ex.Message );字串7
}字串2
} 字串8
更多函数请参考帮助文档。 字串7