底层的增删查改

#region 助手类
public class Dbhelper
{

static string sqlConnectionString = @"Database=CMS;Server=42.159.145.218,1401;User ID =azureadmin; Password =P@ss1234567890;";

//新增
public object Insert(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(string sql, List 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(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(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(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 SearchData(string sql, object obj)
{
IDbConnection connection = new SqlConnection(sqlConnectionString);
return connection.Query(sql, obj).ToList();
}

 

}
#endregion

你可能感兴趣的:(底层的增删查改)