如何使用c#连接到Mysql数据库?
我有这个代码
entstring MyConnection = "server=localhost; port=3306; databae=database_name; uid=root;pwd=;sslMode=None; ;
答案 0 :(得分:1)
首先从nuget包管理器下载MySql.Data
包,然后添加命名空间
//Add MySql Library
using MySql.Data.MySqlClient;
连接代码 -
string connString = "server=server;user=user;database=db;password=*****;";
MySqlConnection con = new MySqlConnection(connString);
答案 1 :(得分:0)
这是一个想法。
string MyConnection = "server=localhost; port=3306; databae=database_name; uid=root;pwd=;sslMode=None; ;
string Query = "INSERT INTO table_name(username,password)VALUES('"+textbox_name.Text+"','"+password_name.Password+"');
MySqlConnection con = new MySqlConnection(MyConnection);
MySqlCommand command = new MySqlCommand(Query,con);
con.open();
try{
command.ExcuteNonQuery();
}
catch(Expection ex)
{
MessageDialog box = new MessageDialog(ex.ToString());
box.ShowAsync();
}
con.close();
}