您好,欢迎访问一九零五行业门户网

C# SqlServer的Helper基类

using system; using system.collections.generic; using system.linq; using system.text; using system.collections; using system.data; using system.data.sqlclient; using system.data.common; namespace sqlserverdal { public class sqlhelper { sta
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.collections;
using system.data;
using system.data.sqlclient;
using system.data.common;
namespace sqlserverdal
{
public class sqlhelper
{
static string _sqlconnectionstring;
public static string sqlconnectionstring
{
get
{
return _sqlconnectionstring;
}
set
{
_sqlconnectionstring = value;
}
}
///
/// 设置参数内容
///
/// 名称
/// 数据类型
/// 长度大小
/// 值
/// 类型
///
public static sqlparameter setdataparameter(string paramname, sqldbtype dbtype, int32 size, object paramevalue, parameterdirection direction)
{
sqlparameter param = new sqlparameter();
param.sqldbtype = dbtype;
param.parametername = paramname;
if (size > 0)
{
param.size = size;
}
if (paramevalue.tostring() != && paramevalue != null && direction != parameterdirection.output)
{
param.value = paramevalue;
}
param.direction = direction;
return param;
}
///
/// 设置参数内容
///
/// 名称
/// 数据类型
/// 类型
///
public static sqlparameter setdataparameter(string paramname, sqldbtype dbtype, parameterdirection direction)
{
sqlparameter param = new sqlparameter();
param.sqldbtype = dbtype;
param.parametername = paramname;
param.direction = direction;
return param;
}
#region 私有办法
///
/// 将sqlparameter参数数组(参数值)分派给dbcommand号令.
/// 这个办法将给任何一个参数分派dbnull.value;
/// 该操纵将阻拦默认值的应用.
///
/// 号令名
/// sqlparameters数组
private static void attachparameters(sqlcommand command, sqlparameter[] commandparameters)
{
if (command == null) throw new argumentnullexception(command);
if (commandparameters != null)
{
foreach (sqlparameter p in commandparameters)
{
if (p != null)
{
// 搜检未分派值的输出参数,将其分派以dbnull.value.
if ((p.direction == parameterdirection.inputoutput || p.direction == parameterdirection.input) &&
(p.value == null))
{
p.value = dbnull.value;
}
command.parameters.add(p);
}
}
}
//应用完成后清除原所有参数值
// clearidataparameter();
}
///
/// 预处理惩罚用户供给的号令,数据库连接/事务/号令类型/参数
///
/// 一个有效的事务或者是null值
/// 号令类型 (存储过程,号令文本, 其它.)
/// 存储过程名或都sql号令文本
/// 和号令相接洽关系的sqlparameter参数数组,若是没有参数为null
/// true 若是连接是打开的,则为true,其它景象下为false.
private static void preparecommand(sqlconnection connection, sqlcommand command, sqltransaction transaction, commandtype commandtype, string commandtext, sqlparameter[] commandparameters, out bool mustcloseconnection)
{
try
{
if (command == null) throw new argumentnullexception(command);
if (commandtext == null || commandtext.length == 0) throw new argumentnullexception(commandtext);
if (connection.state != connectionstate.open)
{
mustcloseconnection = true;
connection.open();
}
else
{
mustcloseconnection = false;
}
// 给号令分派一个数据库连接.
command.connection = connection;
// 设置号令文本(存储过程名或sql语句)
command.commandtext = commandtext;
// 分派事务
if (transaction != null)
{
if (transaction.connection == null) throw new argumentexception(the transaction was rollbacked or commited, please provide an open transaction., transaction);
command.transaction = transaction;
}
// 设置号令类型.
command.commandtype = commandtype;
// 分派号令参数
if (commandparameters != null)
{
//sqlparameter[] dpitem =
attachparameters(command, commandparameters);
}
}
catch
{
mustcloseconnection = false;
}
}
#endregion
#region utedataset 数据表
///
/// 履行指定命据库连接字符串的号令,返回dataset.
///
/// 存储过程名称或sql语句
/// 返回一个包含成果集的dataset
public static dataset utedataset(string commandtext)
{
return utedataset((sqltransaction)null, commandtext, commandtype.text, (sqlparameter[])null);
}
///
/// 履行指定命据库连接字符串的号令,返回dataset.
///
/// 存储过程名称或sql语句
/// 号令类型 (存储过程,号令文本或其它)
/// 参数
/// 返回一个包含成果集的dataset
public static dataset utedataset(sqltransaction transaction, string commandtext, commandtype commandtype, params sqlparameter[] commandparameters)
{
if (sqlconnectionstring == null || sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
using (sqlconnection connection = new sqlconnection(sqlconnectionstring))
{
connection.open();
bool mustcloseconnection = false;
sqlcommand command = new sqlcommand();
preparecommand(connection, command, transaction, commandtype, commandtext, commandparameters, out mustcloseconnection);
try
{
sqldataadapter sdap = new sqldataadapter();
sdap.selectcommand = command;
dataset ds = new dataset();
sdap.fill(ds);
command.parameters.clear();//清空
command.dispose();
if (mustcloseconnection)
connection.close();
return ds;
}
catch (exception ex)
{
command.parameters.clear();//清空
command.dispose();
if (mustcloseconnection)
connection.close();
return new dataset();
}
}
}
///
/// 履行指定命据库连接字符串的号令,返回dataset.
///
/// 存储过程名称或sql语句
/// 号令类型 (存储过程,号令文本或其它)
/// 返回一个包含成果集的dataset
public static dataset utedataset(string commandtext, commandtype commandtytpe)
{
return utedataset((sqltransaction)null, commandtext, commandtytpe, (sqlparameter[])null);
}
///
/// 履行指定命据库连接字符串的号令,返回dataset.
///
/// 输出输出参数成果凑集,例:{name,value}
/// 存储过程名称或sql语句
/// 号令类型 (存储过程,号令文本或其它)
/// 参数
/// 返回一个包含成果集的dataset
public static dataset utedataset(ref hashtable outparameters, string commandtext, commandtype commandtype, params sqlparameter[] commandparameters)
{
if (sqlconnectionstring == null || sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
using (sqlconnection connection = new sqlconnection(sqlconnectionstring))
{
connection.open();
bool mustcloseconnection = false;
sqlcommand command = new sqlcommand();
preparecommand(connection, command, (sqltransaction)null, commandtype, commandtext, commandparameters, out mustcloseconnection);
try
{
sqldataadapter sdap = new sqldataadapter();
sdap.selectcommand = command;
dataset ds = new dataset();
sdap.fill(ds);
if (outparameters != null)
{
for (int i = 0; i {
if (command.parameters[i].direction == parameterdirection.output)
{
if (!outparameters.contains(command.parameters[i].parametername))
{
outparameters.add(command.parameters[i].parametername, command.parameters[i].value.tostring());
}
}
}
}
command.parameters.clear();//清空
command.dispose();
if (mustcloseconnection)
connection.close();
return ds;
}
catch (exception ex)
{
command.parameters.clear();//清空
command.dispose();
if (mustcloseconnection)
connection.close();
return new dataset();
}
}
}
///
/// 履行指定命据库连接字符串的号令,返回dataset.
///
/// 输出输出参数成果凑集,例:{name,value}
/// 存储过程
/// 参数
/// 返回一个包含成果集的dataset
public static dataset utedataset(ref hashtable outparameters, string sqlspro, params sqlparameter[] commandparameters)
{
return utedataset(ref outparameters, sqlspro, commandtype.storedprocedure, commandparameters);
}
///
/// 履行指定命据库连接字符串的号令,返回dataset.
///
/// 输出输出参数成果凑集,例:{name,value}
/// 存储过程
/// 参数
/// 返回一个包含成果集的dataset
public static dataset utedataset(string sqlspro, params sqlparameter[] commandparameters)
{
hashtable outparameters = null;
return utedataset(ref outparameters, sqlspro, commandtype.storedprocedure, commandparameters);
}
#endregion
#region utescalar 返回成果集中的第一行第一列
///
/// 返回一条数据
///
///
///
///
///
///
public static object utescalar(sqltransaction transaction, commandtype commandtype, string commandtext, params sqlparameter[] commandparameters)
{
if (sqlconnectionstring == null || sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
using (sqlconnection connection = new sqlconnection(sqlconnectionstring))
{
connection.open();
bool mustcloseconnection = false;
sqlcommand command = new sqlcommand();
try
{
preparecommand(connection, command, transaction, commandtype, commandtext, commandparameters, out mustcloseconnection);
object rvalue = command.utescalar();
command.parameters.clear();//清空
command.dispose();
if (mustcloseconnection)
connection.close();
return rvalue;
}
catch (exception ex)
{
command.parameters.clear();//清空
if (mustcloseconnection)
connection.close();
return null;
}
}
}
///
/// 返回一条数据
///
///
///
///
public static object utescalar(commandtype commandtype, string commandtext)
{
return utescalar((sqltransaction)null, commandtype, commandtext, (sqlparameter[])null);
}
///
/// 返回一条数据
///
///
///
///
public static object utescalar(commandtype commandtype, string commandtext, params sqlparameter[] commandparameters)
{
return utescalar((sqltransaction)null, commandtype, commandtext, commandparameters);
}
///
/// 返回一条数据
///
///
///
///
///
public static object utescalar(commandtype commandtype, string commandtext, sqltransaction transaction)
{
return utescalar(transaction, commandtype, commandtext, (sqlparameter[])null);
}
///
/// 返回一第数据
///
///
///
public static object utescalar(string commandtext)
{
return utescalar(commandtype.text, commandtext, (sqltransaction)null);
}
#endregion
#region utedatareader 数据浏览器
public static dbdatareader utedatareader(sqlconnection connection, sqltransaction transaction, commandtype commandtype, string commandtext, params sqlparameter[] commandparameters)
{
if (connection == null) throw new argumentnullexception(connection);
dbdatareader reader = null;
bool mustcloseconnection = false;
sqlcommand command = new sqlcommand();
try
{
preparecommand(connection, command, transaction, commandtype, commandtext, commandparameters, out mustcloseconnection);
reader = command.utereader(system.data.commandbehavior.closeconnection);
//reader = command.utereader(system.data.commandbehavior.closeconnection);
command.parameters.clear();//清空
//command.dispose();
}
catch (exception ex)
{
}
return reader;
}
public static dbdatareader utedatareader(sqltransaction transaction, commandtype commandtype, string commandtext)
{
if (sqlconnectionstring == null || sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
sqlconnection connection = null;
try
{
connection = new sqlconnection(sqlconnectionstring);
connection.open();
return utedatareader(connection, transaction, commandtype, commandtext, (sqlparameter[])null);
}
catch
{
// if we fail to return the sqldatreader, we need to close the connection ourselves
if (connection != null) connection.close();
throw;
}
}
public static dbdatareader utedatareader(commandtype commandtype, string commandtext, params sqlparameter[] commandparameters)
{
if (sqlconnectionstring == null || sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
sqlconnection connection = null;
try
{
connection = new sqlconnection(sqlconnectionstring);
connection.open();
return utedatareader(connection, (sqltransaction)null, commandtype, commandtext, commandparameters);
}
catch
{
// if we fail to return the sqldatreader, we need to close the connection ourselves
if (connection != null) connection.close();
throw;
}
}
public static dbdatareader utedatareader(commandtype commandtype, string commandtext)
{
return utedatareader(commandtype, commandtext, (sqlparameter[])null);
}
public static dbdatareader utedatareader(string commandtext)
{
return utedatareader(commandtype.text, commandtext);
}
public static dbdatareader utedatareader(sqlconnection connection, out list outparameters, string commandtext, commandtype commandtype, params sqlparameter[] commandparameters)
{
dbdatareader reader = null;
outparameters = new list();
try
{
bool mustcloseconnection = false;
sqlcommand command = new sqlcommand();
preparecommand(connection, command, (sqltransaction)null, commandtype, commandtext, commandparameters, out mustcloseconnection);
reader = command.utereader(system.data.commandbehavior.closeconnection);// (commandbehavior.closeconnection);
command.parameters.clear();//清空
command.dispose();
}
catch (exception ex)
{
}
return reader;
}
public static dbdatareader utedatareader(out list outparameters, string sqlspro, params sqlparameter[] commandparameters)
{
if (sqlconnectionstring == null || sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
sqlconnection connection = null;
try
{
connection = new sqlconnection(sqlconnectionstring);
connection.open();
return utedatareader(connection, out outparameters, sqlspro, commandtype.storedprocedure, commandparameters);
}
catch
{
// if we fail to return the sqldatreader, we need to close the connection ourselves
if (connection != null) connection.close();
throw;
}
}
#endregion
#region utedatarow 返回成果集中第一行
///
/// 履行指定命据库连接字符串的号令,返回dataset第一行.
///
/// 存储过程名称或sql语句
/// 号令类型 (存储过程,号令文本或其它)
/// 参数
/// 返回一个包含成果集的dataset中的第一行
public static datarow utedatarow(sqltransaction transaction, string commandtext, commandtype commandtype, params sqlparameter[] commandparameters)
{
try
{
datarow row = null;
dataset ds = utedataset(transaction, commandtext, commandtype, commandparameters);
if (ds.tables[0].rows.count > 0)
{
row = (datarow)ds.tables[0].rows[0];
}
ds.dispose();
return row;
}
catch
{
return null;
}
}
public static datarow utedatarow(string commandtext, commandtype commandtype)
{
return utedatarow((sqltransaction)null, commandtext, commandtype, (sqlparameter[])null);
}
public static datarow utedatarow(string commandtext, commandtype commandtype, params sqlparameter[] commandparameters)
{
return utedatarow((sqltransaction)null, commandtext, commandtype, commandparameters);
}
public static datarow utedatarow(string commandtext)
{
return utedatarow((sqltransaction)null, commandtext,香港服务器, commandtype.text, (sqlparameter[])null);
}
#endregion
#region utenonquery办法
public static int utenonquery(ref hashtable output, string commandtext, params sqlparameter[] commandparameters)
{
if (_sqlconnectionstring == null || _sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
using (sqlconnection connection = new sqlconnection(_sqlconnectionstring))
{
connection.open();
// 创建dbcommand号令,并进行预处理惩罚
bool mustcloseconnection = false;
sqlcommand command = new sqlcommand();
preparecommand(connection, command, (sqltransaction)null, commandtype.storedprocedure, commandtext, commandparameters, out mustcloseconnection);
// 履行号令
int retval = command.utenonquery();
for (int i = 0; i {
if (command.parameters[i].direction == parameterdirection.output)
{
if (!output.contains(command.parameters[i].parametername.tostring()))
{
output.add(command.parameters[i].parametername.tostring(), command.parameters[i].value.tostring());
}
}
}
// 清除参数,以便再次应用.
command.parameters.clear();
command.dispose();
return retval;
}
}
///
/// datatable批量添加到数据库
///
/// 要写入的表名
/// datatable表
public static void copyutnonquery(string tablename, datatable dt)
{
if (sqlconnectionstring == null || sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
using (sqlconnection connection = new sqlconnection(sqlconnectionstring))
{
connection.open();
using (sqltransaction trans = connection.begintransaction())
{
using (sqlbulkcopy sqlbc = new sqlbulkcopy(connection, sqlbulkcopyoptions.firetriggers, trans))
{
try
{
//一次批量的插入的数据量
sqlbc.batchsize = dt.rows.count;
//超时之前操纵完成所容许的秒数,若是超时则事务不会提交 ,数据将回滚,所有已复制的行都邑从目标表中移除
sqlbc.bulkcopytimeout = 360;
//設定 notifyafter 属性,以便在每插入10000 条数据时,呼唤响应事务。
// sqlbc.notifyafter = 1000;
// sqlbc.sqlrowscopied += new sqlrowscopiedeventhandler(onsqlrowscopied);
//设置要批量写入的表
sqlbc.destinationtablename = tablename;
//自定义的datatable和数据库的字段进行对应
//sqlbc.columnmappings.add(id, tel);
//sqlbc.columnmappings.add(name, neirong);
//for (int i = 0; i //{
// sqlbc.columnmappings.add(dtcolum[i].columnname.tostring(), dtcolum[i].columnname.tostring());
//}
//批量写入
sqlbc.writetoserver(dt);
trans.commit();
}
catch
{
trans.rollback();
}
}
}
}
}
///
/// 履行指定命据库连接对象的号令
///
/// 号令类型(存储过程,号令文本或其它.)
/// t存储过程名称或sql语句
/// sqlparamter参数数组
/// 返回影响的行数
public static int utenonquery(commandtype commandtype, string commandtext, params sqlparameter[] commandparameters)
{
if (sqlconnectionstring == null || sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
using (sqlconnection connection = new sqlconnection(sqlconnectionstring))
{
connection.open();
// 创建dbcommand号令,并进行预处理惩罚
bool mustcloseconnection = false;
sqlcommand command = new sqlcommand();
preparecommand(connection, command, (sqltransaction)null, commandtype, commandtext, commandparameters, out mustcloseconnection);
// 履行号令
int retval = command.utenonquery();
// 清除参数,以便再次应用.
command.parameters.clear();
command.dispose();
return retval;
}
}
public static int utenonquery(sqltransaction transaction, commandtype commandtype, string commandtext, params sqlparameter[] commandparameters)
{
bool mustcloseconnection = false;
sqlcommand command = new sqlcommand(); ;
preparecommand(transaction.connection, command, transaction, commandtype, commandtext, commandparameters, out mustcloseconnection);
try
{
// 履行号令
int retval = command.utenonquery();
// 清除参数,以便再次应用.
command.parameters.clear();
if (transaction != null)
{
transaction.commit();
}
command.dispose();
if (mustcloseconnection)
{
transaction.connection.close();
transaction.connection.dispose();
}
return retval;
}
catch (exception ex)
{
if (transaction != null)
{
transaction.rollback();
}
command.dispose();
return 0;
}
}
public static int utenonquery(out int scope_identity, sqltransaction transaction, commandtype commandtype, string commandtext, params sqlparameter[] commandparameters)
{
scope_identity = 0;
bool mustcloseconnection = false;
sqlcommand command = new sqlcommand();
preparecommand(transaction.connection, command, transaction, commandtype, commandtext, commandparameters, out mustcloseconnection);
try
{
// 履行号令
int retval = command.utenonquery();
// 清除参数,以便再次应用.
command.parameters.clear();
command.commandtype = commandtype.text;
command.commandtext = select scope_identity();
scope_identity = int.parse(command.utescalar().tostring());
command.dispose();
if (mustcloseconnection)
{
transaction.connection.close();
transaction.connection.dispose();
}
return retval;
}
catch (exception ex)
{
command.dispose();
transaction.connection.close();
transaction.connection.dispose();
return 0;
}
}
///
/// 履行指定命据库连接对象的号令,并输出最后履行的成果编号
///
/// 输出最后履行成果
/// 号令类型(存储过程,号令文本或其它.)
/// t存储过程名称或sql语句
/// sqlparamter参数数组
/// 返回影响的行数
public static int utenonquery(out int scope_identity, commandtype commandtype, string commandtext, params sqlparameter[] commandparameters)
{
scope_identity = 0;
if (sqlconnectionstring == null || sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
using (sqlconnection connection = new sqlconnection(sqlconnectionstring))
{
connection.open();
// 创建dbcommand号令,并进行预处理惩罚
bool mustcloseconnection = false;
sqlcommand command = new sqlcommand();
preparecommand(connection, command, (sqltransaction)null, commandtype, commandtext, commandparameters, out mustcloseconnection);
try
{
// 履行号令
int retval = command.utenonquery();
// 清除参数,以便再次应用.
command.parameters.clear();
command.commandtype = commandtype.text;
command.commandtext = select scope_identity();
scope_identity = int.parse(command.utescalar().tostring());
command.dispose();
return retval;
}
catch (exception ex)
{
command.dispose();
return 0;
}
}
}
///
/// 履行指定命据库连接对象的号令
///
/// 一个有效的数据库连接对象
/// 号令类型(存储过程,号令文本或其它.)
/// 存储过程名称或sql语句
/// 返回影响的行数
public static int utenonquery(commandtype commandtype, string commandtext)
{
if (commandtext == null || commandtext.length == 0) throw new argumentnullexception(commandtext);
return utenonquery(commandtype, commandtext, (sqlparameter[])null);
}
///
/// 履行指定命据库连接对象的号令,将对象数组的值赋给存储过程参数.
///
/// 一个有效的数据库连接对象
/// 存储过程名
/// 分派给存储过程输入参数的对象数组
/// 返回影响的行数
public static int utenonquery(string spname,香港空间, params sqlparameter[] commandparameters)
{
if (spname == null || spname.length == 0) throw new argumentnullexception(spname);
// 若是有参数值
if ((commandparameters != null) && (commandparameters.length > 0))
{
return utenonquery(commandtype.storedprocedure, spname, commandparameters);
}
else
{
return utenonquery(commandtype.storedprocedure, spname);
}
}
///
/// 履行带事务的sql语句
///
///
///
public static int utenonquery(string commandtext)
{
if (commandtext == null || commandtext.length == 0) throw new argumentnullexception(commandtext);
//sqltransaction transaction = bbdataprovider.transaction;
if (sqlconnectionstring == null || sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
using (sqlconnection connection = new sqlconnection(sqlconnectionstring))
{
connection.open();
sqltransaction transaction = connection.begintransaction();
return utenonquery(transaction, commandtype.text, commandtext, (sqlparameter[])null);
}
}
///
/// 履行存储过程,返回output成果
///
/// 存储过程名
/// 号令类型(存储过程)
/// sqlparamter参数数组
/// output成果
public static list utenonquery(string commandtext, commandtype commandtype, params sqlparameter[] commandparameters)
{
if (sqlconnectionstring == null || sqlconnectionstring.length == 0) throw new argumentnullexception(connectionstring);
using (sqlconnection connection = new sqlconnection(sqlconnectionstring))
{
connection.open();
bool mustcloseconnection = false;
sqlcommand command = new sqlcommand();
preparecommand(connection, command, (sqltransaction)null, commandtype.storedprocedure, commandtext, commandparameters, out mustcloseconnection);
try
{
command.utenonquery();
}
catch { }
list outparameters = new list();
for (int i = 0; i {
if (command.parameters[i].direction == parameterdirection.output)
{
string[] parameteritem = { command.parameters[i].parametername.tostring(), command.parameters[i].value.tostring() };
outparameters.add(parameteritem);
}
}
command.parameters.clear();//清空
command.dispose();
if (mustcloseconnection)
connection.close();
return outparameters;
}
}
#endregion
}
},美国服务器
其它类似信息

推荐信息