一、SqlHelper
1、SqlserverHelper
<connectionStrings> <add name="ConnectString" connectionString="server=192.168.10.181;database=BB;UID=sa;PWD=sa;" providerName="System.Data.SqlClient"/> </connectionStrings>
using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Text; namespace MCLiChengTest { public class SqlServerHelper { private static string connStr = ConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString; public static int ExecuteNonQuery(string sql) { using (var conn = new SqlConnection(connStr)) { conn.Open(); var cmd = new SqlCommand(sql, conn); var aRows = cmd.ExecuteNonQuery(); cmd.Dispose(); return aRows; } } public static object ExecuteScalar(string sql) { using (var conn = new SqlConnection(connStr)) { conn.Open(); var cmd = new SqlCommand(sql, conn); return cmd.ExecuteScalar(); } } public static void ExecuteSqlTrans(List<string> sqlList) { using (var conn = new SqlConnection(connStr)) { conn.Open(); SqlTransaction trans = conn.BeginTransaction(); var cmd = conn.CreateCommand(); cmd.Transaction = trans; try { foreach (var sql in sqlList) { cmd.CommandText = sql; cmd.ExecuteNonQuery(); } trans.Commit(); } catch (Exception ex) { try { Console.WriteLine("Mysql趟次存储错误:" + ex.ToString()); trans.Rollback(); } catch (Exception ex2) { throw; } } } } public static DataSet ExecuteDataset(string sql) { using (var conn = new SqlConnection(connStr)) { var ds = new DataSet(); var da = new SqlDataAdapter(sql, conn); da.Fill(ds); return ds; } } public static void ExecuteReader(string sql, Action<IDataReader> dataReaderAction) { using (var conn = new SqlConnection(connStr)) { conn.Open(); var cmd = new SqlCommand(sql, conn); using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { while (reader.Read()) { dataReaderAction(reader); } reader.Close(); } } } public static void ProExec(string proName, SqlParameter[] sqlParme) { using (var conn = new SqlConnection(connStr)) { SqlCommand cmd = new SqlCommand(proName, conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddRange(sqlParme); conn.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); } } public static void ProExecDb(string proName, SqlParameter[] sqlParme, string DbName) { using (var conn = new SqlConnection(connStr)) { SqlCommand cmd = new SqlCommand(proName, conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddRange(sqlParme); conn.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); } } public static SqlDataReader ProcExecResult(string proName, SqlParameter[] sqlParme) { using (var conn = new SqlConnection(connStr)) { SqlDataReader reader = null; SqlCommand cmd = new SqlCommand(proName, conn); try { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddRange(sqlParme); conn.Open(); //cmd.ExecuteNonQuery(); reader = cmd.ExecuteReader(); cmd.Dispose(); return reader; } catch (Exception ex) { Console.WriteLine("ProcExecResult 执行存储过程:[" + proName + "]发生错误:" + ex.ToString()); cmd.Dispose(); } finally { cmd.Dispose(); } return reader; } } } }
2、MySqlHelper
需要添加Mysql.Data.dll,在NuGet中
<connectionStrings> <add name="MysqlConnectionString" connectionString="server=192.168.1.244;Initial Catalog=ycztc;Integrated Security=False;Persist Security Info=False;User ID=server;Password=Exsun@2015;Charset=utf8" providerName="MySql.Data.MySqlClient" /> </connectionStrings>
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; using System.Data.SqlClient; using FrameworkCommon; namespace Exsun.CommunicationCommon.DataStorage.Mysql { public class MysqlHelper { private static string connStr = ConfigurationManager.ConnectionStrings["MysqlConnectionString"].ConnectionString; public static int ExecuteNonQuery(string sql) { using (var conn = new MySqlConnection(connStr)) { conn.Open(); var cmd = new MySqlCommand(sql, conn); var aRows=cmd.ExecuteNonQuery(); cmd.Dispose(); return aRows; } } public static object ExecuteScalar(string sql) { using (var conn = new MySqlConnection(connStr)) { conn.Open(); var cmd = new MySqlCommand(sql, conn); return cmd.ExecuteScalar(); } } public static void ExecuteSqlTrans(List<string> sqlList) { using (var conn = new MySqlConnection(connStr)) { conn.Open(); MySqlTransaction trans = conn.BeginTransaction(); var cmd = conn.CreateCommand(); cmd.Transaction = trans; try { foreach (var sql in sqlList) { cmd.CommandText = sql; cmd.ExecuteNonQuery(); } trans.Commit(); } catch (Exception ex) { try { LogHelper.Info("Mysql趟次存储错误:" + ex.ToString()); trans.Rollback(); } catch (Exception ex2) { throw; } } } } public static DataSet ExecuteDataset(string sql) { using (var conn = new MySqlConnection(connStr)) { var ds = new DataSet(); var da = new MySqlDataAdapter(sql, conn); da.Fill(ds); return ds; } } public static void ExecuteReader(string sql, Action<IDataReader> dataReaderAction) { using (var conn = new MySqlConnection(connStr)) { conn.Open(); var cmd = new MySqlCommand(sql, conn); using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { while (reader.Read()) { dataReaderAction(reader); } reader.Close(); } } } public static void ProExec(string proName, MySqlParameter[] sqlParme) { using (var conn = new MySqlConnection(connStr)) { MySqlCommand cmd = new MySqlCommand(proName, conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddRange(sqlParme); conn.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); } } public static void ProExecDb(string proName, MySqlParameter[] sqlParme, string DbName) { using (var conn = new MySqlConnection(connStr)) { MySqlCommand cmd = new MySqlCommand(proName, conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddRange(sqlParme); conn.Open(); cmd.ExecuteNonQuery(); cmd.Dispose(); } } public static MySqlDataReader ProcExecResult(string proName, MySqlParameter[] sqlParme) { using (var conn = new MySqlConnection(connStr)) { MySqlDataReader reader = null; MySqlCommand cmd = new MySqlCommand(proName, conn); try { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddRange(sqlParme); conn.Open(); //cmd.ExecuteNonQuery(); reader = cmd.ExecuteReader(); cmd.Dispose(); return reader; } catch (Exception ex) { LogHelper.Debug("ProcExecResult 执行存储过程:[" + proName + "]发生错误:" + ex.ToString()); cmd.Dispose(); } finally { cmd.Dispose(); } return reader; } } } }
3、其他帮助方法
public static readonly int FzTenantId = ConfigurationManager.AppSettings["FzTenantId"]?.ToInt(0) ?? 0; 【重在理解static、readonly的好处,以及?和 ??的使用】