常用方法
#region Dapper 数据库操作
#region Query 操作
/// <summary>
/// 无参获取单个实体modle
/// </summary>
/// <param name="sqlstr"></param>
/// <returns>T</returns>
public static T QueryModel<T>(string sqlstr) where T : new()
{
T t = new T();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
try
{
connection.Open();
t = connection.QueryFirstOrDefault<T>(sqlstr);
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
//throw new Exception(ex.Message);
connection.Close();
}
}
return t;
}
/// <summary>
/// 参数化获取单个实体modle
/// </summary>
/// <param name="sqlstr">执行sql语句</param>
/// <param name="parms">参数数组</param>
/// <returns>T</returns>
public static T QueryModel<T>(string sqlstr, params MySqlParameter[] parms) where T : new()
{
T t = new T();
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
try
{
connection.Open();
var args = new DynamicParameters(new { });
foreach (MySqlParameter parm in parms)
{
args.Add(parm.ParameterName,parm.Value);
}
t = connection.QueryFirstOrDefault<T>(sqlstr,args);
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
//throw new Exception(ex.Message);
connection.Close();
}
}
return t;
}
/// <summary>
/// 获取多个实体modle
/// </summary>
/// <param name="sqlString">查询语句</param>
/// <returns>List<T></returns>
public static List<T> QueryModelList<T>(string sqlString) where T : new()
{
List<T> tlist = new List<T>();
using (MySqlConnection connection = new MySqlConnection(DbHelperMySQL.connectionString))
{
try
{
connection.Open();
tlist = connection.Query<T>(sqlString).ToList();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
//throw new Exception(ex.Message);
connection.Close();
}
return tlist;
}
}
/// <summary>
/// 参数化获取多个实体modle
/// </summary>
/// <param name="sqlString">查询语句</param>
/// <returns>List<T></returns>
public static List<T> QueryModelList<T>(string sqlString, params MySqlParameter[] parms) where T : new()
{
List<T> tlist = new List<T>();
using (MySqlConnection connection = new MySqlConnection(DbHelperMySQL.connectionString))
{
try
{
connection.Open();
var args = new DynamicParameters(new { });
foreach (MySqlParameter parm in parms)
{
args.Add(parm.ParameterName, parm.Value);
}
tlist = connection.Query<T>(sqlString, args).ToList();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
//throw new Exception(ex.Message);
connection.Close();
}
return tlist;
}
}
#endregion
#region Execute 操作
/// <summary>
/// 单条执行一条sql数据
/// </summary>
/// <param name="sqlstr">执行语句</param>
/// <returns></returns>
public static int Execute(string sqlstr)
{
int obj = 0;
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
try
{
connection.Open();
obj = connection.Execute(sqlstr);
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
//throw new Exception(ex.Message);
connection.Close();
}
}
return obj;
}
/// <summary>
/// 参数化单条执行一条sql数据
/// </summary>
/// <param name="sqlstr">执行语句</param>
/// <returns></returns>
public static int Execute(string sqlstr, params MySqlParameter[] parms)
{
int obj = 0;
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
try
{
connection.Open();
var args = new DynamicParameters(new { });
foreach (MySqlParameter parm in parms)
{
args.Add(parm.ParameterName, parm.Value);
}
obj = connection.Execute(sqlstr, args);
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
//throw new Exception(ex.Message);
connection.Close();
}
}
return obj;
}
#endregion
#endregion