微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight通过Webservice连接数据库操作

@H_404_4@ 

Silverlight通过Webservice连接数据库操作

 
    silverlight(简称SL)进行数据库操作,有多种方法,这里介绍最传统的通过WebService(简称WS)实现方式。本文的主要目的是在SL不支持DataSet和DataTable的基础上,提供通用的返回数据的WS方法
一:创建项目
    首先,创建SL应用程序,如QuestionsDbSL,创建的时候选择生成网站QuestionsDbSL.Web。另外,往往大家不想将sql语句写在主应用程序中,所以,这里还需要创建一个业务层QuestionsDbSLServices。同时,实体层,我们创建为QuestionsDbSLModel。
    QuestionsDbSL:SL主应用程序;
    QuestionsDbSL.Web:承载SL主应用程序的网站,同时也提供WS;
    QuestionsDbSLServices:SL类库项目,提供逻辑层;
    QuestionsDbSLModel:SL类库项目,提供实体层,对应数据库表。
 
二:必要的辅助类和文件
    第一个辅助类,就是sqlHelper,对数据库的直接操作,由它提供直接对数据库的操作。见附件一:sqlHelper.cs。该文件创建在QuestionsDbSL.Web.ClientBin中。
    第二个是配置文件Web.config,这里需要用到的是定义数据连接字符串:
 <ppSettings>
  <add key="ConnString" value="server =192.168.0.96; user id = sa; password = sa; database = xxxProp"/>
 </appSettings>
    
三:WS函数
    首先是获取记录集的函数
    public List<BaseVars> GetListBaseVars(string sql,params sqlParameter[] commandParams)
    很遗憾的一点是,SL不支持DataSet或者DataTable,所以你无法直接返回一个记录集。但你可以通过返回一个List的方法来迂回解决
    如数据库表Student,则你可以返回一个List<Student>。问题的关键是,这样一来,你不得不为每一类查询提供一个WS函数。也许你会尝试用List<T>来解决这个问题,同样遗憾的是,SL调用WS函数的时候,只能序列化一般类型的类,所以我们必须换一个方式解决该问题。
    要知道,数据表也就是各种基础类型的字段所组成的,所以,我们来模拟一张表就是:
    /// <summary>
    /// 模拟数据库
    /// PS:一张表的字段不外乎几个基本数据类型,查询出来的值,在这里进行赋值
    /// 本类的一个实例,相当于是一条记录,本类的一个实例数组,就相当于是datatable
    /// 在UI端,你可将本类的实例数组转换为数据源
    /// </summary>
    public class BaseVars
    {
        public BaseVars()
        {
            ListString = new List<string>();
            ListInt = new List<int>();
            ListBool = new List<bool>();
            ListByte = new List<byte>();
            ListFloat = new List<float>();
            ListDouble = new List<double>();
        }
        public List<string> ListString { get; set; }
        public List<int> ListInt { get; set; }
        public List<bool> ListBool { get; set; }
        public List<byte> ListByte { get; set; }
        public List<float> ListFloat { get; set; }
        public List<double> ListDouble { get; set; }
    }
    则,WS函数就是如下:
        /// <summary>
        /// 通用查询方法
        /// 1:查询返回的记录,将会生成实例BaseVars;
        /// 2:本方法返回BaseVars的列表;
        /// 3:在调用方,要重新对数据进行组织,以便进行展示;
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="commandParams"></param>
        /// <returns></returns>
        [WebMethod]
        public List<BaseVars> GetListBaseVars(string sql,params sqlParameter[] commandParams)
        {
            List<BaseVars> lr = new List<BaseVars>();
            using (DataSet ds = sqlHelper.ExecuteDataSet(sql,commandParams))
            {
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return null;
                }
                lr = ListBaseDataSet.DataSetToListBaseVars(ds,0);
            }
            return lr;
        }
    注意到上段代码中,ListBaseDataSet.DataSetToListBaseVars(ds,0)就是将一个DataSet转换为BaseVars的一个实例列表。ListBaseDataSet.cs见附件二。
 
四:WS调用
    WS的调用在SL都是异步调用,WS提供的函数名在调用方都会被自动加上一个后缀Async。所以,如果你觉得有必要,你就需要提供一个委托函数来在WS调用完毕的做点什么,如下:
wss.GetListBaseVarsCompleted += new EventHandler<QuestionsDbSLServices.ServiceReferenceYi.GetListBaseVarsCompletedEventArgs>(wss_GetListBaseVarsCompleted);
    wss是WS的实例,wss_GetListBaseVarsCompleted就是该委托函数。在该委托函数中,我是将得到的数据记录重构成一个具体对象的列表。
        void wss_GetListBaseVarsCompleted(object sender,QuestionsDbSLServices.ServiceReferenceYi.GetListBaseVarsCompletedEventArgs e)
        {
            ServiceReferenceYi.BaseVars[] lr = e.Result;
            foreach (var item in lr)
            {
                QuestionsDbSLModel.Res_Source ds = new QuestionsDbSLModel.Res_Source();
                ds.sourceId = item.ListInt[0];
                ds.sourceName = item.ListString[0];
                ds.dispOrder = item.ListInt[1];
                ldResource.Add(ds);
            }
        }
 
五:一个BUG
     在业务层QuestionsDbSLServices引用WS后,在QuestionsDbSL使用QuestionsDbSLServices后会报错,查询结果是配置文件ServiceReferences.ClientConfig未被包含在XAP中。如果你直接在WEB中调用WS,则在打包项目的时候,ServiceReferences.ClientConfig自动打包进去。
    解决的办法是,你在WEB中,需要手动创建一个ServiceReferences.ClientConfig,然后将QuestionsDbSLServices的该文件内容copY进去。
 
附件一:sqlHelper.cs
  @H_404_4@using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.sqlClient;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Diagnostics;

@H_404_4@namespace QuestionsDbSL.Web.ClientBin
{
    [DebuggerStepThrough]
    public sealed class sqlHelper
    {
        #region private utility methods & constructors

@H_404_4@        //Since this class provides only static methods,make the default constructor private to prevent
        //instances from being created with "new sqlHelper()".
        private sqlHelper() { }

@H_404_4@        /// <summary>
        /// This method is used to attach array of sqlParameters to a sqlCommand.
        /// This method will assign a value of dbnull to any parameter with a direction of
        /// InputOutput and a value of null.
        /// This behavior will prevent default values from being used,but
        /// this will be the less common case than an intended pure output parameter (derived as InputOutput)
        /// where the user provided no input value.
        /// </summary>
        /// <param name="command">The command to which the parameters will be added</param>
        /// <param name="commandParameters">an array of sqlParameters tho be added to command</param>
        private static void AttachParameters(sqlCommand command,sqlParameter[] commandParameters)
        {
            foreach (sqlParameter p in commandParameters)
            {
                //check for derived output value with no value assigned
                if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
                {
                    p.Value = dbnull.Value;
                }
                command.Parameters.Add(p);
            }
        }

@H_404_4@        /// <summary>
        /// This method assigns an array of values to an array of sqlParameters.
        /// </summary>
        /// <param name="commandParameters">array of sqlParameters to be assigned values</param>
        /// <param name="parameterValues">array of Components holding the values to be assigned</param>
        private static void AssignParameterValues(sqlParameter[] commandParameters,object[] parameterValues)
        {
            if ((commandParameters == null) || (parameterValues == null))
            {
                //do nothing if we get no data
                return;
            }

@H_404_4@            // we must have the same number of values as we pave parameters to put them in
            if (commandParameters.Length != parameterValues.Length)
            {
                throw new ArgumentException("Parameter count does not match Parameter Value count.");
            }

@H_404_4@            //iterate through the sqlParameters,assigning the values from the corresponding position in the
            //value array
            for (int i = 0,j = commandParameters.Length; i < j; i++)
            {
                commandParameters[i].Value = parameterValues[i];
            }
        }

@H_404_4@        /// <summary>
        /// This method opens (if necessary) and assigns a connection,transaction,command type and parameters
        /// to the provided command.
        /// </summary>
        /// <param name="command">the sqlCommand to be prepared</param>
        /// <param name="connection">a valid sqlConnection,on which to execute this command</param>
        /// <param name="transaction">a valid sqlTransaction,or 'null'</param>
        /// <param name="commandType">the CommandType (stored procedure,text,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParameters to be associated with the command or 'null' if no parameters are required</param>
        private static void PrepareCommand(sqlCommand command,sqlConnection connection,sqlTransaction transaction,CommandType commandType,string commandText,sqlParameter[] commandParameters)
        {
            //if the provided connection is not open,we will open it
            if (connection.State != ConnectionState.Open)
                connection.open();

@H_404_4@            //associate the connection with the command
            command.Connection = connection;
            command.CommandTimeout = 180;

@H_404_4@            //set the command text (stored procedure name or sql statement)
            command.CommandText = commandText;

@H_404_4@            //if we were provided a transaction,assign it.
            if (transaction != null)
                command.Transaction = transaction;

@H_404_4@            //set the command type
            command.CommandType = commandType;

@H_404_4@            //attach the command parameters if they are provided
            if (commandParameters != null)
                AttachParameters(command,commandParameters);

@H_404_4@            return;
        }

@H_404_4@
        #endregion private utility methods & constructors

@H_404_4@        #region DataHelpers

@H_404_4@        public static string CheckNull(object obj)
        {
            return (string)obj;
        }

@H_404_4@        public static string CheckNull(dbnull obj)
        {
            return null;
        }

@H_404_4@        #endregion

@H_404_4@        #region AddParameters

@H_404_4@        public static object CheckForNullString(string text)
        {
            if (text == null || text.Trim().Length == 0)
            {
                return dbnull.Value;
            }
            else
            {
                return text;
            }
        }

@H_404_4@        public static sqlParameter MakeInParam(string ParamName,object Value)
        {
            return new sqlParameter(ParamName,Value);
        }

@H_404_4@        /// <summary>
        /// Make input param.
        /// </summary>
        /// <param name="ParamName">Name of param.</param>
        /// <param name="DbType">Param type.</param>
        /// <param name="Size">Param size.</param>
        /// <param name="Value">Param value.</param>
        /// <returns>New parameter.</returns>
        public static sqlParameter MakeInParam(string ParamName,sqlDbType DbType,int Size,object Value)
        {
            return MakeParam(ParamName,DbType,Size,ParameterDirection.Input,Value);
        }

@H_404_4@        /// <summary>
        /// Make input param.
        /// </summary>
        /// <param name="ParamName">Name of param.</param>
        /// <param name="DbType">Param type.</param>
        /// <param name="Size">Param size.</param>
        /// <returns>New parameter.</returns>
        public static sqlParameter MakeOutParam(string ParamName,int Size)
        {
            return MakeParam(ParamName,ParameterDirection.Output,null);
        }

@H_404_4@        /// <summary>
        /// Make stored procedure param.
        /// </summary>
        /// <param name="ParamName">Name of param.</param>
        /// <param name="DbType">Param type.</param>
        /// <param name="Size">Param size.</param>
        /// <param name="Direction">Parm direction.</param>
        /// <param name="Value">Param value.</param>
        /// <returns>New parameter.</returns>
        public static sqlParameter MakeParam(string ParamName,Int32 Size,ParameterDirection Direction,object Value)
        {
            sqlParameter param;

@H_404_4@            if (Size > 0)
                param = new sqlParameter(ParamName,Size);
            else
                param = new sqlParameter(ParamName,DbType);

@H_404_4@            param.Direction = Direction;
            if (!(Direction == ParameterDirection.Output && Value == null))
                param.Value = Value;

@H_404_4@            return param;
        }

@H_404_4@
        #endregion

@H_404_4@        #region ExecuteNonQuery

@H_404_4@        /// <summary>
        /// 执行一个简单的sqlcommand 没有返回结果
        /// </summary>
        ///  例如: 
        ///  int result = ExecuteNonQuery("delete from test where 1>2 ");  返回 result =0
        /// <param name="commandText">只能是sql语句</param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteNonQuery(string commandText)
        {
            return ExecuteNonQuery(ConfigurationManager.AppSettings["ConnString"],CommandType.Text,commandText,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// 执行一个简单的sqlcommand 没有返回结果
        /// </summary>
        /// 例如:   
        ///  int result = ExecuteNonQuery("delete from test where  tt =@tt",new sqlParameter("@tt",24));
        /// <param name="commandText">只能是sql语句</param>
        /// <param name="commandParameters">参数</param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteNonQuery(string commandText,params sqlParameter[] commandParameters)
        {
            int i = 0;
            using (sqlConnection cn = new sqlConnection(ConfigurationManager.AppSettings["ConnString"]))
            {
                cn.open();
                return ExecuteNonQuery(cn,commandParameters);
            }
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns no resultset and takes no parameters) against the database specified in
        /// the connection string.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int result = ExecuteNonQuery(connString,CommandType.StoredProcedure,"PublishOrders");
        /// </remarks>
        /// <param name="connectionString">a valid connection string for a sqlConnection</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>an int representing the number of rows affected by the command</returns>
        public static int ExecuteNonQuery(string connectionString,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteNonQuery(connectionString,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns no resultset and takes no parameters) against the database specified in
        /// the connection string.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int result = ExecuteNonQuery(connString,commandType,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns no resultset) against the database specified in the connection string
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int result = ExecuteNonQuery(connString,"PublishOrders",new sqlParameter("@prodid",24));
        /// </remarks>
        /// <param name="connectionString">a valid connection string for a sqlConnection</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>an int representing the number of rows affected by the command</returns>
        public static int ExecuteNonQuery(string connectionString,params sqlParameter[] commandParameters)
        {
            //create & open a sqlConnection,and dispose of it after we are done.
            using (sqlConnection cn = new sqlConnection(connectionString))
            {
                cn.open();

@H_404_4@                //call the overload that takes a connection in place of the connection string
                return ExecuteNonQuery(cn,commandParameters);
            }
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns no resultset and takes no parameters) against the provided sqlConnection.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int result = ExecuteNonQuery(conn,"PublishOrders");
        /// </remarks>
        /// <param name="connection">a valid sqlConnection</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>an int representing the number of rows affected by the command</returns>
        public static int ExecuteNonQuery(sqlConnection connection,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteNonQuery(connection,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns no resultset and takes no parameters) against the provided sqlConnection.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int result = ExecuteNonQuery(conn,"PublishOrders");
        /// </remarks>
        /// <param name="connection">a valid sqlConnection</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>an int representing the number of rows affected by the command</returns>
        public static int ExecuteNonQuery(sqlConnection connection,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns no resultset) against the specified sqlConnection
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int result = ExecuteNonQuery(conn,24));
        /// </remarks>
        /// <param name="connection">a valid sqlConnection</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>an int representing the number of rows affected by the command</returns>
        public static int ExecuteNonQuery(sqlConnection connection,params sqlParameter[] commandParameters)
        {
            return ExecuteNonQuery(connection,commandParameters);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns no resultset) against the specified sqlConnection
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int result = ExecuteNonQuery(conn,params sqlParameter[] commandParameters)
        {
            //create a command and prepare it for execution
            sqlCommand cmd = new sqlCommand();
            PrepareCommand(cmd,connection,(sqlTransaction)null,commandParameters);

@H_404_4@            //finally,execute the command.
            int retval = cmd.ExecuteNonQuery();

@H_404_4@            // detach the sqlParameters from the command object,so they can be used again.
            cmd.Parameters.Clear();
            if (connection.State == ConnectionState.Open)
                connection.Close();
            return retval;
        }

@H_404_4@        /// <summary>
        /// 执行一个简单的sqlcommand 没有返回结果
        /// </summary>
        ///  例如: 
        ///  int result = ExecuteNonQuery(myTran,"delete from test where 1>2 ");  返回 result =0
        /// <param name="transaction">事务名称</param>
        /// <param name="commandText">只能是sql语句</param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteNonQuery(sqlTransaction transaction,string commandText)
        {
            return ExecuteNonQuery(transaction,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns no resultset and takes no parameters) against the provided sqlTransaction.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int result = ExecuteNonQuery(trans,"PublishOrders");
        /// </remarks>
        /// <param name="transaction">a valid sqlTransaction</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>an int representing the number of rows affected by the command</returns>
        public static int ExecuteNonQuery(sqlTransaction transaction,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteNonQuery(transaction,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// 带参数和参数的查询
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="commandText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(sqlTransaction transaction,params sqlParameter[] commandParameters)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteNonQuery(transaction,commandParameters);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns no resultset) against the specified sqlTransaction
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int result = ExecuteNonQuery(trans,"Getorders",24));
        /// </remarks>
        /// <param name="transaction">a valid sqlTransaction</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>an int representing the number of rows affected by the command</returns>
        public static int ExecuteNonQuery(sqlTransaction transaction,params sqlParameter[] commandParameters)
        {
            if (transaction == null)
            {
                //create & open a sqlConnection,and dispose of it after we are done.
                using (sqlConnection cn = new sqlConnection(ConfigurationManager.AppSettings["ConnString"]))
                {
                    cn.open();

@H_404_4@                    //call the overload that takes a connection in place of the connection string
                    return ExecuteNonQuery(cn,commandParameters);
                }
            }
            else
            {
                //create a command and prepare it for execution
                sqlCommand cmd = new sqlCommand();
                PrepareCommand(cmd,transaction.Connection,commandParameters);

@H_404_4@                //finally,execute the command.
                int retval = cmd.ExecuteNonQuery();

@H_404_4@                // detach the sqlParameters from the command object,so they can be used again.
                cmd.Parameters.Clear();
                return retval;
            }
        }

@H_404_4@        #endregion ExecuteNonQuery

@H_404_4@        #region 执行sqlDataAdapter
        public static sqlDataAdapter ExecutesqlDataAdapter(string commandText)
        {
            using (sqlConnection cn = new sqlConnection(ConfigurationManager.AppSettings["ConnString"]))
            {
                cn.open();
                return ExecutesqlDataAdapter(cn,commandText);
            }
        }

@H_404_4@        public static sqlDataAdapter ExecutesqlDataAdapter(sqlConnection connection,string commandText)
        {
            sqlDataAdapter myda = new sqlDataAdapter(commandText,connection);
            myda.SelectCommand.CommandTimeout = 180;
            connection.Close();
            return myda;
        }

@H_404_4@        #endregion

@H_404_4@        #region ExecuteDataSet

@H_404_4@        /// <summary>
        /// 返回datataset 只需要传入查询语句
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteDataSet(ConfigurationManager.AppSettings["ConnString"],(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// 带参数查询
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(string commandText,params sqlParameter[] commandParameters)
        {
            using (sqlConnection cn = new sqlConnection(ConfigurationManager.AppSettings["ConnString"]))
            {
                cn.open();
                return ExecuteDataSet(cn,commandParameters);
            }
        }

@H_404_4@        /// <summary>
        /// 执行存储过程 返回相应的dataset
        /// </summary>
        /// <param name="commandText">存储过程名字</param>
        /// <param name="commandType"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(string commandText,commandParameters);
            }
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the database specified in
        /// the connection string.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataSet ds = ExecuteDataset(connString,"Getorders");
        /// </remarks>
        /// <param name="connectionString">a valid connection string for a sqlConnection</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>a dataset containing the resultset generated by the command</returns>
        public static DataSet ExecuteDataSet(string connectionString,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteDataSet(connectionString,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the database specified in
        /// the connection string.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataSet ds = ExecuteDataset(connString,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset) against the database specified in the connection string
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataSet ds = ExecuteDataset(connString,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>a dataset containing the resultset generated by the command</returns>
        public static DataSet ExecuteDataSet(string connectionString,and dispose of it after we are done.
            try
            {
                using (sqlConnection cn = new sqlConnection(connectionString))
                {
                    cn.open();

@H_404_4@
                    //call the overload that takes a connection in place of the connection string
                    return ExecuteDataSet(cn,commandParameters);
                }
            }
            catch (Exception e)
            {

@H_404_4@                throw e;
            }
        }

@H_404_4@        /// <summary>
        /// 返回datataset 只需要传入查询语句
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(sqlConnection connection,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteDataSet(connection,commandText);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the provided sqlConnection.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataSet ds = ExecuteDataset(conn,"Getorders");
        /// </remarks>
        /// <param name="connection">a valid sqlConnection</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>a dataset containing the resultset generated by the command</returns>
        public static DataSet ExecuteDataSet(sqlConnection connection,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset) against the specified sqlConnection
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataSet ds = ExecuteDataset(conn,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>a dataset containing the resultset generated by the command</returns>
        public static DataSet ExecuteDataSet(sqlConnection connection,commandParameters);

@H_404_4@            //create the DataAdapter & DataSet
            sqlDataAdapter da = new sqlDataAdapter(cmd);
            DataSet ds = new DataSet();

@H_404_4@            //fill the DataSet using default values for DataTable names,etc.
            da.Fill(ds);

@H_404_4@            // detach the sqlParameters from the command object,so they can be used again.   
            cmd.Parameters.Clear();

@H_404_4@            if (connection.State == ConnectionState.Open)
                connection.Close();
            //return the dataset
            return ds;
        }

@H_404_4@        /// <summary>
        /// s事务中执行返回dataset
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(sqlTransaction transaction,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteDataSet(transaction,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the provided sqlTransaction.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataSet ds = ExecuteDataset(trans,"Getorders");
        /// </remarks>
        /// <param name="transaction">a valid sqlTransaction</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>a dataset containing the resultset generated by the command</returns>
        public static DataSet ExecuteDataSet(sqlTransaction transaction,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// 事务中返回dataset 可带参数
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="commandText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(sqlTransaction transaction,params sqlParameter[] commandParameters)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteDataSet(transaction,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset) against the specified sqlTransaction
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataSet ds = ExecuteDataset(trans,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>a dataset containing the resultset generated by the command</returns>
        public static DataSet ExecuteDataSet(sqlTransaction transaction,and dispose of it after we are done.
                using (sqlConnection cn = new sqlConnection(ConfigurationManager.AppSettings["ConnString"]))
                {
                    cn.open();

@H_404_4@                    //call the overload that takes a connection in place of the connection string
                    return ExecuteDataSet(cn,commandParameters);

@H_404_4@                //create the DataAdapter & DataSet
                sqlDataAdapter da = new sqlDataAdapter(cmd);
                DataSet ds = new DataSet();

@H_404_4@                //fill the DataSet using default values for DataTable names,etc.
                da.Fill(ds);

@H_404_4@                // detach the sqlParameters from the command object,so they can be used again.
                cmd.Parameters.Clear();

@H_404_4@                //return the dataset
                return ds;
            }
        }

@H_404_4@        #endregion ExecuteDataSet

@H_404_4@        #region ExecuteDataTable

@H_404_4@        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static DataTable ExecuteDataTable(string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteDataTable(ConfigurationManager.AppSettings["ConnString"],(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static DataTable ExecuteDataTable(string commandText,params sqlParameter[] commandParameters)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteDataTable(ConfigurationManager.AppSettings["ConnString"],commandParameters);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the database specified in
        /// the connection string.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataTable dt = ExecuteDataTable(connString,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>a DataTable containing the resultset generated by the command</returns>
        public static DataTable ExecuteDataTable(string connectionString,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteDataTable(connectionString,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset) against the database specified in the connection string
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataTable dt = ExecuteDataTable(connString,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>a DataTable containing the resultset generated by the command</returns>
        public static DataTable ExecuteDataTable(string connectionString,and dispose of it after we are done.
            using (sqlConnection cn = new sqlConnection(connectionString))
            {
                cn.open();

@H_404_4@                //call the overload that takes a connection in place of the connection string
                return ExecuteDataTable(cn,commandParameters);
            }
        }

@H_404_4@        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <param name="commandText"></param>
        /// <returns></returns>
        public static DataTable ExecuteDataTable(sqlConnection connection,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteDataTable(connection,commandText);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the provided sqlConnection.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataTable dt = ExecuteDataTable(conn,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>a DataTable containing the resultset generated by the command</returns>
        public static DataTable ExecuteDataTable(sqlConnection connection,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset) against the specified sqlConnection
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataTable dt = ExecuteDataTable(conn,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>a DataTable containing the resultset generated by the command</returns>
        public static DataTable ExecuteDataTable(sqlConnection connection,commandParameters);

@H_404_4@            //create the DataAdapter & DataTable
            sqlDataAdapter da = new sqlDataAdapter(cmd);
            DataTable dt = new DataTable();

@H_404_4@            //fill the DataTable using default values for DataTable names,etc.
            da.Fill(dt);

@H_404_4@            // detach the sqlParameters from the command object,so they can be used again.   
            cmd.Parameters.Clear();

@H_404_4@            if (connection.State == ConnectionState.Open)
                connection.Close();
            //return the DataTable
            return dt;
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the provided sqlTransaction.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataTable dt = ExecuteDataTable(trans,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>a DataTable containing the resultset generated by the command</returns>
        public static DataTable ExecuteDataTable(sqlTransaction transaction,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteDataTable(transaction,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the provided sqlTransaction.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataTable dt = ExecuteDataTable(trans,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset) against the specified sqlTransaction
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  DataTable dt = ExecuteDataTable(trans,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>a DataTable containing the resultset generated by the command</returns>
        public static DataTable ExecuteDataTable(sqlTransaction transaction,and dispose of it after we are done.
                using (sqlConnection cn = new sqlConnection(ConfigurationManager.AppSettings["ConnString"]))
                {
                    cn.open();

@H_404_4@                    //call the overload that takes a connection in place of the connection string
                    return ExecuteDataTable(cn,commandParameters);

@H_404_4@                //create the DataAdapter & DataTable
                sqlDataAdapter da = new sqlDataAdapter(cmd);
                DataTable dt = new DataTable();

@H_404_4@                //fill the DataTable using default values for DataTable names,etc.
                da.Fill(dt);

@H_404_4@                // detach the sqlParameters from the command object,so they can be used again.
                cmd.Parameters.Clear();

@H_404_4@                //return the DataTable
                return dt;
            }
        }

@H_404_4@        #endregion ExecuteDataTable

@H_404_4@        #region ExecuteReader

@H_404_4@        /// <summary>
        /// this enum is used to indicate whether the connection was provided by the caller,or created by sqlHelper,so that
        /// we can set the appropriate CommandBehavior when calling ExecuteReader()
        /// </summary>
        private enum sqlConnectioNownership
        {
            /// <summary>Connection is owned and managed by sqlHelper</summary>
            Internal,
            /// <summary>Connection is owned and managed by the caller</summary>
            External
        }

@H_404_4@        /// <summary>
        /// 返回sqlDataReader 只是传入一条sql语句
        /// </summary>
        /// <param name="commandText">sql语句</param>
        /// <returns></returns>
        public static sqlDataReader ExecuteReader(string commandText)
        {
            return ExecuteReader(ConfigurationManager.AppSettings["ConnString"],(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// 返回sqlDataReader 只是传入一条sql语句和相应的参数
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        public static sqlDataReader ExecuteReader(string commandText,params sqlParameter[] commandParameters)
        {
            return ExecuteReader(ConfigurationManager.AppSettings["ConnString"],commandParameters);
        }

@H_404_4@        /// <summary>
        /// 返回sqlDataReader 只是传入一条sql语句和相应的参数
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="commandParameters"></param>
        /// <returns></returns>
        public static sqlDataReader ExecuteReader(string commandText,commandParameters);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the database specified in
        /// the connection string.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  sqlDataReader dr = ExecuteReader(connString,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>a sqlDataReader containing the resultset generated by the command</returns>
        public static sqlDataReader ExecuteReader(string connectionString,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteReader(connectionString,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the database specified in
        /// the connection string.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  sqlDataReader dr = ExecuteReader(connString,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset) against the database specified in the connection string
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  sqlDataReader dr = ExecuteReader(connString,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>a sqlDataReader containing the resultset generated by the command</returns>
        public static sqlDataReader ExecuteReader(string connectionString,params sqlParameter[] commandParameters)
        {
            //create & open a sqlConnection
            sqlConnection cn = new sqlConnection(connectionString);
            cn.open();

@H_404_4@            try
            {
                //call the private overload that takes an internally owned connection in place of the connection string
                return ExecuteReader(cn,null,commandParameters,sqlConnectioNownership.Internal);
            }
            catch
            {
                //if we fail to return the sqlDatReader,we need to close the connection ourselves
                cn.Close();
                throw;
            }
        }

@H_404_4@        /// <summary>
        /// 返回sqlDataReader 只是传入一条sql语句
        /// </summary>
        /// <param name="commandText">sql语句</param>
        /// <returns></returns>
        public static sqlDataReader ExecuteReader(sqlConnection connection,string commandText)
        {
            return ExecuteReader(connection,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the provided sqlConnection.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  sqlDataReader dr = ExecuteReader(conn,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>a sqlDataReader containing the resultset generated by the command</returns>
        public static sqlDataReader ExecuteReader(sqlConnection connection,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteReader(connection,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset) against the specified sqlConnection
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  sqlDataReader dr = ExecuteReader(conn,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>a sqlDataReader containing the resultset generated by the command</returns>
        public static sqlDataReader ExecuteReader(sqlConnection connection,params sqlParameter[] commandParameters)
        {
            //pass through the call to the private overload using a null transaction value and an externally owned connection
            return ExecuteReader(connection,sqlConnectioNownership.External);
        }

@H_404_4@        /// <summary>
        /// Create and prepare a sqlCommand,and call ExecuteReader with the appropriate CommandBehavior.
        /// </summary>
        /// <remarks>
        /// If we created and opened the connection,we want the connection to be closed when the DataReader is closed.
        ///
        /// If the caller provided the connection,we want to leave it to them to manage.
        /// </remarks>
        /// <param name="connection">a valid sqlConnection,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParameters to be associated with the command or 'null' if no parameters are required</param>
        /// <param name="connectioNownership">indicates whether the connection parameter was provided by the caller,or created by sqlHelper</param>
        /// <returns>sqlDataReader containing the results of the command</returns>
        private static sqlDataReader ExecuteReader(sqlConnection connection,sqlParameter[] commandParameters,sqlConnectioNownership connectioNownership)
        {
            //create a command and prepare it for execution
            sqlCommand cmd = new sqlCommand();
            PrepareCommand(cmd,commandParameters);

@H_404_4@            //create a reader
            sqlDataReader dr;

@H_404_4@            // call ExecuteReader with the appropriate CommandBehavior
            if (connectioNownership == sqlConnectioNownership.External)
                dr = cmd.ExecuteReader();
            else
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

@H_404_4@            // detach the sqlParameters from the command object,so they can be used again.
            cmd.Parameters.Clear();

@H_404_4@            return dr;
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the provided sqlTransaction.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  sqlDataReader dr = ExecuteReader(trans,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>a sqlDataReader containing the resultset generated by the command</returns>
        public static sqlDataReader ExecuteReader(sqlTransaction transaction,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteReader(transaction,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset) against the specified sqlTransaction
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///   sqlDataReader dr = ExecuteReader(trans,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>a sqlDataReader containing the resultset generated by the command</returns>
        public static sqlDataReader ExecuteReader(sqlTransaction transaction,params sqlParameter[] commandParameters)
        {
            if (transaction == null)
            {
                //create & open a sqlConnection
                sqlConnection cn = new sqlConnection(ConfigurationManager.AppSettings["ConnString"]);
                cn.open();

@H_404_4@                try
                {
                    //call the private overload that takes an internally owned connection in place of the connection string
                    return ExecuteReader(cn,sqlConnectioNownership.Internal);
                }
                catch
                {
                    //if we fail to return the sqlDatReader,we need to close the connection ourselves
                    cn.Close();
                    throw;
                }
            }
            else
                //pass through to private overload,indicating that the connection is owned by the caller
                return ExecuteReader(transaction.Connection,sqlConnectioNownership.External);
        }

@H_404_4@        #endregion ExecuteReader

@H_404_4@        #region ExecuteScalar

@H_404_4@        /// <summary>
        /// 返回ExecuteScalar 只是传入一条sql语句
        /// </summary>
        /// <param name="commandText">sql语句</param>
        /// <returns></returns>
        public static object ExecuteScalar(string commandText)
        {
            return ExecuteScalar(ConfigurationManager.AppSettings["ConnString"],(sqlParameter[])null);
        }

@H_404_4@        public static object ExecuteScalar(string commandText,params sqlParameter[] commandParameters)
        {
            return ExecuteScalar(ConfigurationManager.AppSettings["ConnString"],commandParameters);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a 1x1 resultset and takes no parameters) against the database specified in
        /// the connection string.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int orderCount = (int)ExecuteScalar(connString,"GetorderCount");
        /// </remarks>
        /// <param name="connectionString">a valid connection string for a sqlConnection</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
        public static object ExecuteScalar(string connectionString,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteScalar(connectionString,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a 1x1 resultset) against the database specified in the connection string
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int orderCount = (int)ExecuteScalar(connString,"GetorderCount",etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
        public static object ExecuteScalar(string connectionString,and dispose of it after we are done.
            using (sqlConnection cn = new sqlConnection(connectionString))
            {
                cn.open();

@H_404_4@                //call the overload that takes a connection in place of the connection string
                return ExecuteScalar(cn,commandParameters);
            }
        }

@H_404_4@        /// <summary>
        /// 返回ExecuteScalar 只是传入一条sql语句
        /// </summary>
        /// <param name="commandText">sql语句</param>
        /// <returns></returns>
        public static object ExecuteScalar(sqlConnection connection,string commandText)
        {
            return ExecuteScalar(connection,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a 1x1 resultset and takes no parameters) against the provided sqlConnection.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int orderCount = (int)ExecuteScalar(conn,"GetorderCount");
        /// </remarks>
        /// <param name="connection">a valid sqlConnection</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
        public static object ExecuteScalar(sqlConnection connection,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteScalar(connection,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a 1x1 resultset) against the specified sqlConnection
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int orderCount = (int)ExecuteScalar(conn,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
        public static object ExecuteScalar(sqlConnection connection,params sqlParameter[] commandParameters)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteScalar(connection,commandParameters);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a 1x1 resultset) against the specified sqlConnection
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int orderCount = (int)ExecuteScalar(conn,commandParameters);

@H_404_4@            //execute the command & return the results
            object retval = cmd.ExecuteScalar();

@H_404_4@            // detach the sqlParameters from the command object,so they can be used again.
            cmd.Parameters.Clear();
            if (connection.State == ConnectionState.Open)
                connection.Close();
            return retval;

@H_404_4@        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a 1x1 resultset and takes no parameters) against the provided sqlTransaction.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int orderCount = (int)ExecuteScalar(trans,"GetorderCount");
        /// </remarks>
        /// <param name="transaction">a valid sqlTransaction</param>
        /// <param name="commandType">the CommandType (stored procedure,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
        public static object ExecuteScalar(sqlTransaction transaction,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteScalar(transaction,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a 1x1 resultset and takes no parameters) against the provided sqlTransaction.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int orderCount = (int)ExecuteScalar(trans,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a 1x1 resultset) against the specified sqlTransaction
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int orderCount = (int)ExecuteScalar(trans,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
        public static object ExecuteScalar(sqlTransaction transaction,params sqlParameter[] commandParameters)
        {
            return ExecuteScalar(transaction,commandParameters);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a 1x1 resultset) against the specified sqlTransaction
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  int orderCount = (int)ExecuteScalar(trans,and dispose of it after we are done.
                using (sqlConnection cn = new sqlConnection(ConfigurationManager.AppSettings["ConnString"]))
                {
                    cn.open();

@H_404_4@                    //call the overload that takes a connection in place of the connection string
                    return ExecuteScalar(cn,commandParameters);

@H_404_4@                //execute the command & return the results
                object retval = cmd.ExecuteScalar();

@H_404_4@                // detach the sqlParameters from the command object,so they can be used again.
                cmd.Parameters.Clear();
                return retval;
            }
        }

@H_404_4@        #endregion ExecuteScalar

@H_404_4@        #region ExecuteXmlReader

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the provided sqlConnection.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  XmlReader r = ExecuteXmlReader(conn,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command using "FOR XML AUTO"</param>
        /// <returns>an XmlReader containing the resultset generated by the command</returns>
        public static XmlReader ExecuteXmlReader(sqlConnection connection,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteXmlReader(connection,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset) against the specified sqlConnection
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  XmlReader r = ExecuteXmlReader(conn,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command using "FOR XML AUTO"</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>an XmlReader containing the resultset generated by the command</returns>
        public static XmlReader ExecuteXmlReader(sqlConnection connection,commandParameters);

@H_404_4@            //create the DataAdapter & DataSet
            XmlReader retval = cmd.ExecuteXmlReader();

@H_404_4@            // detach the sqlParameters from the command object,so they can be used again.
            cmd.Parameters.Clear();
            if (connection.State == ConnectionState.Open)
                connection.Close();
            return retval;

@H_404_4@        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset and takes no parameters) against the provided sqlTransaction.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  XmlReader r = ExecuteXmlReader(trans,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command using "FOR XML AUTO"</param>
        /// <returns>an XmlReader containing the resultset generated by the command</returns>
        public static XmlReader ExecuteXmlReader(sqlTransaction transaction,string commandText)
        {
            //pass through the call providing null for the set of sqlParameters
            return ExecuteXmlReader(transaction,(sqlParameter[])null);
        }

@H_404_4@        /// <summary>
        /// Execute a sqlCommand (that returns a resultset) against the specified sqlTransaction
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.: 
        ///  XmlReader r = ExecuteXmlReader(trans,etc.)</param>
        /// <param name="commandText">the stored procedure name or T-sql command using "FOR XML AUTO"</param>
        /// <param name="commandParameters">an array of sqlParamters used to execute the command</param>
        /// <returns>an XmlReader containing the resultset generated by the command</returns>
        public static XmlReader ExecuteXmlReader(sqlTransaction transaction,so they can be used again.
            cmd.Parameters.Clear();
            return retval;

@H_404_4@        }

@H_404_4@ 

@H_404_4@        #endregion ExecuteXmlReader

@H_404_4@        #region CreatesqlConnection
        public static sqlConnection CreatesqlConnection(string connectionString)
        {
            //create & open a sqlConnection
            sqlConnection cn = new sqlConnection(connectionString);
            try
            {
                cn.open();
            }
            catch
            {
                //if we fail to return the sqlTransaction,we need to close the connection ourselves
                cn.Close();
                throw;
            }
            return cn;
        }

@H_404_4@
        public static sqlConnection CreatesqlConnection()
        {
            return CreatesqlConnection(ConfigurationManager.AppSettings["ConnString"]);
        }
        #endregion

@H_404_4@        #region ExecutesqlCommand

@H_404_4@        public static sqlCommand ExecutesqlCommand()
        {
            return ExecutesqlCommand("",ConfigurationManager.AppSettings["ConnString"]);
        }

@H_404_4@        public static sqlCommand ExecutesqlCommand(string CmdText)
        {
            return ExecutesqlCommand(CmdText,ConfigurationManager.AppSettings["ConnString"]);
        }

@H_404_4@        public static sqlCommand ExecutesqlCommand(sqlConnection cn)
        {
            if (cn.State == ConnectionState.Closed || cn.State == ConnectionState.broken)
                cn.open();
            try
            {
                return new sqlCommand("",cn);
            }
            catch
            {
                //if we fail to return the sqlCommand,we need to close the connection ourselves
                cn.Close();
                throw;
            }
        }

@H_404_4@        public static sqlCommand ExecutesqlCommand(string connectionString,string CmdText)
        {
            //create & open a sqlConnection
            sqlConnection cn = new sqlConnection(connectionString);
            cn.open();

@H_404_4@            try
            {
                return new sqlCommand(CmdText,we need to close the connection ourselves
                cn.Close();
                throw;
            }
        }

@H_404_4@        #endregion
    }

@H_404_4@}

 
附件二:ListBaseDataSet.cs
@H_404_4@ 

@H_404_4@using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using QuestionsDbSLModel;

@H_404_4@namespace QuestionsDbSL.Web.ClientBin
{
    /// <summary>
    /// 用于进行通用数据转化的类
    /// 将数据库中取出的数据通过 webserice传到SL端
    /// </summary>
    public class ListBaseDataSet
    {
        /// <summary>
        /// 按列的次序,将值依次转换到ListBaseVar中
        /// 按记录条数,将记录压到列表中
        /// </summary>
        /// <param name="p_DataSet"></param>
        /// <param name="p_TableIndex"></param>
        /// <returns></returns>
        public static List<BaseVars> DataSetToListBaseVars(DataSet p_DataSet,int p_TableIndex)
        {
            if (p_DataSet == null || p_DataSet.Tables.Count < 0)
                return null;
            if (p_TableIndex > p_DataSet.Tables.Count - 1)
                return null;
            if (p_TableIndex < 0)
                p_TableIndex = 0;

@H_404_4@            DataTable p_Data = p_DataSet.Tables[p_TableIndex];
            List<BaseVars> result = new List<BaseVars>();
            lock (p_Data)
            {  
                for (int j = 0; j < p_Data.Rows.Count; j++)
                {
                    BaseVars lbv = new BaseVars();
                    for (int k = 0; k < p_Data.Columns.Count; k++)
                    {
                        if (p_Data.Rows[j][k].GetType() == typeof(string))
                        {
                            lbv.ListString.Add(p_Data.Rows[j][k].ToString());
                        }
                        else if (p_Data.Rows[j][k].GetType() == typeof(int))
                        {
                            lbv.ListInt.Add((int)(p_Data.Rows[j][k]));
                        }
                        else if (p_Data.Rows[j][k].GetType() == typeof(bool))
                        {
                            lbv.ListBool.Add((bool)(p_Data.Rows[j][k]));
                        }
                        else if (p_Data.Rows[j][k].GetType() == typeof(byte))
                        {
                            lbv.ListByte.Add((byte)(p_Data.Rows[j][k]));
                        }
                        else if (p_Data.Rows[j][k].GetType() == typeof(float))
                        {
                            lbv.ListFloat.Add((float)(p_Data.Rows[j][k]));
                        }
                        else if (p_Data.Rows[j][k].GetType() == typeof(double))
                        {
                            lbv.ListDouble.Add((double)(p_Data.Rows[j][k]));
                        }
                    }

@H_404_4@                    result.Add(lbv);                }            }            return result;        }    }}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐