123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900 |
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Text;
- using System.Data;
- using System.Data.SqlClient;
- //using System.Timers;
- namespace SysBaseLibs
- {
- public delegate void evDBConnectionError(string pcCommand, string pcMessage, string pcErrorNumbers);
- public delegate void evDBConnectionAction(string pcCommand,string pcMessage);
- public class DBConnSql : IErrorMsg
- {
- // Fields
- private SqlDataAdapter _Adapter=null;
- private SqlConnection _Connection=null;
- private string _ConnectionString=null;
- protected DbConnStatus _Status=DbConnStatus.Error;
- private SqlTransaction _Transaction=null;
- // private Timer _timer = null;
- // Events
- public event evDBConnectionError OnDBConnectionError;
- public event evDBConnectionAction OnDBConnectionAction;
- // Methods
- public DBConnSql()
- {
- this._Adapter = new SqlDataAdapter();
- this._DataSet = null;
- this._Adapter.RowUpdated += new SqlRowUpdatedEventHandler(this._Adapter_RowUpdated);
- this._DataSet = new DataSet();
- this._ConnectionString = DALibs.CenterConnectionStr;
- }
- private void _Adapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
- {
- if (e.Errors != null)
- {
- this.FireDBConnectionError(" RowUpdated ", e.ToString(), e.Errors.Message);
- }
- }
- /// <summary>
- /// 打开数据库连接
- /// </summary>
- /// <returns></returns>
- public bool Open()
- {
- bool llRetVal = true;
- if (_ConnectionString=="")
- _ConnectionString = DALibs.CenterConnectionStr;
- try
- {
- _Connection = new SqlConnection(_ConnectionString);
- _Connection.Open();
- this._Status = DbConnStatus.Open;
- }
- catch (Exception e)
- {
- _ErrorMsg = "OpenConnection " + " Establishing Connection " + " \r\n " + e.Message;
- this.FireDBConnectionError("OpenConnection", "Establishing Connection", e.Message);
- llRetVal = false;
- }
- return llRetVal;
- }
- /// <summary>
- /// 打开数据库连接
- /// </summary>
- /// <param name="pcConnString"></param>
- /// <returns></returns>
- public bool Open(string pcConnString)
- {
- _ConnectionString = pcConnString;
- return Open();
- }
- /// <summary>
- /// 关闭数据库连接
- /// </summary>
- public void Close()
- {
- if (this._Connection != null)
- {
- this._Connection.Close();
- this._Connection.Dispose();
- this._Connection = null;
- this._Status = DbConnStatus.Close;
- }
- }
- /// <summary>
- /// 开始事务
- /// </summary>
- /// <returns></returns>
- public bool BeginTrans()
- {
- bool llRetVal = false;
- if (this._Connection != null && this._Transaction == null)
- {
- try
- {
- _Transaction = this._Connection.BeginTransaction();
- }
- catch (SqlException e)
- {
- _ErrorMsg = "BeginTrans " + " Start transaction: " + " \r\n " + e.Message;
- this.FireDBConnectionError("BeginTrans", "Start transaction: ", e.Message);
- }
- _ErrorMsg = "";
- llRetVal = true;
- }
- return llRetVal;
- }
- /// <summary>
- /// 确认事务
- /// </summary>
- /// <returns></returns>
- public bool CommitTrans()
- {
- if (_Transaction != null)
- {
- try
- {
- _Transaction.Commit();
- }
- catch (SqlException e)
- {
- _ErrorMsg = "CommitTrans: " + " Commit transaction: " + " \r\n " + e.Message;
- this.FireDBConnectionError("CommitTrans", "Commit transaction: ", e.Message);
- }
- _ErrorMsg = "";
- _Transaction.Dispose();
- _Transaction = null;
- }
- return true;
- }
- /// <summary>
- /// 回滚事务
- /// </summary>
- /// <returns></returns>
- public bool RollbackTrans()
- {
- if (this._Transaction != null)
- {
- try
- {
- _Transaction.Rollback();
- }
- catch (SqlException e)
- {
- this.FireDBConnectionError("RollbackTrans", "Rollback transaction: ", e.Message);
- }
-
- _Transaction.Dispose();
- _Transaction = null;
- }
- return true;
- }
- /// <summary>
- /// 在事务控制下,执行一个SQL语句
- /// </summary>
- /// <param name="pcCommand"></param>
- /// <returns></returns>
- public bool ExcuteSqlTran(string pcCommand)
- {
- bool lbRetVal = false;
- if (this.BeginTrans())
- {
- if (this.ExecuteSql(pcCommand))
- {
- lbRetVal = true;
- this.CommitTrans();
- }
- else
- this.RollbackTrans();
- }
- return lbRetVal;
- }
- /// <summary>
- /// 执行SQL语句
- /// </summary>
- /// <param name="pcCommand"></param>
- /// <returns></returns>
- public bool ExecuteSql(string pcCommand)
- {
- FireDBConnectionAction("ExecuteSql", pcCommand + " ");
- bool llRetVal = false;
- if (this._Connection != null)
- {
- try
- {
- SqlCommand loCommand = new SqlCommand(pcCommand, this._Connection,
- this._Transaction);
- loCommand.ExecuteNonQuery();
- llRetVal = true;
- }
- catch (Exception e)
- {
- _ErrorMsg ="ExecuteSql: "+ pcCommand+" \r\n "+ e.Message;
- this.FireDBConnectionError("ExecuteSql", pcCommand, e.Message);
- llRetVal = false;
- }
- }
- return llRetVal;
- }
- //@TableName varchar(500), --查询的数据表名
- //@SelectFieldNames varchar(2000)='*', --查询的字段名称(不能包含"SELECT"关键字;以","隔开)
- //@StrWhere nvarchar(2000)=NULL, --查询条件字符串(不能包括"WHERE"关键字)
- //@OrderBy varchar(500)=NULL, --排序字段连接字符串(不能包含"ORDER BY"关键字;以","隔开)
- //@OrderType bit=0, --排序类型(0:升序;非0:降序)
- //@PageIndex int=1, --当前页码索引,默认1
- //@PageSize int=10, --每页显示的记录大小,默认10
- //@RowCount int=0 output, --总行数(记录数),默认0(输出参数)
- //@PageCount int=0 output --总页数;默认0(输出参数)
- /// <summary>
- ///
- /// </summary>
- /// <param name="pcTableName">查询的数据表名</param>
- /// <param name="pcPrimaryKey">主键</param>
- /// <param name="pcSelectFieldNames">查询的字段名称(不能包含"SELECT"关键字;以","隔开)</param>
- /// <param name="pcStrWhere">查询条件字符串(不能包括"WHERE"关键字)</param>
- /// <param name="pcOrderBy">排序字段连接字符串(不能包含"ORDER BY"关键字;以","隔开)</param>
- /// <param name="piOrderType">排序类型(0:升序;非0:降序)</param>
- /// <param name="piPageIndex">当前页码索引,默认1</param>
- /// <param name="piPageSize">每页显示的记录大小,默认10</param>
- /// <param name="piRowCount">总行数(记录数),默认0(输出参数)</param>
- /// <param name="piPageCount">总页数;默认0(输出参数)</param>
- /// <returns></returns>
- public DataSet GetPageRecords(string pcTableName, string pcPrimaryKey, string pcSelectFieldNames, string pcStrWhere, string pcOrderBy,
- int piOrderType, int piPageIndex, int piPageSize, out int piRowCount, out int piPageCount)
- {
- IdCollection loColl = new IdCollection();
- SqlSPPar loSPpar = new SqlSPPar("TableName", SqlDbType.VarChar);
- loSPpar.ParaLength = 500;
- loSPpar.ParameterValue = pcTableName;
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("PrimaryKey", SqlDbType.VarChar);
- loSPpar.ParaLength = 100;
- loSPpar.ParameterValue = pcPrimaryKey;
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("SelectFieldNames", SqlDbType.VarChar);
- loSPpar.ParaLength = 2000;
- loSPpar.ParameterValue = pcSelectFieldNames;
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("StrWhere", SqlDbType.NVarChar);
- loSPpar.ParaLength = 2000;
- loSPpar.ParameterValue = pcStrWhere;
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("OrderBy", SqlDbType.VarChar);
- loSPpar.ParaLength = 2000;
- loSPpar.ParameterValue = pcOrderBy;
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("OrderType", SqlDbType.Int);
- loSPpar.ParameterValue = piOrderType + "";
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("PageIndex", SqlDbType.Int);
- loSPpar.ParameterValue = piPageIndex + "";
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("PageSize", SqlDbType.Int);
- //loSPpar.ParaLength = 5;
- loSPpar.ParameterValue = piPageSize + "";
- loColl.Add(loSPpar);
- piRowCount = 0;
- loSPpar = new SqlSPPar("RowCount", SqlDbType.Int);
- loSPpar.ParameterValue = piRowCount + "";
- loSPpar.IsOutput = true;
- loColl.Add(loSPpar);
- piPageCount = 0;
- loSPpar = new SqlSPPar("PageCount", SqlDbType.Int);
- loSPpar.ParameterValue = piPageCount + "";
- loSPpar.IsOutput = true;
- loColl.Add(loSPpar);
- DataSet loRetVal = new DataSet();
- if (!ExecuteStoredProc("p_Pagination", loColl, loRetVal))
- {
- loRetVal = null;
- }
- piRowCount = Utils.ValI(((SqlSPPar)loColl[8]).ParameterValue.ToString());
- piPageCount = Utils.ValI(((SqlSPPar)loColl[9]).ParameterValue.ToString());
- return loRetVal;
- }
- ///*************************************************************
- //Description:常用分页查询存储过程
- //**************************************************************/
- //CREATE PROC [dbo].[p_Pagination]
- //@TableName varchar(500), --查询的数据表名
- //@SelectFieldNames varchar(2000)='*', --查询的字段名称(不能包含"SELECT"关键字;以","隔开)
- //@StrWhere nvarchar(2000)=NULL, --查询条件字符串(不能包括"WHERE"关键字)
- //@OrderBy varchar(500)=NULL, --排序字段连接字符串(不能包含"ORDER BY"关键字;以","隔开)
- //@OrderType bit=0, --排序类型(0:升序;非0:降序)
- //@PageIndex int=1, --当前页码索引,默认1
- //@PageSize int=10, --每页显示的记录大小,默认10
- //@RowCount int=0 output, --总行数(记录数),默认0(输出参数)
- //@PageCount int=0 output --总页数;默认0(输出参数)
- //AS
- //BEGIN
- // --局部变量声明
- // DECLARE @StrSQL Nvarchar(4000), --分页主查询SQL语句变量
- // @StrGetCountSQL Nvarchar(4000), --查询总行数SQL语句变量
- // @PrimaryKey varchar(100), --主键列名称变量
- // @StrOrderType varchar(4) --排序类型字符(0:ASC;1:DESC)
- // --查询该表的主键列名称
- // SELECT @PrimaryKey=c.name FROM sysindexes AS i
- // JOIN sysindexkeys AS k on i.id = k.id AND i.indid = k.indid
- // JOIN sysobjects AS o on i.id = o.id
- // JOIN syscolumns AS c on i.id=c.id AND k.colid = c.colid
- // WHERE o.xtype = 'U' AND
- // EXISTS(SELECT 1 FROM sysobjects WHERE xtype = 'PK' and name = i.name)
- // AND o.name=@TableName
- // --判断页码是否小于0
- // IF(@PageIndex <1)SET @PageIndex=1;
- // IF(@SelectFieldNames='')SET @SelectFieldNames='*';
- //-- SET @StrWhere=LTRIM(RTRIM(@StrWhere));
- //-- SET @StrWhere=LTRIM(RTRIM(@StrWhere));
- // --判断查询条件是否为NULL或空字符串
- // IF ((@StrWhere IS NULL) OR (@StrWhere = '') )
- // BEGIN
- // SET @StrWhere='';
- // END
- // ELSE
- // BEGIN
- // SET @StrWhere=' AND '+@StrWhere;
- // END
- // --判断排序条件是否为NULL或空字符串
- // IF ((@OrderBy IS NULL) OR (@OrderBy = '') )
- // BEGIN
- // SET @OrderBy='';
- // END
- // ELSE
- // BEGIN
- // --判断排序类型
- // IF(@OrderType=0)
- // SET @StrOrderType='ASC';
- // ELSE
- // SET @StrOrderType='DESC';
- // SET @OrderBy=' Order By '+@OrderBy+' '+@StrOrderType;
- // END
- // --初始化查询记录总行数SQL语句
- // SET @StrGetCountSQL='SELECT @RowCount=COUNT(*) FROM '+@TableName +' WHERE 1=1 '+@StrWhere;
- // --初始化分页查询SQL语句
- // SET @StrSQL='SELECT TOP '+STR(@PageSize)+' '+@SelectFieldNames+' FROM '+@TableName
- // +' WHERE '+@PrimaryKey+' NOT IN ( SELECT TOP '+STR(@PageSize*(@PageIndex-1))+' '
- // +@PrimaryKey+' FROM '+@TableName +' WHERE 1=1 '+@StrWhere+@OrderBy+' ) '
- // +@StrWhere+@OrderBy;
- // --输出查询语句
- // print @StrGetCountSQL
- // print @StrSQL
- // --执行SQL查询语句
- // --EXEC(@StrGetCountSQL)--错误的执行带参数的方法
- // --正确的方法;声明变量输出
- // EXEC SP_EXECUTESQL @StrGetCountSQL,N'@RowCount int OUT',@RowCount out
- // --计算总页数
- // SET @PageCount=@RowCount/@PageSize
- // IF(@RowCount%@PageSize<>0)SET @PageCount=@PageCount+1;
- // EXEC(@StrSQL)
- //END
- //GO
- public DataSet GetPageRecords_RowNum(string pcTableName, string pcSelectFieldNames, string pcStrWhere, string pcOrderBy, int piPageIndex, int piPageSize, out int piRowCount, out int piPageCount)
- {
- IdCollection loColl = new IdCollection();
- SqlSPPar loSPpar = new SqlSPPar("TableName", SqlDbType.VarChar)
- {
- ParaLength = 500,
- ParameterValue = pcTableName
- };
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("SelectFieldNames", SqlDbType.VarChar)
- {
- ParaLength = 2000,
- ParameterValue = pcSelectFieldNames
- };
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("StrWhere", SqlDbType.NVarChar)
- {
- ParaLength = 4000,
- ParameterValue = pcStrWhere
- };
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("OrderBy", SqlDbType.VarChar)
- {
- ParaLength = 2000,
- ParameterValue = pcOrderBy
- };
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("PageIndex", SqlDbType.Int) {ParameterValue = piPageIndex + ""};
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("PageSize", SqlDbType.Int) {ParameterValue = piPageSize + ""};
- //loSPpar.ParaLength = 5;
- loColl.Add(loSPpar);
- piRowCount = 0;
- loSPpar = new SqlSPPar("RowCount", SqlDbType.Int)
- {
- ParameterValue = piRowCount + "",
- IsOutput = true
- };
- loColl.Add(loSPpar);
- piPageCount = 0;
- loSPpar = new SqlSPPar("PageCount", SqlDbType.Int)
- {
- ParameterValue = piPageCount + "",
- IsOutput = true
- };
- loColl.Add(loSPpar);
- DataSet loRetVal = new DataSet();
- if (!ExecuteStoredProc("Pr_Pagination_RowNum", loColl, loRetVal))
- {
- loRetVal = null;
- }
- piRowCount = Utils.ValI(((SqlSPPar)loColl[6]).ParameterValue.ToString());
- piPageCount = Utils.ValI(((SqlSPPar)loColl[7]).ParameterValue.ToString ());
- return loRetVal;
- }
- public DataSet GetPageRecords(string pcQueryStr, int piPageSize, int piPageCurrent, string pcFdShow, string pcFdOrder)
- {
- IdCollection loColl = new IdCollection();
- SqlSPPar loSPpar = new SqlSPPar("QueryStr", SqlDbType.NVarChar);
- loSPpar.ParaLength = 4000;
- loSPpar.ParameterValue = pcQueryStr;
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("PageSize", SqlDbType.Int);
- //loSPpar.ParaLength = 2000;
- loSPpar.ParameterValue = piPageSize + "";
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("PageCurrent", SqlDbType.Int);
- //loSPpar.ParaLength = 5;
- loSPpar.ParameterValue = piPageCurrent + "";
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("FdShow", SqlDbType.NVarChar);
- loSPpar.ParaLength = 4000;
- loSPpar.ParameterValue = pcFdShow;
- loColl.Add(loSPpar);
- loSPpar = new SqlSPPar("FdOrder", SqlDbType.NVarChar);
- loSPpar.ParaLength = 1000;
- loSPpar.ParameterValue = pcFdOrder;
- loColl.Add(loSPpar);
- DataSet loRetVal = new DataSet();
- if (!ExecuteStoredProc("p_Pagination", loColl, loRetVal))
- {
- loRetVal = null;
- }
- return loRetVal;
- }
- public bool ExecuteStoredProc(string pcSPName, IdCollection poParams, DataSet poOutDataSet)
- {
- FireDBConnectionAction("ExecuteStoredProc", pcSPName + " ");
- bool llRetVal = false;
- if (this._Connection != null)
- {
- try
- {
- SqlCommand loCommand = new SqlCommand(pcSPName, this._Connection, this._Transaction);
- loCommand.CommandType = CommandType.StoredProcedure;
- foreach (SqlSPPar loParmObject in poParams)
- {
- SqlParameter loSqlParm = new SqlParameter("@" + loParmObject.ParameterName.Trim(), loParmObject.ParameterType);
- if (loParmObject.IsOutput)
- {
- loSqlParm.Direction = ParameterDirection.Output;
- if (loParmObject.ParaLength > 0)
- loSqlParm.Size = loParmObject.ParaLength;
- }
- else
- {
- loSqlParm.Direction = ParameterDirection.Input;
- loSqlParm.Value = loParmObject.ParameterValue;
- if (loParmObject.ParaLength > 0)
- loSqlParm.Size = loParmObject.ParaLength;
- }
- loCommand.Parameters.Add(loSqlParm);
- }
- this._Adapter.SelectCommand = loCommand;
- this._Adapter.Fill(poOutDataSet);
- foreach (SqlParameter loParam in loCommand.Parameters)
- {
- if (loParam.Direction == ParameterDirection.Output)
- {
- string lcOutputParm = loParam.ParameterName.TrimStart('@');
- if (poParams[lcOutputParm] != null && poParams[lcOutputParm] is SqlSPPar)
- {
- SqlSPPar loOutputParm = (SqlSPPar)poParams[lcOutputParm];
- loOutputParm.ParameterValue = loParam.Value.ToString();
- }
- }
- }
- llRetVal = true;
- }
- catch (Exception e)
- {
- _ErrorMsg = "ExecuteStoredProc: " + pcSPName + " \r\n " + e.Message;
- this.FireDBConnectionError("ExecuteStoredProc", pcSPName, e.Message);
- }
- }
- return llRetVal;
- }
- /// <summary>
- /// 执行一个存储过程
- /// </summary>
- /// <param name="pcSPName">存储过程名</param>
- /// <param name="poParams">存储过程的参数</param>
- /// <returns>返回执行是否成功</returns>
- public bool ExecuteStoredProc(string pcSPName, IdCollection poParams)
- {
- FireDBConnectionAction("ExecuteStoredProc", pcSPName+" ");
- bool llRetVal = false;
- if (this._Connection != null)
- {
- try
- {
- SqlCommand loCommand = new SqlCommand(pcSPName, this._Connection,this._Transaction);
- loCommand.CommandType = CommandType.StoredProcedure;
- foreach (SqlSPPar loParmObject in poParams)
- {
- SqlParameter loSqlParm = new SqlParameter("@" + loParmObject.ParameterName.Trim(), loParmObject.ParameterType);
- if (loParmObject.IsOutput)
- {
- loSqlParm.Direction = ParameterDirection.Output;
- if (loParmObject.ParaLength > 0)
- loSqlParm.Size = loParmObject.ParaLength;
- }
- else
- {
- loSqlParm.Direction = ParameterDirection.Input;
- loSqlParm.Value = loParmObject.ParameterValue;
- if (loParmObject.ParaLength > 0)
- loSqlParm.Size = loParmObject.ParaLength;
- }
- loCommand.Parameters.Add(loSqlParm);
- }
- loCommand.ExecuteNonQuery();
- foreach (SqlParameter loParam in loCommand.Parameters)
- {
- if (loParam.Direction == ParameterDirection.Output)
- {
- string lcOutputParm = loParam.ParameterName.TrimStart('@');
- if (poParams[lcOutputParm] != null && poParams[lcOutputParm] is SqlSPPar)
- {
- SqlSPPar loOutputParm = (SqlSPPar)poParams[lcOutputParm];
- loOutputParm.ParameterValue = loParam.Value.ToString();
- }
- }
- }
- llRetVal = true;
- }
- catch (Exception e)
- {
- _ErrorMsg = "ExecuteStoredProc: " + pcSPName + " \r\n " + e.Message;
- this.FireDBConnectionError("ExecuteStoredProc", pcSPName, e.Message);
- }
- }
- return llRetVal;
- }
- private void FireDBConnectionAction(string pcCommand, string pcMessage)
- {
- if (OnDBConnectionAction != null)
- {
- if (OnlyExec)
- {
- if (pcCommand == "ExecuteSql")
- OnDBConnectionAction(pcCommand, pcMessage);
- }
- else
- OnDBConnectionAction(pcCommand, pcMessage);
- }
- }
- private void FireDBConnectionError(string pcCommand, string pcMessage, string pcErrorMessage)
- {
- if (OnDBConnectionError != null)
- {
- OnDBConnectionError(pcCommand, pcMessage, pcErrorMessage);
- }
- }
- /// <summary>
- /// 执行SQL语句,返回XML的数据集和数据结构
- /// </summary>
- /// <param name="pcSqlCommand"></param>
- /// <param name="pcXmlData"></param>
- /// <param name="pcXmlDataSchema"></param>
- /// <returns></returns>
- public bool GetData(string pcSqlCommand, ref string pcXmlData, ref string pcXmlDataSchema)
- {
- return GetData("", pcSqlCommand, ref pcXmlData, ref pcXmlDataSchema);
- }
- public bool GetData(string pcTableName, string pcSqlCommand, ref string pcXmlData, ref string pcXmlDataSchema)
- {
- FireDBConnectionAction("GetData", pcSqlCommand);
- bool lbRetVal = false;
- DataSet loDS = OpenDataSet(pcTableName, pcSqlCommand);
- if (loDS != null && loDS.Tables.Count == 1)
- {
- pcXmlData = loDS.GetXml();
- pcXmlDataSchema = loDS.GetXmlSchema();
- lbRetVal = true;
- }
-
- return lbRetVal;
- }
- // 扩展
- public bool GetData(string pcSqlCommand, ref string pcXmlData, ref string pcXmlDataSchema, ref int piRecCount)
- {
- return GetData("", pcSqlCommand, ref pcXmlData, ref pcXmlDataSchema, ref piRecCount);
- }
- public bool GetData(string pcTableName, string pcSqlCommand, ref string pcXmlData, ref string pcXmlDataSchema, ref int piRecCount)
- {
- FireDBConnectionAction("GetData", pcSqlCommand);
- bool lbRetVal = false;
- DataSet loDS = OpenDataSet(pcTableName, pcSqlCommand);
- if (loDS != null && loDS.Tables.Count == 1)
- {
- pcXmlData = loDS.GetXml();
- pcXmlDataSchema = loDS.GetXmlSchema();
- piRecCount = loDS.Tables[0].Rows.Count;
- lbRetVal = true;
- }
- return lbRetVal;
- }
- /// <summary>
- /// 打开一个dataset
- /// </summary>
- /// <param name="pcSqlCommand"></param>
- /// <returns></returns>
- public DataSet OpenDataSet(string pcSqlCommand)
- {
- return OpenDataSet("", pcSqlCommand);
- }
- public DataSet OpenDataSet(string pcTableName, string pcSqlCommand)
- {
- FireDBConnectionAction("OpenDataSet", pcSqlCommand);
- DataSet dataSet = new DataSet();
- if (this._Connection != null)
- {
- SqlCommand command = new SqlCommand(pcSqlCommand, this._Connection, this._Transaction);
- command.CommandTimeout = 60;
- this._Adapter.SelectCommand = command;
- try
- {
- if (pcTableName.Trim().Length == 0)
- this._Adapter.Fill(dataSet);
- else
- this._Adapter.Fill(dataSet, pcTableName);
- command.Dispose();
- command = null;
- }
- catch (Exception e)
- {
- _ErrorMsg = pcSqlCommand + " \r\n " + e.ToString();
- this.FireDBConnectionError("OpenDataSet", pcSqlCommand, e.Message);
- }
- }
- return dataSet;
- }
-
- /// <summary>
- /// 打开一个query
- /// </summary>
- /// <param name="pcSqlCommand"></param>
- /// <returns></returns>
- public rsQuery OpenQuery(string pcSqlCommand)
- {
- rsQuery loRetVal = new rsQuery();
- DataTable loTable = this.OpenDataTable(pcSqlCommand);
- loRetVal.OpenFromDataTable(loTable);
- return loRetVal;
- }
- /// <summary>
- /// 打开一个dataTable
- /// </summary>
- /// <param name="pcSqlCommand"></param>
- /// <returns></returns>
- public DataTable OpenDataTable(string pcSqlCommand)
- {
- FireDBConnectionAction("OpenDataTable", pcSqlCommand);
- DataTable loRetVal = new DataTable();
- if (this._Connection != null)
- {
- SqlCommand loComm = new SqlCommand(pcSqlCommand, this._Connection, this._Transaction);
- loComm.CommandTimeout = 60;
- this._Adapter.SelectCommand = loComm;
- try
- {
- this._Adapter.Fill(loRetVal);
- loComm.Dispose();
- loComm = null;
- }
- catch (Exception e)
- {
- _ErrorMsg = "OpenDataTable: " + pcSqlCommand + " \r\n " + e.Message;
- this.FireDBConnectionError("OpenDataTable", pcSqlCommand, e.Message);
- }
- }
- return loRetVal;
- }
- /// <summary>
- /// 打开一个dataTable
- /// </summary>
- /// <param name="pcSqlCommand"></param>
- /// <returns></returns>
- public DataTable OpenDataTable(DataSet poDataSet, string pcTableName, string pcSqlCommand)
- {
- FireDBConnectionAction("OpenDataTable", pcSqlCommand);
- DataTable table2 = null;
- if (this._Connection != null)
- {
- SqlCommand command = new SqlCommand(pcSqlCommand, this._Connection, this._Transaction);
- command.CommandTimeout = 60;
- if (poDataSet == null)
- {
- poDataSet = this._DataSet;
- }
- if (poDataSet.Tables.Contains(pcTableName))
- {
- poDataSet.Tables[pcTableName].Columns.Clear();
- poDataSet.Tables[pcTableName].Clear();
- }
- this._Adapter.SelectCommand = command;
- try
- {
- this._Adapter.Fill(poDataSet, pcTableName);
- command.Dispose();
- command = null;
- table2 = poDataSet.Tables[pcTableName];
- }
- catch (Exception e)
- {
- _ErrorMsg = "OpenDataTable : 执行的语句 "+pcSqlCommand +" 错误信息: "+e.Message ;
- this.FireDBConnectionError("OpenDataTable", pcSqlCommand, e.Message);
- }
- }
- return table2;
- }
- #region Properties
- private DataSet _DataSet = null;
- public DataSet DataSetDB
- {
- get
- {
- return this._DataSet;
- }
- }
- private string _ErrorMsg = "";
- public string ErrorMsg
- {
- get { return _ErrorMsg; }
- set { _ErrorMsg = value; }
- }
- public bool IsOpened
- {
- get
- {
- return (this._Status == DbConnStatus.Open);
- }
- }
- bool _OnlyExec=true;
- public bool OnlyExec
- {
- get { return _OnlyExec; }
- set { _OnlyExec = value; }
- }
- #endregion
- }
- public enum DbConnStatus
- {
- Open,
- Close,
- Error
- }
- public class SqlSPPar:INpId
- {
- // Fields
- private bool _IsOutput = false;
- private string _ParameterName;
- private SqlDbType _ParameterType;
- private object _ParameterValue = null;
-
- // Methods
- public SqlSPPar(string pcParameterName, SqlDbType pcParameterType)
- {
- this._ParameterName = pcParameterName.Trim();
- this._ParameterType = pcParameterType;
- }
- public string Id
- {
- get { return _ParameterName; }
- set { }
- }
- // Properties
- public bool IsOutput
- {
- get
- {
- return this._IsOutput;
- }
- set
- {
- this._IsOutput = value;
- }
- }
- public string ParameterName
- {
- get
- {
- return this._ParameterName;
- }
- }
- public SqlDbType ParameterType
- {
- get
- {
- return this._ParameterType;
- }
- }
- public object ParameterValue
- {
- get
- {
- return this._ParameterValue;
- }
- set
- {
- this._ParameterValue = value;
- }
- }
- private int _ParaLength = 0;
- public int ParaLength
- {
- get { return _ParaLength; }
- set { _ParaLength = value; }
- }
- }
- }
|