环境配置
C#操作mysql需要依赖一个库
Mysql.Data.dll: 下载
然后引用
准备两个表格
表格【roles】
表格【users】
一个存储过程
存储过程的代码,查找两个表格,两个输入,一个输出
CREATE DEFINER=`root`@`localhost` PROCEDURE `selectPerson`(IN ssdate date,IN age INT,OUT u_name INT)
BEGIN
SELECT * FROM users;
SELECT * FROM roles;
SET u_name=123;
END
可以在文件中找到测试的数据库
这里采用mysql连接池,封装MysqlPool.cs
MysqlPool.cs
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
private MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.Server = "localhost"; //数据库ip
builder.Port = 3306; //端口
builder.Database = "test";//数据库名称
builder.UserID = "root";//用户名
builder.Password = "root";//密码
builder.AllowZeroDateTime = true;//支持date与datetime
builder.Pooling = true; //连接池开启
builder.MaximumPoolSize = 20; //连接池的最大数量
MySqlConnection connection = new MySqlConnection(builder.ConnectionString);
*/
//mysql数据库连接池
public class MysqlPool
{
private static MySqlConnection connection = null;
public MysqlPool(MySqlConnectionStringBuilder builder)
{
connection = new MySqlConnection(builder.ConnectionString);
}
/// <summary>
/// 测试连接
/// </summary>
/// <returns></returns>
public bool TestConn()
{
try
{
connection.Open();
connection.Close();
return true;
}
catch (Exception)
{
connection.Close();
return false;
}
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sql">语句</param>
/// <returns></returns>
public DataTable Query(string sql)
{
try
{
connection.Open();
MySqlDataReader reader = new MySqlCommand(sql, connection).ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();
dt.Load(reader);
connection.Close();
return dt;
}
catch (Exception err)
{
connection.Close();
throw err; //抛出异常
}
}
/// <summary>
/// 多表查询
/// </summary>
/// <param name="sql"></param>
/// <param name="tbname">表名</param>
/// <returns></returns>
public DataSet Query(string sql, string tbname)
{
try
{
connection.Open();
MySqlCommand command = new MySqlCommand(sql, connection);
DataSet ds = new DataSet();
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
adapter.Fill(ds, tbname);
connection.Close();
return ds;
}
catch (Exception err)
{
connection.Close();
throw err;
}
}
/// <summary>
/// 增删改
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public bool Execute(string sql)
{
try
{
connection.Open();
int ret = new MySqlCommand(sql, connection).ExecuteNonQuery();
connection.Close();
return ret == 1;
}
catch (Exception err)
{
connection.Close();
throw err;
}
}
/***************************************************存储过程******************************************************/
public ProcedureResponse Query(string procedureName, Dictionary<string, string> inParameters, List<string> outParameters)
{
try
{
connection.Open();
MySqlCommand mySqlCommand = new MySqlCommand(procedureName, connection);
mySqlCommand.CommandType = CommandType.StoredProcedure;//设置为存储过程模式
if (inParameters != null)
{
foreach (string key in inParameters.Keys)
{
mySqlCommand.Parameters.Add(key, MySqlDbType.VarChar).Value = inParameters[key];
}
}
if (outParameters != null)
{
foreach (string key in outParameters)
{
mySqlCommand.Parameters.Add(key, MySqlDbType.VarChar);
mySqlCommand.Parameters[key].Direction = ParameterDirection.Output;
}
}
//执行
mySqlCommand.ExecuteNonQuery();
ProcedureResponse procedureResponse = new ProcedureResponse();
//获取过程存储中查询到的所有返回值参数
if (outParameters != null)
{
foreach (string key in outParameters)
{
procedureResponse.fields.Add(key, mySqlCommand.Parameters[key].Value.ToString());
}
}
//获取过程中查询的所有表格
MySqlDataAdapter adapter = new MySqlDataAdapter(mySqlCommand);
adapter.Fill(procedureResponse.dataset, procedureName);
connection.Close();
return procedureResponse;
}
catch (Exception err)
{
connection.Close();
throw err; //抛出异常
}
}
}
//用于存储过程
public class ProcedureResponse
{
// 存储过程返回的所有表格
public DataSet dataset = new DataSet();
// 存储过程返回的所有输出值
public Dictionary<string, string> fields = new Dictionary<string, string>();
}
使用
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace c_sharp_mysqlpool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.Server = "localhost"; //数据库ip
builder.Port = 3306; //端口
builder.Database = "test";//数据库名称
builder.UserID = "root";//用户名
builder.Password = "root";//密码
builder.Pooling = true; //连接池开启
builder.MaximumPoolSize = 20; //连接池的最大数量
mysqltestPool = new MysqlPool(builder);
}
MysqlPool mysqltestPool = null;
//查询
private void button1_Click(object sender, EventArgs e)
{
string sql = "SELECT * FROM users;";
this.dataGridView1.DataSource = mysqltestPool.Query(sql);
}
//更新
private void button2_Click(object sender, EventArgs e)
{
string sql = "UPDATE users SET username='Xie Zhennan' WHERE username='Xie Zhennan';";
Console.WriteLine(mysqltestPool.Execute(sql));
}
//存储过程
private void button3_Click(object sender, EventArgs e)
{
Dictionary<string, string> inParameters = new Dictionary<string, string>();
List<string> outParameters = new List<string>();
inParameters.Add("age", "37");
inParameters.Add("ssdate", "2022-11-02");
outParameters.Add("u_name");
ProcedureResponse procedureResponse = mysqltestPool.Query("selectPerson", inParameters, outParameters);
Dictionary<string, string> fields = new Dictionary<string, string>();
fields = procedureResponse.fields;
this.dataGridView1.DataSource = procedureResponse.dataset.Tables[0];
this.dataGridView2.DataSource = procedureResponse.dataset.Tables[1];
}
}
}
下载
源码: 下载