Пример #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SWParameterAttribute"/> class with the specified
 /// <see cref="Name"/>, <see cref="SqlDbType"/>, <see cref="Size"/>, <see cref="Precision"/>,
 /// <see cref="Scale"/>, <see cref="TreatAsNull"/> and <see cref="ParameterType"/> values
 /// </summary>
 /// <param name="name">Is a value of <see cref="Name"/> property</param>
 /// <param name="sqlDbType">Is a value of <see cref="SqlDbType"/> property</param>
 /// <param name="size">Is a value of <see cref="Size"/> property</param>
 /// <param name="precision">Is a value of <see cref="Precision"/> property</param>
 /// <param name="scale">Is a value of <see cref="Scale"/> property</param>
 /// <param name="treatAsNull">Is a value of <see cref="TreatAsNull"/> property</param>
 /// <param name="parameterType">Is a value of <see cref="ParameterType"/> property</param>
 public SWParameterAttribute(string name, SqlDbType sqlDbType, int size, byte precision, byte scale, object treatAsNull, SWParameterType parameterType)
 {
     Name          = name;
     SqlDbType     = sqlDbType;
     Size          = size;
     Precision     = precision;
     Scale         = scale;
     TreatAsNull   = treatAsNull;
     ParameterType = parameterType;
 }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SWParameterAttribute"/> class
 /// </summary>
 /// <param name="parameterType">Is a value of <see cref="ParameterType"/> property</param>
 /// <param name="name">Is a value of <see cref="Name"/> property</param>
 public SWParameterAttribute(SWParameterType parameterType, string name)
 {
     ParameterType = parameterType;
     m_name        = name;
 }
Пример #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SWParameterAttribute"/> class
 /// </summary>
 /// <param name="parameterType">Is a value of <see cref="ParameterType"/> property</param>
 public SWParameterAttribute(SWParameterType parameterType)
 {
     ParameterType = parameterType;
 }
Пример #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SWParameterAttribute"/> class
 /// </summary>
 /// <param name="parameterType">Is a value of <see cref="ParameterType"/> property</param>
 /// <param name="size">Is a value of <see cref="Size"/> property</param>
 public SWParameterAttribute(SWParameterType parameterType, int size)
 {
     ParameterType = parameterType;
     Size          = size;
 }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SWParameterAttribute"/> class with the specified
 /// <see cref="Name"/>, <see cref="SqlDbType"/>, <see cref="Size"/>, <see cref="Precision"/>, 
 /// <see cref="Scale"/>, <see cref="TreatAsNull"/> and <see cref="ParameterType"/> values
 /// </summary>
 /// <param name="name">Is a value of <see cref="Name"/> property</param>
 /// <param name="sqlDbType">Is a value of <see cref="SqlDbType"/> property</param>
 /// <param name="size">Is a value of <see cref="Size"/> property</param>
 /// <param name="precision">Is a value of <see cref="Precision"/> property</param>
 /// <param name="scale">Is a value of <see cref="Scale"/> property</param>
 /// <param name="treatAsNull">Is a value of <see cref="TreatAsNull"/> property</param>
 /// <param name="parameterType">Is a value of <see cref="ParameterType"/> property</param>
 public SWParameterAttribute(string name, SqlDbType sqlDbType, int size, byte precision, byte scale, object treatAsNull, SWParameterType parameterType)
 {
     Name = name;
     SqlDbType = sqlDbType;
     Size = size;
     Precision = precision;
     Scale = scale;
     TreatAsNull = treatAsNull;
     ParameterType = parameterType;
 }
Пример #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SWParameterAttribute"/> class
 /// </summary>
 /// <param name="parameterType">Is a value of <see cref="ParameterType"/> property</param>
 /// <param name="name">Is a value of <see cref="Name"/> property</param>
 public SWParameterAttribute(SWParameterType parameterType, string name)
 {
     ParameterType = parameterType;
     m_name = name;
 }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SWParameterAttribute"/> class
 /// </summary>
 /// <param name="parameterType">Is a value of <see cref="ParameterType"/> property</param>
 /// <param name="size">Is a value of <see cref="Size"/> property</param>
 public SWParameterAttribute(SWParameterType parameterType, int size)
 {
     ParameterType = parameterType;
     Size = size;
 }
Пример #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SWParameterAttribute"/> class
 /// </summary>
 /// <param name="parameterType">Is a value of <see cref="ParameterType"/> property</param>
 public SWParameterAttribute(SWParameterType parameterType)
 {
     ParameterType = parameterType;
 }
Пример #9
0
        /// <summary>
        /// Generates command parameters.
        /// For some command types the command text can be changed during parameter generating.
        /// </summary>
        /// <param name="command">Command object.</param>
        /// <param name="method"><see cref="MethodInfo"/> type object</param>
        /// <param name="values">Array of values for the command parameters.</param>
        /// <param name="swCommandType"><see cref="SWCommandType"/> enumeration value</param>
        /// <param name="indexes">Array of parameter indices.</param>
        private static void GenerateCommandParameters(DbCommand command, MethodInfo method, object[] values, int[] indexes, SWCommandType swCommandType)
        {
            #region InsertUpdate parts declaration
            string sUpdatePart1             = "";
            string sUpdatePart2             = "";
            string sUpdate                  = "";
            string sInsertPart1             = "";
            string sInsertPart2             = "";
            string sInsert                  = "";
            string sAutoincrementColumnName = "";
            #endregion


            //DatabaseHelper db = new DatabaseHelper();
            ParameterInfo[] methodParameters = method.GetParameters();

            int sqlParamIndex = 0;
            for (int paramIndex = 0; paramIndex < methodParameters.Length; ++paramIndex)
            {
                indexes[paramIndex] = -1;
                ParameterInfo paramInfo = methodParameters[paramIndex];

                // create command parameter
                DbParameter sqlParameter = command.CreateParameter();

                // set default values
                string          paramName     = paramInfo.Name;
                SWParameterType paramCustType = SWParameterType.Default;
                object          v             = values[paramIndex];

                // get parameter attribute and set command parameter settings
                SWParameterAttribute paramAttribute = (SWParameterAttribute)Attribute.GetCustomAttribute(paramInfo, typeof(SWParameterAttribute));
                if (paramAttribute != null)
                {
                    paramCustType = paramAttribute.ParameterType;

                    if (paramAttribute.IsNameDefined)
                    {
                        paramName = sqlParameter.ParameterName;
                    }

                    if (paramAttribute.IsTypeDefined)
                    {
                        sqlParameter.DbType = (DbType)paramAttribute.SqlDbType;
                    }

                    if (paramAttribute.IsSizeDefined)
                    {
                        sqlParameter.Size = paramAttribute.Size;
                    }

                    //if (paramAttribute.IsScaleDefined)
                    //	sqlParameter.Scale = paramAttribute.Scale;

                    //if (paramAttribute.IsPrecisionDefined)
                    //	sqlParameter..Precision = paramAttribute.Precision;

                    if (CompareTreatAsNullValues(paramAttribute.TreatAsNull, v, paramInfo.ParameterType))
                    {
                        v = DBNull.Value;
                    }
                }


                // parameter direction
                if (paramCustType == SWParameterType.SPReturnValue)
                {
                    sqlParameter.Direction = ParameterDirection.ReturnValue;
                    sqlParameter.DbType    = DbType.Int32;
                }
                else if (paramInfo.ParameterType.IsByRef)
                {
                    sqlParameter.Direction = paramInfo.IsOut ? ParameterDirection.Output : ParameterDirection.InputOutput;
                }
                else
                {
                    sqlParameter.Direction = ParameterDirection.Input;
                }

                // generate parts of InsertUpdate expresion
                #region generate parts of InsertUpdate expresion
                if (paramCustType == SWParameterType.Identity)
                {
                    if (sAutoincrementColumnName.Length > 0)
                    {
                        throw new SqlWrapperException("Only one identity parameter is possible");
                    }
                    sAutoincrementColumnName = paramName;
                    Type reftype = GetRefType(paramInfo.ParameterType);
                    if (reftype == null)
                    {
                        throw new SqlWrapperException("Identity parameter must be ByRef parameter");
                    }

                    // check default value
                    if (paramAttribute.TreatAsNull.ToString() == SWParameterAttribute.NullReturnValueToken)
                    {
                        if (Convert.ToInt64(v) <= 0)
                        {
                            v = DBNull.Value;
                        }
                    }
                }

                if (swCommandType == SWCommandType.InsertUpdate)
                {
                    string fieldName    = "[" + paramName + "]";
                    string cmdparamName = "@" + paramName;

                    if (paramCustType != SWParameterType.Identity)
                    {
                        sInsertPart1 = AddWithDelim(sInsertPart1, ", ", fieldName);
                        sInsertPart2 = AddWithDelim(sInsertPart2, ", ", cmdparamName);
                    }
                    if ((paramCustType == SWParameterType.Key) || (paramCustType == SWParameterType.Identity))
                    {
                        sUpdatePart2 = AddWithDelim(sUpdatePart2, " and ", fieldName + "=" + cmdparamName);
                    }
                    if (paramCustType != SWParameterType.Identity)
                    {
                        sUpdatePart1 = AddWithDelim(sUpdatePart1, ", ", fieldName + "=" + cmdparamName);
                    }
                }
                #endregion

                // set parameter name
                sqlParameter.ParameterName = "@" + paramName;


                // set parameter value
                if (v == null)
                {
                    v = DBNull.Value;
                }
                sqlParameter.Value = values[paramIndex];                // this is to set a proper data type
                sqlParameter.Value = v;

                // add parameter to the command object
                command.Parameters.Add(sqlParameter);
                indexes[paramIndex] = sqlParamIndex;
                sqlParamIndex++;
            }

            // in case of InsertUpdate command type compile new command text
            #region generate InsertUpdate expresion
            if (swCommandType == SWCommandType.InsertUpdate)
            {
                string TableName   = command.CommandText;
                string CommandText = "";

                if (sUpdatePart2 == "")
                {
                    throw new SqlWrapperException("No Identity or Autoincrement field is defined.");
                }

                sInsert = String.Format(" insert into [{0}]({1}) values({2}) ", TableName, sInsertPart1, sInsertPart2);
                sUpdate = String.Format(" update [{0}] set {1} where {2} ", TableName, sUpdatePart1, sUpdatePart2);

                if (sAutoincrementColumnName == "")
                {
                    CommandText += String.Format("{0} if (@@rowcount = 0) {1}", sUpdate, sInsert);
                }
                else
                {
                    CommandText += String.Format("if(@{0} is NULL) begin {1} select @{0} = SCOPE_IDENTITY() end ", sAutoincrementColumnName, sInsert);
                    CommandText += String.Format("else begin {0} end", sUpdate);
                }

                command.CommandText = CommandText;
            }
            #endregion
        }