示例#1
0
        //public static void CheckStringPara(string SQL,string tableName, IDataParameter[] paras,string parameterChar)
        //{
        //    for (int index = 0; index < paras.Length; index++)
        //    {
        //        //为字符串类型的参数指定长度 edit at 2012.4.23
        //        if (paras[index].Value != null && paras[index].Value.GetType() == typeof(string))
        //        {
        //            string field = FindFieldNameInSql(SQL, paras[index].ParameterName, parameterChar);
        //            ((IDbDataParameter)paras[index]).Size = EntityBase.GetStringFieldSize(tableName, field);
        //        }
        //    }
        //}

        /// <summary>
        /// 使用SQL查询命令信息 执行数据阅读器查询。注意,查询单行数据不支持存储过程。
        /// </summary>
        /// <param name="si">SQL查询命令信息</param>
        /// <param name="db">数据访问对象</param>
        /// <param name="single">是否仅查询单行数据</param>
        /// <returns>数据阅读器</returns>
        public static IDataReader ExecuteDataReader(SqlInfo si, AdoHelper db, bool single)
        {
            IDataReader reader = null;

            if (si.Parameters != null && si.Parameters.Count > 0)
            {
                IDataParameter[] paras = GetParameters(si.Parameters, db);

                if (single)
                {
                    reader = db.ExecuteDataReaderWithSingleRow(si.SQL, paras);
                }
                else
                {
                    reader = db.ExecuteDataReader(si.SQL, si.CommandType, paras);
                }
            }
            else
            {
                if (single)
                {
                    reader = db.ExecuteDataReaderWithSingleRow(si.SQL);
                }
                else
                {
                    reader = db.ExecuteDataReader(si.SQL, si.CommandType, null);
                }
            }
            return(reader);
        }
示例#2
0
        /// <summary>
        ///  根据OQL查询数据获得DataReader。如果指定single=真,将执行优化的查询以获取单条记录
        /// </summary>
        /// <param name="oql">OQL表达式</param>
        /// <param name="db">当前数据库访问对象</param>
        /// <param name="factEntityType">实体类类型</param>
        /// <param name="single">是否只查询一条记录</param>
        /// <returns>DataReader</returns>
        public static IDataReader ExecuteDataReader(OQL oql, AdoHelper db, Type factEntityType, bool single)
        {
            string sql = "";
            Dictionary <string, object> Parameters = null;

            //处理用户查询映射的实体类
            if (oql.EntityMap == PWMIS.Common.EntityMapType.SqlMap)
            {
                if (CommonUtil.CacheEntityMapSql == null)
                {
                    CommonUtil.CacheEntityMapSql = new Dictionary <string, string>();
                }
                if (!CommonUtil.CacheEntityMapSql.ContainsKey(oql.sql_table))
                {
                    string tempView = GetMapSql(factEntityType);
                    CommonUtil.CacheEntityMapSql.Add(oql.sql_table, tempView);
                }
                sql = oql.GetMapSQL(CommonUtil.CacheEntityMapSql[oql.sql_table]);


                //如果用户本身没有初始化参数对象,则这里声明一个 edit at 2012.11.16
                Parameters = oql.InitParameters ?? new Dictionary <string, object>();
                if (oql.Parameters != null && oql.Parameters.Count > 0)
                {
                    foreach (string name in oql.Parameters.Keys)
                    {
                        Parameters.Add(name, oql.Parameters[name]);
                    }
                }
            }
            else if (oql.EntityMap == PWMIS.Common.EntityMapType.StoredProcedure)
            {
                string script = "";
                if (CommonUtil.CacheEntityMapSql == null)
                {
                    CommonUtil.CacheEntityMapSql = new Dictionary <string, string>();
                }
                //获取SQL-MAP脚本
                if (CommonUtil.CacheEntityMapSql.ContainsKey(oql.sql_table))
                {
                    script = CommonUtil.CacheEntityMapSql[oql.sql_table];
                }
                else
                {
                    script = GetMapSql(factEntityType);
                    CommonUtil.CacheEntityMapSql.Add(oql.sql_table, script);
                }
                //对SQL-MAP格式的参数进行解析
                SqlMap.SqlMapper mapper = new PWMIS.DataMap.SqlMap.SqlMapper();
                mapper.DataBase = db;
                //解析存储过程名称
                sql = mapper.FindWords(mapper.GetScriptInfo(script), 0, 255); //由于是存储过程,需要特殊处理,调用 FindWords方法
                //解析参数
                IDataParameter[] paras = mapper.GetParameters(script);
                if (oql.InitParameters != null && oql.InitParameters.Count > 0)
                {
                    try
                    {
                        foreach (IDataParameter para in paras)
                        {
                            string key = para.ParameterName.TrimStart(db.GetParameterChar.ToCharArray());
                            para.Value = oql.InitParameters[key];
                        }
                    }
                    catch (KeyNotFoundException exKey)
                    {
                        throw new KeyNotFoundException("'存储过程实体类'的初始化参数中没有找到指定的参数名,请检查参数定义和设置。", exKey);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    if (paras.Length > 0)
                    {
                        throw new Exception("当前'存储过程实体类'需要提供初始化参数,请设置OQL对象的InitParameters属性");
                    }
                }

                return(db.ExecuteDataReader(sql, CommandType.StoredProcedure, paras));
            }
            else
            {
                //edit at 2013-2-25 处理非SQLSERVER的参数名前缀问题
                if (db is SqlServer)
                {
                    sql = oql.ToString();
                }
                else
                {
                    sql = oql.ToString().Replace("@", db.GetParameterChar);
                }
                Parameters = oql.Parameters;
            }


            //处理实体类分页 2010.6.20
            if (oql.PageEnable && !single)
            {
                switch (db.CurrentDBMSType)
                {
                case PWMIS.Common.DBMSType.Access:
                case PWMIS.Common.DBMSType.SqlServer:
                case PWMIS.Common.DBMSType.SqlServerCe:
                    //如果含有Order By 子句,则不能使用主键分页
                    if (oql.HaveJoinOpt || sql.IndexOf("order by", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.SqlServer, sql, "", oql.PageSize, oql.PageNumber, oql.PageWithAllRecordCount);
                    }
                    else
                    {
                        if (oql.PageOrderDesc)
                        {
                            sql = PWMIS.Common.SQLPage.GetDescPageSQLbyPrimaryKey(oql.PageNumber, oql.PageSize, oql.sql_fields, oql.sql_table, oql.PageField, oql.sql_condition);
                        }
                        else
                        {
                            sql = PWMIS.Common.SQLPage.GetAscPageSQLbyPrimaryKey(oql.PageNumber, oql.PageSize, oql.sql_fields, oql.sql_table, oql.PageField, oql.sql_condition);
                        }
                    }
                    break;

                //case PWMIS.Common.DBMSType.Oracle:
                //    sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.Oracle, sql, "", oql.PageSize, oql.PageNumber, oql.PageWithAllRecordCount);
                //    break ;
                //case PWMIS.Common.DBMSType.MySql:
                //case PWMIS.Common.DBMSType.SQLite:
                //    sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.MySql, sql, "", oql.PageSize, oql.PageNumber, oql.PageWithAllRecordCount);
                //    break;
                //case PWMIS.Common.DBMSType.PostgreSQL:
                //    sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.PostgreSQL, sql, "", oql.PageSize, oql.PageNumber, oql.PageWithAllRecordCount);
                //    break;
                default:
                    sql = PWMIS.Common.SQLPage.MakeSQLStringByPage(db.CurrentDBMSType, sql, "", oql.PageSize, oql.PageNumber, oql.PageWithAllRecordCount);
                    break;
                }
            }

            IDataReader reader = null;

            if (Parameters != null && Parameters.Count > 0)
            {
                int fieldCount         = Parameters.Count;
                IDataParameter[] paras = new IDataParameter[fieldCount];
                int index = 0;

                foreach (string name in Parameters.Keys)
                {
                    paras[index] = db.GetParameter(name, Parameters[name]);
                    //为字符串类型的参数指定长度 edit at 2012.4.23
                    if (paras[index].Value != null && paras[index].Value.GetType() == typeof(string))
                    {
                        string field = FindFieldNameInSql(sql, name, db.GetParameterChar);
                        ((IDbDataParameter)paras[index]).Size = EntityBase.GetStringFieldSize(oql.sql_table, field);
                    }
                    index++;
                }
                if (single)
                {
                    reader = db.ExecuteDataReaderWithSingleRow(sql, paras);
                }
                else
                {
                    reader = db.ExecuteDataReader(sql, CommandType.Text, paras);
                }
            }
            else
            {
                if (single)
                {
                    reader = db.ExecuteDataReaderWithSingleRow(sql);
                }
                else
                {
                    reader = db.ExecuteDataReader(sql);
                }
            }
            return(reader);
        }