Beispiel #1
0
        /// <summary>
        /// Sets where statement used.
        /// </summary>
        /// <param name="format">A composite format string.</param>
        /// <param name="args">An System.Object array containing zero or more objects to format. Those objects are inserted in Parameters as IDbDataParameter with name of @Px where x is order index.</param>
        public void SetWhere(string format, params object[] args)
        {
            if (_whereParameters != null)
            {
                for (var i = 0; i < _whereParameters.Count; ++i)
                {
                    BaseCommand.Parameters.Remove(_whereParameters[i]);
                }
            }

            _needsMonoFix = false;
            var argList = new List <string>();

            if (args != null)
            {
                for (var i = 0; i < args.Length; ++i)
                {
                    if (args[i] == null)
                    {
                        argList.Add("NULL");
                    }
                    else
                    {
                        var paramName = string.Format(CultureInfo.InvariantCulture, "@W{0}", i);
                        argList.Add(paramName);
                        var param = BaseCommand.CreateParameter();
                        param.ParameterName = paramName;
                        if ((args[i] is DateTime) && (IsRunningOnMono))
                        {
                            args[i]       = ((DateTime)args[i]).ToString(CultureInfo.InvariantCulture);
                            _needsMonoFix = true;
                        }
                        param.Value = args[i];
                        if (param.DbType == DbType.DateTime)
                        {
                            if (param is OleDbParameter odp)
                            {
                                odp.OleDbType = OleDbType.Date;
                            }
                        }
                        _whereParameters.Add(param);
                        BaseCommand.Parameters.Add(param);
                    }
                }
            }
            if (argList.Count > 0)
            {
                _whereText = string.Format(CultureInfo.InvariantCulture, format, argList.ToArray());
            }
            else
            {
                _whereText = format;
            }

            UpdateCommandText();
        }
Beispiel #2
0
 /// <summary>
 /// Creates a new instance of an System.Data.IDbDataParameter object.
 /// </summary>
 public IDbDataParameter CreateParameter()
 {
     return(BaseCommand.CreateParameter());
 }
        private readonly bool NeedsMonoFix; //Mono bug #500987 / Error converting data type varchar to datetime


        /// <summary>
        /// Creates new instance.
        /// </summary>
        /// <param name="connection">A connection object.</param>
        /// <param name="tableName">Name of table.</param>
        /// <param name="columnsAndValues">Column names and values in alternating order (name1, value1, name2, value2).</param>
        /// <exception cref="System.ArgumentNullException">Connection cannot be null.</exception>
        /// <exception cref="System.ArgumentException">Table name cannot be empty or null.</exception>
        /// <exception cref="System.InvalidCastException">Column name should be string and non-null.</exception>
        public InsertCommand(IDbConnection connection, string tableName, params object[] columnsAndValues)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection", Resources.ExceptionConnectionCannotBeNull);
            }
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException(Resources.ExceptionTableNameCannotBeEmptyOrNull, "tableName");
            }
            if (columnsAndValues == null)
            {
                throw new ArgumentException(Resources.ExceptionNumberOfParametersMustBeMultipleOfTwo, "columnsAndValues");
            }
            if (columnsAndValues.Length % 2 != 0)
            {
                throw new ArgumentException(Resources.ExceptionNumberOfParametersMustBeMultipleOfTwo, "columnsAndValues");
            }

            BaseCommand = connection.CreateCommand();

            TableName = tableName;

            NeedsMonoFix = false;
            var sbColumns = new StringBuilder();
            var sbValues  = new StringBuilder();

            for (var i = 0; i < columnsAndValues.Length; i += 2)
            {
                if (!(columnsAndValues[i] is string name))
                {
                    throw new InvalidCastException(Resources.ExceptionColumnNameShouldBeStringAndNonNull);
                }
                var value = columnsAndValues[i + 1];

                if (sbColumns.Length > 0)
                {
                    sbColumns.Append(", ");
                }
                if (Connection is SqlConnection)
                {
                    sbColumns.Append("[" + name + "]");
                }
                else
                {
                    sbColumns.Append(name);
                }

                if (sbValues.Length > 0)
                {
                    sbValues.Append(", ");
                }
                if (value == null)
                {
                    sbValues.Append("NULL");
                }
                else
                {
                    var paramName = "@P" + (i / 2).ToString(CultureInfo.InvariantCulture);
                    sbValues.Append(paramName);
                    var param = BaseCommand.CreateParameter();
                    param.ParameterName = paramName;
                    if ((value is DateTime) && (IsRunningOnMono))
                    {
                        value        = ((DateTime)value).ToString(CultureInfo.InvariantCulture);
                        NeedsMonoFix = true;
                    }
                    param.Value = value;
                    if (param.DbType == DbType.DateTime)
                    {
                        if (param is OleDbParameter odp)
                        {
                            odp.OleDbType = OleDbType.Date;
                        }
                    }
                    BaseCommand.Parameters.Add(param);
                }
            }
            ColumnsText = sbColumns.ToString();
            ValuesText  = sbValues.ToString();

            UpdateCommandText();
        }