Esempio n. 1
0
        /// <summary>
        /// 准备数据库指令
        /// </summary>
        /// <param name="statement">上层指令</param>
        /// <returns>数据库指令</returns>
        private DbCommand PrepareCommand(Statement statement)
        {
            DbCommand command = m_DatabaseProvider.CreateCommand();

            command.CommandText    = statement.StatementText;
            command.CommandType    = statement.StatementType == StatementType.Sql ? CommandType.Text : CommandType.StoredProcedure;
            command.CommandTimeout = statement.Timeout;
            String providerName = m_DatabaseProvider.GetType().Name;

            foreach (var p in statement.Parameters)
            {
                if (p.ExtendType == 1)
                {
                    var parameter = (SqlParameter)command.CreateParameter();
                    parameter.ParameterName = m_DatabaseProvider.CreateParameterName(p.Name);
                    parameter.SqlDbType     = (SqlDbType)p.ExtendTypeValue;

                    if (!String.IsNullOrEmpty(p.TypeName))
                    {
                        parameter.TypeName = p.TypeName;
                    }
                    parameter.Size       = p.Size;
                    parameter.Value      = p.Value ?? DBNull.Value;
                    parameter.Direction  = p.Direction;
                    parameter.IsNullable = p.IsNullable;
                    command.Parameters.Add(parameter);
                }
                else
                {
                    if (p.DbType == DbType.Time && providerName == "SqlDatabaseProvider")
                    {
                        var parameter = (SqlParameter)command.CreateParameter();
                        parameter.ParameterName = m_DatabaseProvider.CreateParameterName(p.Name);
                        parameter.SqlDbType     = SqlDbType.Time;
                        parameter.Size          = p.Size;
                        parameter.Value         = p.Value ?? DBNull.Value;
                        parameter.Direction     = p.Direction;
                        parameter.IsNullable    = p.IsNullable;
                        command.Parameters.Add(parameter);
                    }
                    else
                    {
                        var parameter = command.CreateParameter();
                        parameter.ParameterName = m_DatabaseProvider.CreateParameterName(p.Name);
                        parameter.DbType        = p.DbType;
                        parameter.Size          = p.Size;
                        parameter.Value         = p.Value ?? DBNull.Value;
                        parameter.Direction     = p.Direction;
                        parameter.IsNullable    = p.IsNullable;

                        if (providerName.Equals("MySqlDatabaseProvider"))
                        {
                            command.Parameters.Insert(-1, parameter);   //work around for legacy mysql driver versions
                        }
                        else
                        {
                            command.Parameters.Add(parameter);
                        }
                    }
                }
            }

            return(command);
        }