底层的增删查改
Posted ning-xiaowo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了底层的增删查改相关的知识,希望对你有一定的参考价值。
#region 助手类
public class Dbhelper
{
static string sqlConnectionString = @"Database=CMS;Server=42.159.145.218,1401;User ID =azureadmin; Password =P@ss1234567890;";
//新增
public object Insert<T>(string sql, T t)
{
try
{
object result;
using (IDbConnection connection = new SqlConnection(sqlConnectionString))
{
result = connection.Execute(sql, t);
}
return result;
}
catch (Exception ex)
{
Console.Write(ex.Message);
return -999;
}
}
public object BatchInsert<T>(string sql, List<T> list)
{
try
{
object result;
using (IDbConnection connection = new SqlConnection(sqlConnectionString))
{
result = connection.Execute(sql, list);
}
return result;
}
catch (Exception ex)
{
Console.Write(ex.Message);
return -999;
}
}
//删除
public int Delete<T>(string sql, T t)
{
try
{
int result;
using (IDbConnection connection = new SqlConnection(sqlConnectionString))
{
result = connection.Execute(sql, t);
}
return result;
}
catch
{
return -999;
}
}
//查询
public object Search<T>(string sql, T t)
{
try
{
object result;
using (IDbConnection connection = new SqlConnection(sqlConnectionString))
{
result = connection.Execute(sql, t);
}
return result;
}
catch (Exception ex)
{
return ex.Message;
}
}
//修改
public int Update<T>(string sql, T t)
{
try
{
int result;
using (IDbConnection connection = new SqlConnection(sqlConnectionString))
{
result = connection.Execute(sql, t);
}
return result;
}
catch
{
return -999;
}
}
//查询
public List<T> SearchData<T>(string sql, object obj)
{
IDbConnection connection = new SqlConnection(sqlConnectionString);
return connection.Query<T>(sql, obj).ToList();
}
}
#endregion
以上是关于底层的增删查改的主要内容,如果未能解决你的问题,请参考以下文章