本文共 1720 字,大约阅读时间需要 5 分钟。
01:添加mysqlData.dll到C#项目的引用中,就可以编写程序进行数据库的操作了
class Program { static void Main(string[] args) { String mysqlStr = "server=localhost;User Id=root;password=1234;Database=mygame"; //连接数据库 MySqlConnection connection=new MySqlConnection(mysqlStr); //打开数据库 connection.Open(); //查询数据表 string commandStr ="select* from server"; //执行命令语句sql MySqlCommand command=new MySqlCommand(commandStr,connection); //获取查询结果 using (MySqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { int id= reader.GetInt32(0); string name =reader.GetString(1); string ip = reader.GetString(2); Console.WriteLine(id+" "+ name+" "+ip); } } //关闭数据集 connection.Close(); Console.ReadKey(); } }02:插入操作
string str= "insert into mytable set id = @i, username = @u, password = @p"; //插入数据 MySqlCommand command1 = new MySqlCommand(str, connection); command1.Parameters.AddWithValue("i", 3); command1.Parameters.AddWithValue("u", "小伙子"); command1.Parameters.AddWithValue("p", "185255"); command1.ExecuteNonQuery();
删除
MySqlCommand cmd = new MySqlCommand("delete from user where id = @id", conn); cmd.Parameters.AddWithValue("id", 18); cmd.ExecuteNonQuery();
更新
MySqlCommand cmd = new MySqlCommand("update user set password = @pwd where id = 14", conn); cmd.Parameters.AddWithValue("pwd", "sikiedu.com"); cmd.ExecuteNonQuery();
转载地址:http://bjrxo.baihongyu.com/