C#连接mysql三种方式

第一种方式:
使用MySQLDriverCS.dll连接
MySQLDriverCS软件下载:https://sourceforge.net/projects/ mysqldrivercs/?source=typ_redirect
安装完之后再引用中添加引用,找到安装目录,找到MySQLDriverCS.dll文件,然后添加using MySQLDriverCS.dll文件
参考网址:/kf/201401/272682.html
C#连接mysql代码
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MySQLConnection DBConn;
DBConn = new MySQLConnection( new MySQLConnectionString( "10.99.19.121" , "haha" , "root" , "root" , 3306 ).AsString);
//DBConn = new MySQLConnection(new MySQLConnectionString("数据源","数据库名", "用户名", "密码", 端口号).AsString);
try
{
DBConn.Open(); // 执行查询语句
MessageBox.Show( "数据库已经连接了!" );
string sql = "select * from tb_user" ;
MySQLDataAdapter mda = new MySQLDataAdapter(sql, DBConn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1" );
this .dataGridView1.DataSource = ds.Tables[ "table1" ];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
DBConn.Close();


或者这么写:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
MySQLConnectionString constr = new MySQLConnectionString( "10.99.19.121" , "haha" , "root" , "root" , 3306 );
MySQLConnection DBConn = new MySQLConnection(constr.AsString);
//MySQLConnection DBConn;
//DBConn = new MySQLConnection(new MySQLConnectionString("10.99.19.121","haha", "root", "root", 3306).AsString);
 
 
try
{
DBConn.Open(); // 执行查询语句
MessageBox.Show( "数据库已经连接了!" );
string sql = "select * from tb_user" ;
MySQLDataAdapter mda = new MySQLDataAdapter(sql, DBConn);
DataSet ds = new DataSet();
mda.Fill(ds, "table1" );
this .dataGridView1.DataSource = ds.Tables[ "table1" ];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
DBConn.Close();





第二种方法:
使用MySql.Data.dll连接
参考网址:https://www.cnblogs.com/sosoft/p/3906136.html
使用过程
dll文件修复方法:
1、解压下载的文件。
2、复制文件“mysql.data.dll”到 系统目录下。
3、系统目录一般为:C:\WINNT\System32 64位系统为C:\Windows\SysWOW64
4、最后点击开始菜单-->运行-->输入regsvr32 mysql.data.dll 后,回车即可解决错误提示!
在再引用中添加引用,找到C:\Windows\SysWOW64目录,找到mysql.data.dll文件,然后添加using MySql.Data.MySqlClient;文件
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string M_str_sqlcon = "server=10.99.19.121;user id=root;password=root;database=haha" ; //根据自己的设置
MySqlConnection mycon = new MySqlConnection();
mycon.ConnectionString = M_str_sqlcon;
try
{
mycon.Open();
 
 
MessageBox.Show( "数据库已经连接了!" );
string sql = "select * from tb_user" ;
MySqlDataAdapter mda = new MySqlDataAdapter(sql, mycon);
DataSet ds = new DataSet();
mda.Fill(ds, "table1" );
this .dataGridView1.DataSource = ds.Tables[ "table1" ];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
mycon.Close();


第三种方式:

通过ODBC访问mysql数据库

(没有时间研究那么多,之后会补充进来)

个人建议C#和sqlserver配合使用很好,但是和mysql不是说不好,只是不太合适,试想,你做一个项目,你还要给人家安装一个软件才能连接上数据库,感觉太麻烦,不专业,当然可以自己写一个库,但是很麻烦,而且又不是谁都会,所以个人建议用sqlserver,个人建议,不喜勿喷!!


转自:https://www.2cto.com/database/201508/437072.html

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C连接MySQL需要使用MySQL连接器,并且需要引用System.Data命名空间和MySql.Data.MySqlClient命名空间。下面是一个示例代码,展示了如何使用C#连接MySQL数据库: ```csharp using System; using System.Data; using MySql.Data.MySqlClient; public class MySQLConnector { private MySqlConnection connection; public MySQLConnector(string connectionString) { connection = new MySqlConnection(connectionString); } public void Connect() { try { connection.Open(); Console.WriteLine("数据库连接成功!"); } catch (Exception ex) { Console.WriteLine("数据库连接失败:" + ex.Message); } } public void Disconnect() { connection.Close(); Console.WriteLine("数据库连接已断开!"); } public DataTable SelectData(string tableName, string columns, string condition) { DataTable dataTable = new DataTable(); string sql = ""; if (string.IsNullOrEmpty(condition)) { sql = "SELECT " + columns + " FROM " + tableName; } else { sql = "SELECT " + columns + " FROM " + tableName + " WHERE " + condition; } MySqlCommand command = new MySqlCommand(sql, connection); MySqlDataAdapter adapter = new MySqlDataAdapter(command); adapter.Fill(dataTable); return dataTable; } public int UpdateData(string tableName, string columnsAndValues, string condition) { string sql = "UPDATE " + tableName + " SET " + columnsAndValues + " WHERE " + condition; MySqlCommand command = new MySqlCommand(sql, connection); int rowsAffected = command.ExecuteNonQuery(); return rowsAffected; } } ``` 以上代码展示了一个MySQL连接器的基本操作,包括连接数据库、断开连接、查询数据和修改数据。你可以根据自己的需求进行相应的修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值