欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# DBHelper类 参考

程序员文章站 2022-11-11 13:20:20
using System;using System.Collections.Generic;using System.Text;using System.Configuration;using System.Data;using System.Data.SqlClient;namespace DAL... ......

C# DBHelper类 参考

using system;
using system.collections.generic;
using system.text;
using system.configuration;
using system.data;
using system.data.sqlclient;

namespace dal
{
public class sqlhelper
{

//连接字符串
static string strconn = configurationmanager.connectionstrings["cnnhorechargeplatformconnectionstring"].tostring();



#region 执行查询,返回datatable对象-----------------------



public static datatable gettable(string strsql)
{
return gettable(strsql, null);
}
public static datatable gettable(string strsql, sqlparameter[] pas)
{
return gettable(strsql, pas, commandtype.text);
}
/// <summary>
/// 执行查询,返回datatable对象
/// </summary>
/// <param name="strsql">sql语句</param>
/// <param name="pas">参数数组</param>
/// <param name="cmdtype">command类型</param>
/// <returns>datatable对象</returns>
public static datatable gettable(string strsql, sqlparameter[] pas, commandtype cmdtype)
{
datatable dt = new datatable(); ;
using (sqlconnection conn = new sqlconnection(strconn))
{
sqldataadapter da = new sqldataadapter(strsql, conn);
da.selectcommand.commandtype = cmdtype;
if (pas != null)
{
da.selectcommand.parameters.addrange(pas);
}
da.fill(dt);
}
return dt;
}



#endregion




#region 执行查询,返回dataset对象-------------------------




public static dataset getdataset(string strsql)
{
return getdataset(strsql,null);
}

public static dataset getdataset(string strsql, sqlparameter[] pas)
{
return getdataset(strsql,pas,commandtype.text);
}
/// <summary>
/// 执行查询,返回dataset对象
/// </summary>
/// <param name="strsql">sql语句</param>
/// <param name="pas">参数数组</param>
/// <param name="cmdtype">command类型</param>
/// <returns>dataset对象</returns>
public static dataset getdataset(string strsql, sqlparameter[] pas, commandtype cmdtype)
{
dataset dt = new dataset(); ;
using (sqlconnection conn = new sqlconnection(strconn))
{
sqldataadapter da = new sqldataadapter(strsql, conn);
da.selectcommand.commandtype = cmdtype;
if (pas != null)
{
da.selectcommand.parameters.addrange(pas);
}
da.fill(dt);
}
return dt;
}
#endregion





#region 执行非查询存储过程和sql语句-----------------------------




public static int excuteproc(string procname)
{
return excutesql(procname, null, commandtype.storedprocedure);
}

public static int excuteproc(string procname, sqlparameter[] pars)
{
return excutesql(procname, pars, commandtype.storedprocedure);
}

public static int excutesql(string strsql)
{
return excutesql(strsql, null);
}

public static int excutesql(string strsql, sqlparameter[] paras)
{
return excutesql(strsql, paras, commandtype.text);
}

/// 执行非查询存储过程和sql语句
/// 增、删、改
/// </summary>
/// <param name="strsql">要执行的sql语句</param>
/// <param name="paras">参数列表,没有参数填入null</param>
/// <param name="cmdtype">command类型</param>
/// <returns>返回影响行数</returns>
public static int excutesql(string strsql, sqlparameter[] paras, commandtype cmdtype)
{
int i = 0;
using (sqlconnection conn = new sqlconnection(strconn))
{
sqlcommand cmd = new sqlcommand(strsql, conn);
cmd.commandtype = cmdtype;
if (paras != null)
{
cmd.parameters.addrange(paras);
}
conn.open();
i = cmd.executenonquery();
conn.close();
}
return i;

}


#endregion








#region 执行查询返回第一行,第一列---------------------------------




public static int excutescalarsql(string strsql)
{
return excutescalarsql(strsql, null);
}

public static int excutescalarsql(string strsql, sqlparameter[] paras)
{
return excutescalarsql(strsql, paras, commandtype.text);
}
public static int excutescalarproc(string strsql, sqlparameter[] paras)
{
return excutescalarsql(strsql, paras, commandtype.storedprocedure);
}
/// <summary>
/// 执行sql语句,返回第一行,第一列
/// </summary>
/// <param name="strsql">要执行的sql语句</param>
/// <param name="paras">参数列表,没有参数填入null</param>
/// <returns>返回影响行数</returns>
public static int excutescalarsql(string strsql, sqlparameter[] paras, commandtype cmdtype)
{
int i = 0;
using (sqlconnection conn = new sqlconnection(strconn))
{
sqlcommand cmd = new sqlcommand(strsql, conn);
cmd.commandtype = cmdtype;
if (paras != null)
{
cmd.parameters.addrange(paras);
}
conn.open();
i =convert.toint32( cmd.executescalar());
conn.close();
}
return i;

}


#endregion









#region 查询获取单个值------------------------------------




/// <summary>
/// 调用不带参数的存储过程获取单个值
/// </summary>
/// <param name="procname"></param>
/// <returns></returns>
public static object getobjectbyproc(string procname)
{
return getobjectbyproc(procname, null);
}
/// <summary>
/// 调用带参数的存储过程获取单个值
/// </summary>
/// <param name="procname"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static object getobjectbyproc(string procname, sqlparameter[] paras)
{
return getobject(procname, paras, commandtype.storedprocedure);
}
/// <summary>
/// 根据sql语句获取单个值
/// </summary>
/// <param name="strsql"></param>
/// <returns></returns>
public static object getobject(string strsql)
{
return getobject(strsql,null);
}
/// <summary>
/// 根据sql语句 和 参数数组获取单个值
/// </summary>
/// <param name="strsql"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static object getobject(string strsql, sqlparameter[] paras)
{
return getobject(strsql, paras, commandtype.text);
}

/// <summary>
/// 执行sql语句,返回首行首列
/// </summary>
/// <param name="strsql">要执行的sql语句</param>
/// <param name="paras">参数列表,没有参数填入null</param>
/// <returns>返回的首行首列</returns>
public static object getobject(string strsql, sqlparameter[] paras,commandtype cmdtype)
{
object o = null;
using (sqlconnection conn = new sqlconnection(strconn))
{
sqlcommand cmd = new sqlcommand(strsql, conn);
cmd.commandtype = cmdtype;
if (paras != null)
{
cmd.parameters.addrange(paras);

}

conn.open();
o = cmd.executescalar();
conn.close();
}
return o;

}



#endregion





#region 查询获取datareader------------------------------------




/// <summary>
/// 调用不带参数的存储过程,返回datareader对象
/// </summary>
/// <param name="procname">存储过程名称</param>
/// <returns>datareader对象</returns>
public static sqldatareader getreaderbyproc(string procname)
{
return getreaderbyproc(procname, null);
}
/// <summary>
/// 调用带有参数的存储过程,返回datareader对象
/// </summary>
/// <param name="procname">存储过程名</param>
/// <param name="paras">参数数组</param>
/// <returns>datareader对象</returns>
public static sqldatareader getreaderbyproc(string procname, sqlparameter[] paras)
{
return getreader(procname, paras, commandtype.storedprocedure);
}
/// <summary>
/// 根据sql语句返回datareader对象
/// </summary>
/// <param name="strsql">sql语句</param>
/// <returns>datareader对象</returns>
public static sqldatareader getreader(string strsql)
{
return getreader(strsql, null);
}
/// <summary>
/// 根据sql语句和参数返回datareader对象
/// </summary>
/// <param name="strsql">sql语句</param>
/// <param name="paras">参数数组</param>
/// <returns>datareader对象</returns>
public static sqldatareader getreader(string strsql, sqlparameter[] paras)
{
return getreader(strsql, paras, commandtype.text);
}
/// <summary>
/// 查询sql语句获取datareader
/// </summary>
/// <param name="strsql">查询的sql语句</param>
/// <param name="paras">参数列表,没有参数填入null</param>
/// <returns>查询到的datareader(关闭该对象的时候,自动关闭连接)</returns>
public static sqldatareader getreader(string strsql, sqlparameter[] paras,commandtype cmdtype)
{
sqldatareader sqldr = null;
sqlconnection conn = new sqlconnection(strconn);
sqlcommand cmd = new sqlcommand(strsql, conn);
cmd.commandtype = cmdtype;
if (paras != null)
{
cmd.parameters.addrange(paras);
}
conn.open();
//commandbehavior.closeconnection的作用是如果关联的datareader对象关闭,则连接自动关闭
sqldr = cmd.executereader(commandbehavior.closeconnection);
return sqldr;
}



#endregion




#region 批量插入数据---------------------------------------------




/// <summary>
/// 往数据库中批量插入数据
/// </summary>
/// <param name="sourcedt">数据源表</param>
/// <param name="targettable">服务器上目标表</param>
public static void bulktodb(datatable sourcedt, string targettable)
{
sqlconnection conn = new sqlconnection(strconn);
sqlbulkcopy bulkcopy = new sqlbulkcopy(conn); //用其它源的数据有效批量加载sql server表中
bulkcopy.destinationtablename = targettable; //服务器上目标表的名称
bulkcopy.batchsize = sourcedt.rows.count; //每一批次中的行数

try
{
conn.open();
if (sourcedt != null && sourcedt.rows.count != 0)
bulkcopy.writetoserver(sourcedt); //将提供的数据源中的所有行复制到目标表中
}
catch (exception ex)
{
throw ex;
}
finally
{
conn.close();
if (bulkcopy != null)
bulkcopy.close();
}

}

#endregion


}
}
C# DBHelper类 参考