using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //引用命名空间 using MySql.Data.MySqlClient; using MySql.Data; using System.Data; namespace DAL { public class MySqlDbHelper { //连接数据库 //MySqlConnection conn = new MySqlConnection(""); //public static string connection = ""; static string connectionStrings = "Server=127.0.0.1;Database=周考三;Uid=root;Pwd=root;"; /// <summary> /// 利用存储过程 返回受影响行数 添加 删除 修改 /// </summary> /// <param name="parameter"></param> /// <param name="procName"></param> /// <returns></returns> public static int ProcProcExecuteNonQuery(CommandType commType,string procName, MySqlParameter[] parm=null) { using (MySqlConnection conn=new MySqlConnection(connectionStrings)) { MySqlCommand cmd = new MySqlCommand(procName); cmd.Connection = conn; conn.Open(); cmd.CommandType = commType; if (parm!=null ) { cmd.Parameters.AddRange(parm); } int i = cmd.ExecuteNonQuery(); return i; } } /// <summary> /// 利用存储过程,返回一行一列 /// </summary> /// <param name="procName"></param> /// <param name="parameter"></param> /// <returns></returns> public static object ProcExecuteScalar(CommandType commType, string procName, MySqlParameter[] parm = null) { using (MySqlConnection conn = new MySqlConnection(connectionStrings)) { MySqlCommand cmd = new MySqlCommand(procName); cmd.Connection = conn; conn.Open(); cmd.CommandType = commType; if (parm != null) { cmd.Parameters.AddRange(parm); } object obj = cmd.ExecuteScalar(); return obj; } } /// <summary> /// 利用存储过程,获取数据表 查询 显示 /// </summary> /// <param name="procName"></param> /// <param name="parm"></param> /// <returns></returns> public static DataTable ProcDataAdapter(CommandType commType, string procName, MySqlParameter[] parm = null) { using (MySqlConnection conn = new MySqlConnection(connectionStrings)) { DataTable dt = new DataTable("com"); MySqlCommand cmd = new MySqlCommand(procName); cmd.Connection = conn; conn.Open(); cmd.CommandType = commType; if (parm!=null) { cmd.Parameters.AddRange(parm); } MySqlDataAdapter da = new MySqlDataAdapter(cmd); da.Fill(dt); return dt; } } } }