最近准备写一个仓库管理的项目
客户要求使用C#编写MySQL存储数据
为了方便,整理了数据库操作的工具类
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; /// <summary> /// @author: 房上的猫 /// /// @博客地址: https://www.cnblogs.com/lsy131479/ /// </summary> namespace ConsoleApp1 { public class MySQLDBHelp { #region 建立MySql数据库连接 /// <summary> /// 建立数据库连接. /// </summary> /// <returns>返回MySqlConnection对象</returns> public MySqlConnection getmysqlcon() { string M_str_sqlcon = "server=localhost;port=3306;user id=root;password=root;database=abc"; //根据自己的设置 MySqlConnection myCon = new MySqlConnection(M_str_sqlcon); return myCon; } #endregion #region 执行MySqlCommand命令 /// <summary> /// 执行MySqlCommand /// </summary> /// <param name="M_str_sqlstr">SQL语句</param> public void getmysqlcom(string M_str_sqlstr) { MySqlConnection mysqlcon = this.getmysqlcon(); mysqlcon.Open(); MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon); mysqlcom.ExecuteNonQuery(); mysqlcom.Dispose(); mysqlcon.Close(); mysqlcon.Dispose(); } #endregion #region 创建MySqlDataReader对象 /// <summary> /// 创建一个MySqlDataReader对象 /// </summary> /// <param name="M_str_sqlstr">SQL语句</param> /// <returns>返回MySqlDataReader对象</returns> public MySqlDataReader getmysqlread(string M_str_sqlstr) { MySqlConnection mysqlcon = this.getmysqlcon(); MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon); mysqlcon.Open(); MySqlDataReader mysqlread = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection); return mysqlread; } #endregion } }
原文地址:https://www.cnblogs.com/lsy131479/p/9212640.html
时间: 2024-10-28 16:27:35