/// <summary>
 /// 找出 <paramref name="command"/> 脚本中定义的参数列表,并将参数列表加入其属性 Parameters 中。
 /// </summary>
 /// <param name="database">表示一个 <see cref="Microsoft.Practices.EnterpriseLibrary.Data.Database"/> 对象。</param>
 /// <param name="command">用于查找参数列表的 <see cref="DbCommand"/> 对象。</param>
 public static void DiscoverParameters(Database database, DbCommand command)
 {
     if (command.CommandType == CommandType.StoredProcedure)
     {
         database.DiscoverParameters(command);
         return;
     }
     Check.NotEmpty(command.CommandText, "command.CommandText");
     var parser = NDF.Data.Common.DbProviderFactoryExtensions.GetScriptParameterParser(database.DbProviderFactory);
     string[] parameterNames = parser.GetParameterNames(command.CommandText);
     foreach (var paremeterName in parameterNames)
     {
         DbParameter parameter = CreateParameter(database, paremeterName);
         command.Parameters.Add(parameter);
     }
 }
예제 #2
0
        /// <summary>
        /// <para>
        /// Populates the parameter collection for a command wrapper from the cache 
        /// or performs a round-trip to the database to query the parameters.
        /// </para>
        /// </summary>
        /// <param name="command">
        /// <para>The command to add the parameters.</para>
        /// </param>
        /// <param name="database">
        /// <para>The database to use to set the parameters.</para>
        /// </param>
        public void SetParameters(DbCommand command, Database database)
        {
            if (command == null) throw new ArgumentNullException("command");
            if (database == null) throw new ArgumentNullException("database");

            if (AlreadyCached(command, database))
            {
                AddParametersFromCache(command, database);
            }
            else
            {
                database.DiscoverParameters(command);
                IDataParameter[] copyOfParameters = CreateParameterCopy(command);

                this.cache.AddParameterSetToCache(database.ConnectionString, command, copyOfParameters);
            }
        }
예제 #3
0
파일: Data.cs 프로젝트: wonkim00/Core
        private static DbCommand PrepCommand(Database db, string procName, params object[] parameterVals)
        {
            DbCommand dbCommand = db.GetStoredProcCommand(procName);
            dbCommand.CommandTimeout = 180;
            db.DiscoverParameters(dbCommand);

            int numVals = parameterVals.Length;
            /// Start at 1; skip 0 which is RETURN_VALUE.
            for (int i = 1; i < dbCommand.Parameters.Count; i++)
            {
                /// parameterVals should have same # elements, but index is offset by 1 compared to dbCommand.Parameters.
                int valIndex = i - 1;
                object value = valIndex < numVals ? parameterVals[valIndex] : null;
                db.SetParameterValue(dbCommand, dbCommand.Parameters[i].ParameterName, value);
            }

            return dbCommand;
        }
예제 #4
0
 public void SetParameters(DbCommand command, Database database)
 {
     if (command == null)
     {
         throw new ArgumentNullException(nameof(command));
     }
     if (database == null)
     {
         throw new ArgumentNullException(nameof(database));
     }
     if (this.AlreadyCached((IDbCommand)command, database))
     {
         this.AddParametersFromCache(command, database);
     }
     else
     {
         database.DiscoverParameters(command);
         IDataParameter[] parameterCopy = ParameterCache.CreateParameterCopy(command);
         this.cache.AddParameterSetToCache(database.ConnectionString, (IDbCommand)command, parameterCopy);
     }
 }
예제 #5
0
 public void SetParameters(DbCommand command, Database database)
 {
     if (command == null)
     {
         throw new ArgumentNullException("command");
     }
     if (database == null)
     {
         throw new ArgumentNullException("database");
     }
     if (AlreadyCached(command, database))
     {
         AddParametersFromCache(command, database);
     }
     else
     {
         database.DiscoverParameters(command);
         IDataParameter[] copyOfParameters = CreateParameterCopy(command);
         this.cache.AddParameterSetToCache(database.ConnectionString, command, copyOfParameters);
     }
 }
예제 #6
0
 public DbCommand CreateSQLCommand(Database objDB, string SPName)
 {
     DbCommand objCommand = null;
     try
     {
         if (SPName.Trim().Length > 0)
         {
             //Create Command object
             objCommand = objDB.GetStoredProcCommand(SPName);
             //Derive Parameters of SP
             objDB.DiscoverParameters(objCommand);
             // Assign the provided source columns to these parameters based on parameter order
             for (int index = 0; index < objCommand.Parameters.Count; index++)
                 objCommand.Parameters[index].SourceColumn = objCommand.Parameters[index].ParameterName.Substring(1, objCommand.Parameters[index].ParameterName.Length - 1);
         }
         else
         {
             throw new Exception("Stored Procedure Name cannot be empty.");
         }
     }
     catch (Exception ex)
     {
         ExceptionManager.Publish(ex);
         throw ex;
     }
     return objCommand;
 }