转之:行走的DT
[C#]获取连接MySql数据库及常用的CRUD操作
测试如下:
首先添加引用:MySql.Data.dll
private string mysqlString = "server=127.0.0.1;port=3306;user=root;password=123; database=cs;";
①建立mysql数据库链接
//建立mysql数据库链接
public MySqlConnection getMySqlConn()
{
//string constr = "server=localhost;User Id=root;password=123456;Database=qqmusicdistinct";
string constr = mysqlString;
MySqlConnection mycon = new MySqlConnection(constr);
return mycon;
}
②建立执行命令语句对象
//建立执行命令语句对象
public MySqlCommand getSqlCommand(String sql, MySqlConnection mysql)
{
MySqlCommand mySqlCommand = new MySqlCommand(sql, mysql);
return mySqlCommand;
}
③编写sql语句,对数据做相应处理
//编写sql语句,对数据做相应处理
public void setData()
{
MySqlConnection mysql = getMySqlConn();
//查询sql
String sqlSearch = "select * from student";
//插入sql
//String sqlInsert = "insert into student values (12,'张三',25,'大专')";
//修改sql
//String sqlUpdate = "update student set name='李四' where id= 3";
//删除sql
//String sqlDel = "delete from student where id = 12";
//四种语句对象
MySqlCommand mySqlCommand = getSqlCommand(sqlSearch, mysql);
//MySqlCommand mySqlCommand = getSqlCommand(sqlInsert, mysql);
//MySqlCommand mySqlCommand = getSqlCommand(sqlUpdate, mysql);
//MySqlCommand mySqlCommand = getSqlCommand(sqlDel, mysql);
mysql.Open();
getResultset(mySqlCommand);
//getInsert(mySqlCommand);
//getUpdate(mySqlCommand);
//getDel(mySqlCommand);
//记得关闭
mysql.Close();
}
④Ⅰ.查询并获得结果集并遍历
//查询并获得结果集并遍历
public void getResultset(MySqlCommand mySqlCommand)
{
MySqlDataReader reader = mySqlCommand.ExecuteReader();
try
{
while (reader.Read())
{
if (reader.HasRows)
{
//MessageBox.Show("歌曲名:" + reader.GetString(1) + "|歌手:" + reader.GetString(2));
}
}
}
catch (Exception)
{
MessageBox.Show("查询失败了!");
}
finally
{
reader.Close();
}
}
④Ⅱ.添加数据
//添加数据
public static void getInsert(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("插入数据失败了!" + message);
}
}
④Ⅲ.修改数据
//修改数据
public static void getUpdate(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("修改数据失败了!" + message);
}
}
④Ⅳ.删除数据
//删除数据
public static void getDel(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("删除数据失败了!" + message);
}
}
总代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string mysqlString = "server=127.0.0.1;port=3306;user=root;password=123; database=cs;";
public MySqlConnection conn = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
//建立mysql数据库链接
public MySqlConnection getMySqlConn()
{
string constr = mysqlString;
MySqlConnection mycon = new MySqlConnection(constr);
return mycon;
}
//建立执行命令语句对象
public MySqlCommand getSqlCommand(String sql, MySqlConnection mysql)
{
MySqlCommand mySqlCommand = new MySqlCommand(sql, mysql);
return mySqlCommand;
}
private void button1_Click_1(object sender, EventArgs e)
{
MySqlConnection conn = getMySqlConn();
try
{
conn.Open();//打开连接
Console.WriteLine("已经建立连接");
MySqlDataAdapter sda = new MySqlDataAdapter("select * from stu", conn);//读取表数据
DataTable dt = new DataTable();//dt是一个表类型。
sda.Fill(dt);//把sda读取的数据填到dt里
dataGridView1.DataSource = dt;//把dt的内容绑定到Gridview1里显示。
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
conn.Dispose();
}
//添加数据
public static void getInsert(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("插入数据失败了!" + message);
}
}
private void button2_Click(object sender, EventArgs e)
{
MySqlConnection mysql = getMySqlConn();
//插入sql
String sqlInsert = "insert into stu values ('lijie','m',23)";
MySqlCommand mySqlCommand = getSqlCommand(sqlInsert, mysql);
mysql.Open();
getInsert(mySqlCommand);
mysql.Close();
}
//删除数据
public static void getDel(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("删除数据失败了!" + message);
}
}
private void button3_Click(object sender, EventArgs e)
{
MySqlConnection mysql = getMySqlConn();
//插入sql
String sqlDel = "delete from stu where name = 'lijie'";
MySqlCommand mySqlCommand = getSqlCommand(sqlDel, mysql);
mysql.Open();
getDel(mySqlCommand);
mysql.Close();
}
//修改数据
public static void getUpdate(MySqlCommand mySqlCommand)
{
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("修改数据失败了!" + message);
}
}
private void button4_Click(object sender, EventArgs e)
{
MySqlConnection mysql = getMySqlConn();
//插入sql
String sqlUpdate = "update stu set age=0 where name='lijie'";
MySqlCommand mySqlCommand = getSqlCommand(sqlUpdate, mysql);
mysql.Open();
getUpdate(mySqlCommand);
mysql.Close();
}
}
}