千家信息网

在c#中如何连接Mysql数据库

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,在mysql官网https://dev.mysql.com/downloads/connector/net 下载".NET&MONO"版本的mysql connector。在c#的reference中
千家信息网最后更新 2025年02月02日在c#中如何连接Mysql数据库
  1. 在mysql官网https://dev.mysql.com/downloads/connector/net 下载".NET&MONO"版本的mysql connector。

  2. 在c#的reference中添加v4文件夹中所有的dll的引用。v4对应".NET FRAME4",在project中的属性中选择对应的".NET FRAME"版本。

  3. 在程序首列添加"using MySql.Data.MySqlClient;"

  4. 连接代码如下所示。

String str = "Server=localhost; Database = php; Uid = root; Pwd = redhat";

try

{

msc = new MySqlConnection(str);

msc.Open();

if (msc.State == ConnectionState.Open)

{

MessageBox.Show("database is open");

}

}

catch

{

MessageBox.Show("database is open error!");

}

0