在mysql官網https://dev.mysql.com/downloads/connector/net 下載".NET&MONO"版本的mysql connector。php
在c#的reference中添加v4文件夾中全部的dll的引用。v4對應".NET FRAME4",在project中的屬性中選擇對應的".NET FRAME"版本。mysql
在程序首列添加"using MySql.Data.MySqlClient;"sql
鏈接代碼以下所示。c#
String str = "Server=localhost; Database = php; Uid = root; Pwd = redhat";ide
tryci
{get
msc = new MySqlConnection(str);it
msc.Open();io
if (msc.State == ConnectionState.Open)class
{
MessageBox.Show("database is open");
}
}
catch
{
MessageBox.Show("database is open error!");
}