C# 操作mysql数据库
-
引入与版本对应的 MySql.Data.dll,添加项目引用。下载:MySql.Data.dll
-
在 MySQL 中添加测试表,本例创建了一个数据库studentdb ,并在该数据库中创建了一个 user_info表
3.条件查询返回操作成功的个数
//获取用户输入的信息
String uName = "root";
String uPwd = "123";
//与数据库通信
string str = "server=localhost;User Id=root;password=root;Database=studentdb";//连接MySQL的字符串
MySqlConnection conn = new MySqlConnection(str);//实例化链接
conn.Open();//开启连接
#region 条件查询返回操作成功的个数
//查询返回操作成功的个数
String sql = "select * from user_info where user_name='" + uName + "' and user_pwd='" + uPwd + "'";
MySqlCommand mycmd = new MySqlCommand(sql, conn);
Object result = mycmd.ExecuteScalar();//执行查询,并返回查询结果集中第一行的第一列。所有其他的列和行将被忽略。没有返回NULL值
if (result != null)
{
int count = int.Parse(result.ToString());//返回个数
MessageBox.Show(count.ToString());
}
else
{
MessageBox.Show("用户名或密码错误", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Console.ReadLine();
#endregion
conn.Close();//关闭连接
4.查询user_info表中所有条目
string str = "server=localhost;User Id=root;password=root;Database=studentdb";//连接MySQL的字符串
MySqlConnection conn = new MySqlConnection(str);//实例化链接
conn.Open();//开启连接
#region 查询user_info表中所有条目
// 查询user_info表中所有条目
MySqlCommand mycmd = new MySqlCommand("select * from user_info", conn);
MySqlDataReader reader = mycmd.ExecuteReader();
// 逐行读取数据
while (reader.Read())
{
string username = reader.GetString("user_name");
string password = reader.GetString("user_pwd");
Console.WriteLine(username + ":" + password);
}
reader.Close();
#endregion
conn.Close();//关闭连接
5.插入一条数据
String uName = "hjr";
String uPwd = "123";
#region 插入一条数据
string str = "server=localhost;User Id=root;password=root;Database=studentdb";//连接MySQL的字符串
MySqlConnection conn = new MySqlConnection(str);//实例化链接
conn.Open();//开启连接
mysqlcommand cmd = new mysqlcommand("insert into user_info set user_name ='" + uName + "'" + ",user_pwd='" + uPwd + "'", conn);
int count = cmd.executenonquery();
if (count > 0)
{
messagebox.show("添加成功");
}
else
{
messagebox.show("添加失败");
}
#endregion
conn.Close();//关闭连接
6.删除id为3的数据
#region 删除id为3的数据
//与数据库通信
string str = "server=localhost;User Id=root;password=root;Database=studentdb";//连接MySQL的字符串
MySqlConnection conn = new MySqlConnection(str);//实例化链接
conn.Open();//开启连接
MySqlCommand cmd = new MySqlCommand("delete from user_info where user_id = @id", conn);
cmd.Parameters.AddWithValue("id", 3);
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败");
}
#endregion
conn.Close();//关闭
7.修改id为2的数据
//与数据库通信
string str = "server=localhost;User Id=root;password=root;Database=studentdb";//连接MySQL的字符串
MySqlConnection conn = new MySqlConnection(str);//实例化链接
conn.Open();//开启连接
#region 修改id为2的数据
MySqlCommand cmd = new MySqlCommand("update user_info set user_name = @name where user_id = @id", conn);
cmd.Parameters.AddWithValue("name", "hjr");
cmd.Parameters.AddWithValue("id", 2);
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("修改成功");
}
else
{
MessageBox.Show("修改失败");
}
#endregion
conn.Close();//关闭
如有错误,还请指出给予修改