C#连接MySQL数据库的方法步骤
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpConnectMySQL
{
public class MySqlHelper
{
private static string connstr = "server=127.0.0.1;database=shop;username=root;password=123456;";
#region 执行查询语句,返回MySqlDataReader
///
/// 执行查询语句,返回MySqlDataReader
///
///
///
public static MySqlDataReader ExecuteReader(string sqlString)
{
MySqlConnection connection = new MySqlConnection(connstr);
MySqlCommand cmd = new MySqlCommand(sqlString, connection);
MySqlDataReader myReader = null;
try
{
connection.Open();
myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw new Exception(e.Message);
}
finally
{
if (myReader == null)
{
cmd.Dispose();
connection.Close();
}
}
}
#endregion
#region 执行带参数的查询语句,返回 MySqlDataReader
///
/// 执行带参数的查询语句,返回MySqlDataReader
///
///
///
///
public static MySqlDataReader ExecuteReader(string sqlString, params MySqlParameter[] cmdParms)
{
MySqlConnection connection = new MySqlConnection(connstr);
MySqlCommand cmd = new MySqlCommand();
MySqlDataReader myReader = null;
try
{
PrepareCommand(cmd, connection, null, sqlString, cmdParms);
myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw new Exception(e.Message);
}
finally
{
if (myReader == null)
{
cmd.Dispose();
connection.Close();
}
}
}
#endregion
#region 执行sql语句,返回执行行数
///
/// 执行sql语句,返回执行行数
///
///
///
public static int ExecuteSql(string sql)
{
using (MySqlConnection conn = new MySqlConnection(connstr))
{
using (MySqlCommand cmd = new MySqlCommand(sql, conn))
{
try
{
conn.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (MySql.Data.MySqlClient.MySqlException e)
{
conn.Close();
//throw e;
Console.WriteLine(e.Message);
}
finally
{
cmd.Dispose();
conn.Close();
}
}
}
return -1;
}
#endregion
#region 执行带参数的sql语句,并返回执行行数
///
/// 执行带参数的sql语句,并返回执行行数
///
///
///
///
public static int ExecuteSql(string sqlString, params MySqlParameter[] cmdParms)
{
using (MySqlConnection connection = new MySqlConnection(connstr))
{
using (MySqlCommand cmd = new MySqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, sqlString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
#endregion
#region 执行查询语句,返回DataSet
///
/// 执行查询语句,返回DataSet
///
///
///
public static DataSet GetDataSet(string sql)
{
using (MySqlConnection conn = new MySqlConnection(connstr))
{
DataSet ds = new DataSet();
try
{
conn.Open();
MySqlDataAdapter DataAdapter = new MySqlDataAdapter(sql, conn);
DataAdapter.Fill(ds);
}
catch (Exception ex)
{
//throw ex;
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
return ds;
}
}
#endregion
#region 执行带参数的查询语句,返回DataSet
///
/// 执行带参数的查询语句,返回DataSet
///
///
///
///
public static DataSet GetDataSet(string sqlString, params MySqlParameter[] cmdParms)
{
using (MySqlConnection connection = new MySqlConnection(connstr))
{
MySqlCommand cmd = new MySqlCommand();
PrepareCommand(cmd, connection, null, sqlString, cmdParms);
using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
return ds;
}
}
}
#endregion
#region 执行带参数的sql语句,并返回 object
///
/// 执行带参数的sql语句,并返回object
///
///
///
///
public static object GetSingle(string sqlString, params MySqlParameter[] cmdParms)
{
using (MySqlConnection connection = new MySqlConnection(connstr))
{
using (MySqlCommand cmd = new MySqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, sqlString, cmdParms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
#endregion
///
/// 执行存储过程,返回数据集
///
/// 存储过程名
/// 存储过程参数
///
public static DataSet RunProcedureForDataSet(string storedProcName, IDataParameter[] parameters)
{
using (MySqlConnection connection = new MySqlConnection(connstr))
{
DataSet dataSet = new DataSet();
connection.Open();
MySqlDataAdapter sqlDA = new MySqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dataSet);
connection.Close();
return dataSet;
}
}
///
/// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
///
/// 数据库连接
/// 存储过程名
/// 存储过程参数
///
private static MySqlCommand BuildQueryCommand(MySqlConnection connection, string storedProcName,
IDataParameter[] parameters)
{
MySqlCommand command = new MySqlCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (MySqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
}
#region 装载MySqlCommand对象
///
/// 装载MySqlCommand对象
///
private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, string cmdText,
MySqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
{
cmd.Transaction = trans;
}
cmd.CommandType = CommandType.Text; //cmdType;
if (cmdParms != null)
{
foreach (MySqlParameter parm in cmdParms)
{
cmd.Parameters.Add(parm);
}
}
}
#endregion
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播