using
System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Remoting.Messaging;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace LTP.SQLServerDAL
... {
/**////<summary>
///ADO.NET数据库操作基础类。
///</summary>
publicabstractclassDbManagerSQL
...{
//数据库连接字符串
protectedstaticstringconnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
publicDbManagerSQL()
...{
//
//TODO:在此处添加构造函数逻辑
//
}
/**////<summary>
///执行SQL语句,返回影响的记录数
///</summary>
///<paramname="SQLString"></param>
///<returns></returns>
publicstaticintExecuteSql(stringSQLString)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
using(SqlCommandcmd=newSqlCommand(SQLString,connection))
...{
try
...{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptionE)
...{
thrownewException(E.Message);
}
}
}
}
/**////<summary>
///执行两条SQL语句,实现数据库事务。
///</summary>
///<paramname="SQLString1"></param>
///<paramname="SQLString2"></param>
publicstaticvoidExecuteSqlTran(stringSQLString1,stringSQLString2)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
connection.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=connection;
SqlTransactiontx=connection.BeginTransaction();
cmd.Transaction=tx;
try
...{
cmd.CommandText=SQLString1;
cmd.ExecuteNonQuery();
cmd.CommandText=SQLString2;
cmd.ExecuteNonQuery();
tx.Commit();
}
catch(System.Data.SqlClient.SqlExceptionE)
...{
tx.Rollback();
thrownewException(E.Message);
}
finally
...{
cmd.Dispose();
connection.Close();
}
}
}
/**////<summary>
///执行多条SQL语句,实现数据库事务,每条语句以“;”分割。
///</summary>
///<paramname="SQLStringList"></param>
publicstaticvoidExecuteSqlTran(stringSQLStringList)
...{
using(OdbcConnectionconn=newOdbcConnection(connectionString))
...{
conn.Open();
OdbcCommandcmd=newOdbcCommand();
cmd.Connection=conn;
OdbcTransactiontx=conn.BeginTransaction();
cmd.Transaction=tx;
try
...{
string[]split=SQLStringList.Split(newChar[]...{';'});
foreach(stringstrsqlinsplit)
...{
if(strsql.Trim()!="")
...{
cmd.CommandText=strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch(System.Data.Odbc.OdbcExceptionE)
...{
tx.Rollback();
thrownewException(E.Message);
}
}
}
/**////<summary>
///执行带一个存储过程参数的的SQL语句。
///</summary>
///<paramname="SQLString"></param>
///<paramname="content"></param>
///<returns></returns>
publicstaticintExecuteSql(stringSQLString,stringcontent)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
SqlCommandcmd=newSqlCommand(SQLString,connection);
System.Data.SqlClient.SqlParametermyParameter=newSystem.Data.SqlClient.SqlParameter("@content",SqlDbType.NText);
myParameter.Value=content;
cmd.Parameters.Add(myParameter);
try
...{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptionE)
...{
thrownewException(E.Message);
}
finally
...{
cmd.Dispose();
connection.Close();
}
}
}
/**////<summary>
///向数据库里插入图像格式的字段
///</summary>
///<paramname="strSQL"></param>
///<paramname="fs"></param>
///<returns></returns>
publicstaticintExecuteSqlInsertImg(stringstrSQL,byte[]fs)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
SqlCommandcmd=newSqlCommand(strSQL,connection);
System.Data.SqlClient.SqlParametermyParameter=newSystem.Data.SqlClient.SqlParameter("@fs",SqlDbType.Image);
myParameter.Value=fs;
cmd.Parameters.Add(myParameter);
try
...{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptionE)
...{
thrownewException(E.Message);
}
finally
...{
cmd.Dispose();
connection.Close();
}
}
}
/**////<summary>
///执行一条计算查询结果语句,返回查询结果(整数)。
///</summary>
///<paramname="strSQL"></param>
///<returns></returns>
publicstaticintGetCount(stringstrSQL)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
SqlCommandcmd=newSqlCommand(strSQL,connection);
try
...{
connection.Open();
SqlDataReaderresult=cmd.ExecuteReader();
inti=0;
while(result.Read())
...{
i=result.GetInt32(0);
}
result.Close();
returni;
}
catch(System.Data.SqlClient.SqlExceptione)
...{
thrownewException(e.Message);
}
finally
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Remoting.Messaging;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace LTP.SQLServerDAL
... {
/**////<summary>
///ADO.NET数据库操作基础类。
///</summary>
publicabstractclassDbManagerSQL
...{
//数据库连接字符串
protectedstaticstringconnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
publicDbManagerSQL()
...{
//
//TODO:在此处添加构造函数逻辑
//
}
/**////<summary>
///执行SQL语句,返回影响的记录数
///</summary>
///<paramname="SQLString"></param>
///<returns></returns>
publicstaticintExecuteSql(stringSQLString)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
using(SqlCommandcmd=newSqlCommand(SQLString,connection))
...{
try
...{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptionE)
...{
thrownewException(E.Message);
}
}
}
}
/**////<summary>
///执行两条SQL语句,实现数据库事务。
///</summary>
///<paramname="SQLString1"></param>
///<paramname="SQLString2"></param>
publicstaticvoidExecuteSqlTran(stringSQLString1,stringSQLString2)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
connection.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=connection;
SqlTransactiontx=connection.BeginTransaction();
cmd.Transaction=tx;
try
...{
cmd.CommandText=SQLString1;
cmd.ExecuteNonQuery();
cmd.CommandText=SQLString2;
cmd.ExecuteNonQuery();
tx.Commit();
}
catch(System.Data.SqlClient.SqlExceptionE)
...{
tx.Rollback();
thrownewException(E.Message);
}
finally
...{
cmd.Dispose();
connection.Close();
}
}
}
/**////<summary>
///执行多条SQL语句,实现数据库事务,每条语句以“;”分割。
///</summary>
///<paramname="SQLStringList"></param>
publicstaticvoidExecuteSqlTran(stringSQLStringList)
...{
using(OdbcConnectionconn=newOdbcConnection(connectionString))
...{
conn.Open();
OdbcCommandcmd=newOdbcCommand();
cmd.Connection=conn;
OdbcTransactiontx=conn.BeginTransaction();
cmd.Transaction=tx;
try
...{
string[]split=SQLStringList.Split(newChar[]...{';'});
foreach(stringstrsqlinsplit)
...{
if(strsql.Trim()!="")
...{
cmd.CommandText=strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch(System.Data.Odbc.OdbcExceptionE)
...{
tx.Rollback();
thrownewException(E.Message);
}
}
}
/**////<summary>
///执行带一个存储过程参数的的SQL语句。
///</summary>
///<paramname="SQLString"></param>
///<paramname="content"></param>
///<returns></returns>
publicstaticintExecuteSql(stringSQLString,stringcontent)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
SqlCommandcmd=newSqlCommand(SQLString,connection);
System.Data.SqlClient.SqlParametermyParameter=newSystem.Data.SqlClient.SqlParameter("@content",SqlDbType.NText);
myParameter.Value=content;
cmd.Parameters.Add(myParameter);
try
...{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptionE)
...{
thrownewException(E.Message);
}
finally
...{
cmd.Dispose();
connection.Close();
}
}
}
/**////<summary>
///向数据库里插入图像格式的字段
///</summary>
///<paramname="strSQL"></param>
///<paramname="fs"></param>
///<returns></returns>
publicstaticintExecuteSqlInsertImg(stringstrSQL,byte[]fs)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
SqlCommandcmd=newSqlCommand(strSQL,connection);
System.Data.SqlClient.SqlParametermyParameter=newSystem.Data.SqlClient.SqlParameter("@fs",SqlDbType.Image);
myParameter.Value=fs;
cmd.Parameters.Add(myParameter);
try
...{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptionE)
...{
thrownewException(E.Message);
}
finally
...{
cmd.Dispose();
connection.Close();
}
}
}
/**////<summary>
///执行一条计算查询结果语句,返回查询结果(整数)。
///</summary>
///<paramname="strSQL"></param>
///<returns></returns>
publicstaticintGetCount(stringstrSQL)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
SqlCommandcmd=newSqlCommand(strSQL,connection);
try
...{
connection.Open();
SqlDataReaderresult=cmd.ExecuteReader();
inti=0;
while(result.Read())
...{
i=result.GetInt32(0);
}
result.Close();
returni;
}
catch(System.Data.SqlClient.SqlExceptione)
...{
thrownewException(e.Message);
}
finally