Esempio n. 1
0
        public string RunProcedure(int ProcedureType, long SessionID, bool IsNeedSession, string strLoginName, byte[] bSerializeValue, byte[] bSerializeDataType,
                                   out string strOut, out string ErrorMsg, out bool bolIsError)
        {
            strOut = "";
            DataTable dtOut = null;

            bolIsError = false;
            DataSet dsReturn = null;

            ErrorMsg = "";
            try
            {
                string strConn = GetConnectionStr();
                SQLServerDAL.GetConnectionString = strConn;

                DataTable dtParmValue = new DataTable("SPIN");
                List <Dictionary <object, object> > lstDictValue = DeserializeObject(bSerializeValue) as List <Dictionary <object, object> >;
                Dictionary <object, object>         dictDataType = DeserializeObject(bSerializeDataType) as Dictionary <object, object>;

                foreach (KeyValuePair <object, object> keyvalue in dictDataType)
                {
                    dtParmValue.Columns.Add(keyvalue.Key.ToString(), GetType(keyvalue.Value.ToString()));
                }

                foreach (Dictionary <object, object> dictValue in lstDictValue)
                {
                    DataRow drNew = dtParmValue.NewRow();
                    foreach (KeyValuePair <object, object> keyvalue in dictValue)
                    {
                        drNew[keyvalue.Key.ToString()] = keyvalue.Value;
                    }
                    dtParmValue.Rows.Add(drNew);
                }
                dtParmValue.AcceptChanges();

                DBHelper.Provider = new DBMSSQL();
                SqlConnection con       = new SqlConnection(SQLServerDAL.GetConnectionString);
                string        strDBName = con.Database;
                DBMSSQL.InitSettings(5000, con.DataSource, strDBName, true, "", "");
                con.Close();

                //LBFactory factory = new LBFactory();
                //IBLLFunction function = factory.GetAssemblyFunction(ProcedureType);
                IBLLFunction function = DBHelper.GetFunctionMethod(ProcedureType);
                if (function == null)
                {
                    #region -- 调用存储过程 --

                    DataTable dtView = SQLServerDAL.Query("select * from dbo.SysSPType where SysSPType=" + ProcedureType);
                    if (dtView.Rows.Count > 0)
                    {
                        DataRow drView       = dtView.Rows[0];
                        string  strSysSPName = drView["SysSPName"].ToString().TrimEnd();
                        SQLServerDAL.ExecuteProcedure(strSysSPName, dtParmValue, out dtOut, out dsReturn);
                    }
                    else
                    {
                        throw new Exception("存储过程号【" + ProcedureType + "】不存在!");
                    }

                    #endregion
                }
                else
                {
                    #region -- 调用中间层程序方法 --

                    string strMethod = function.GetFunctionName(ProcedureType);
                    string str       = function.ToString();

                    string   strLoad = str.Substring(0, str.IndexOf('.', 8));
                    Assembly s       = Assembly.Load(strLoad);
                    Type     tpe     = s.GetType(str);


                    //调用GetName方法
                    MethodInfo method = tpe.GetMethod(strMethod);

                    Dictionary <string, string> dictParmType   = new Dictionary <string, string>();
                    ParameterInfo[]             parameterInfos = method.GetParameters();
                    foreach (ParameterInfo parmInfo in parameterInfos)
                    {
                        if (parmInfo.ParameterType.Name != "FactoryArgs")
                        {
                            string strParmTypeName = parmInfo.ParameterType.Name.Replace("&", "");
                            if (!dictParmType.ContainsKey(strParmTypeName))
                            {
                                dictParmType.Add(parmInfo.Name, strParmTypeName);
                            }
                        }
                    }

                    int iRowIndex = 0;

                    if (dtParmValue == null || dtParmValue.Rows.Count == 0)
                    {
                        return(null);
                    }

                    FactoryArgs factoryArgs = new FactoryArgs(strDBName, strLoginName, SessionID, IsNeedSession, null, null);

                    #region -- 校验Session --

                    if (factoryArgs.SessionID > 0)
                    {
                        bool bolExistsSession = SessionTimer.VerifySession(factoryArgs);
                        if (IsNeedSession && !bolExistsSession)
                        {
                            bolIsError = true;
                            ErrorMsg   = "长时间未操作或者被其他人逼退,请重新登录!";
                            return("");
                        }
                    }

                    #endregion -- 校验Session --

                    foreach (DataRow drParmValue in dtParmValue.Rows)
                    {
                        //获取需要传入的参数
                        ParameterInfo[] parms = method.GetParameters();

                        Dictionary <int, string> dictOutFieldName = new Dictionary <int, string>();
                        object[] objValue   = new object[parms.Length];
                        int      iParmIndex = 0;
                        foreach (ParameterInfo ss in parms)
                        {
                            string strParmName = ss.Name;
                            if (ss.ParameterType == typeof(FactoryArgs))
                            {
                                objValue[iParmIndex] = factoryArgs;
                            }
                            else if (ss.Attributes != ParameterAttributes.Out)
                            {
                                if (dtParmValue.Columns.Contains(strParmName))
                                {
                                    DataColumn dc    = dtParmValue.Columns[strParmName];
                                    object     value = null;
                                    if (dc.DataType == typeof(long) ||
                                        dc.DataType == typeof(decimal) ||
                                        dc.DataType == typeof(float) ||
                                        dc.DataType == typeof(double) ||
                                        dc.DataType == typeof(int) ||
                                        dc.DataType == typeof(byte) ||
                                        dc.DataType == typeof(bool))
                                    {
                                        if (drParmValue[strParmName] == DBNull.Value)
                                        {
                                            if (dictParmType.ContainsKey(strParmName))
                                            {
                                                ILBDbType lbType = LBDBType.GetILBDbType(dictParmType[strParmName]);
                                                value = lbType;
                                            }
                                            else
                                            {
                                                value = new t_Decimal();
                                            }
                                        }
                                        else
                                        {
                                            if (dictParmType.ContainsKey(strParmName))
                                            {
                                                ILBDbType lbType = LBDBType.GetILBDbType(dictParmType[strParmName]);
                                                lbType.SetValueWithObject(drParmValue[strParmName]);
                                                value = lbType;
                                            }
                                            else
                                            {
                                                value = new t_Decimal(drParmValue[strParmName]);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (dictParmType.ContainsKey(strParmName))
                                        {
                                            ILBDbType lbType = LBDBType.GetILBDbType(dictParmType[strParmName]);
                                            lbType.SetValueWithObject(drParmValue[strParmName]);
                                            value = lbType;
                                        }
                                        else
                                        {
                                            value = new t_String(drParmValue[strParmName]);
                                        }
                                    }
                                    objValue[iParmIndex] = value;
                                }
                            }
                            else
                            {
                                if (dictParmType.ContainsKey(strParmName))
                                {
                                    ILBDbType lbType = LBDBType.GetILBDbType(dictParmType[strParmName]);
                                    lbType.SetValueWithObject(null);
                                    objValue[iParmIndex] = lbType;
                                }

                                if (dtOut == null)
                                {
                                    dtOut = new DataTable("Out");
                                }
                                if (!dtOut.Columns.Contains(strParmName))
                                {
                                    dtOut.Columns.Add(strParmName, typeof(object));
                                }
                                dictOutFieldName.Add(iParmIndex, strParmName);
                            }

                            iParmIndex++;
                        }

                        if (dtOut != null)
                        {
                            dtOut.Rows.Add(dtOut.NewRow());
                        }

                        //获取Car对象
                        object obj = s.CreateInstance(str);

                        //如果有返回值接收下
                        method.Invoke(obj, objValue);
                        int iobjReturnIndex = 0;
                        foreach (object objReturn in objValue)
                        {
                            if (objReturn is FactoryArgs)
                            {
                                FactoryArgs args = (FactoryArgs)objReturn;
                                if (args.SelectResult != null)
                                {
                                    if (dsReturn == null)
                                    {
                                        dsReturn = new DataSet("DSResult");
                                    }
                                    args.SelectResult.TableName = "Return" + iRowIndex.ToString();
                                    dsReturn.Tables.Add(args.SelectResult.Copy());
                                }
                            }
                            if (dictOutFieldName.ContainsKey(iobjReturnIndex))
                            {
                                if (objReturn is ILBDbType)
                                {
                                    ILBDbType lbtype = objReturn as ILBDbType;
                                    if (lbtype.Value != null)
                                    {
                                        dtOut.Rows[0][dictOutFieldName[iobjReturnIndex]] = lbtype.Value;
                                    }
                                }
                            }
                            iobjReturnIndex++;
                        }
                        iRowIndex++;
                    }

                    #endregion -- 调用中间层程序方法 --
                }

                strOut = RarDataTable(dtOut);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    ErrorMsg = ex.InnerException.Message;
                }
                else
                {
                    ErrorMsg = ex.Message;
                }
                bolIsError = true;
                return("");
            }
            return(RarDataSet(dsReturn));
        }
        public DataSet RunProcedure(int ProcedureType, string strLoginName, DataTable dtParmValue,
                                    out DataTable dtOut, out string ErrorMsg, out bool bolIsError)
        {
            dtOut      = null;
            bolIsError = false;
            DataSet dsReturn = null;

            ErrorMsg = "";
            try
            {
                LBFactory    factory  = new LBFactory();
                IBLLFunction function = factory.GetAssemblyFunction(ProcedureType);

                if (function == null)
                {
                    #region -- 调用存储过程 --

                    DataTable dtView = SQLServerDAL.Query("select * from dbo.SysSPType where SysSPType=" + ProcedureType);
                    if (dtView.Rows.Count > 0)
                    {
                        DataRow drView       = dtView.Rows[0];
                        string  strSysSPName = drView["SysSPName"].ToString().TrimEnd();
                        SQLServerDAL.ExecuteProcedure(strSysSPName, dtParmValue, out dtOut, out dsReturn);
                    }
                    else
                    {
                        throw new Exception("存储过程号【" + ProcedureType + "】不存在!");
                    }

                    #endregion
                }
                else
                {
                    #region -- 调用中间层程序方法 --


                    string strMethod = function.GetFunctionName(ProcedureType);
                    string str       = function.ToString();

                    Assembly s   = Assembly.Load("LB.Web.Project");
                    Type     tpe = s.GetType(str);


                    //调用GetName方法
                    MethodInfo method    = tpe.GetMethod(strMethod);
                    int        iRowIndex = 0;
                    foreach (DataRow drParmValue in dtParmValue.Rows)
                    {
                        //获取需要传入的参数
                        ParameterInfo[] parms = method.GetParameters();

                        FactoryArgs factoryArgs = new FactoryArgs(strLoginName);
                        Dictionary <int, string> dictOutFieldName = new Dictionary <int, string>();
                        object[] objValue   = new object[parms.Length];
                        int      iParmIndex = 0;
                        foreach (ParameterInfo ss in parms)
                        {
                            string strParmName = ss.Name;
                            if (ss.ParameterType == typeof(FactoryArgs))
                            {
                                objValue[iParmIndex] = factoryArgs;
                            }
                            else if (ss.Attributes != ParameterAttributes.Out)
                            {
                                if (dtParmValue.Columns.Contains(strParmName))
                                {
                                    objValue[iParmIndex] = drParmValue[strParmName];
                                }
                            }
                            else
                            {
                                if (dtOut == null)
                                {
                                    dtOut = new DataTable("Out");
                                }
                                if (!dtOut.Columns.Contains(strParmName))
                                {
                                    dtOut.Columns.Add(strParmName, typeof(object));
                                }
                                dictOutFieldName.Add(iParmIndex, strParmName);
                            }

                            iParmIndex++;
                        }

                        if (dtOut != null)
                        {
                            dtOut.Rows.Add(dtOut.NewRow());
                        }

                        //获取Car对象
                        object obj = s.CreateInstance(str);

                        //如果有返回值接收下
                        method.Invoke(obj, objValue);
                        int iobjReturnIndex = 0;
                        foreach (object objReturn in objValue)
                        {
                            if (objReturn is FactoryArgs)
                            {
                                FactoryArgs args = (FactoryArgs)objReturn;
                                if (args.SelectResult != null)
                                {
                                    if (dsReturn == null)
                                    {
                                        dsReturn = new DataSet("DSResult");
                                    }
                                    args.SelectResult.TableName = "Return" + iRowIndex.ToString();
                                    dsReturn.Tables.Add(args.SelectResult.Copy());
                                }
                            }
                            if (dictOutFieldName.ContainsKey(iobjReturnIndex))
                            {
                                dtOut.Rows[0][dictOutFieldName[iobjReturnIndex]] = objReturn;
                            }
                            iobjReturnIndex++;
                        }
                        iRowIndex++;
                    }

                    #endregion -- 调用中间层程序方法 --
                }
            }
            catch (Exception ex)
            {
                ErrorMsg   = ex.InnerException.Message;
                bolIsError = true;
            }
            return(dsReturn);
        }