Beispiel #1
0
        public static Boolean AddParameter(SqlCommand parCMD, object obj, string parPropertyName)
        {
            Boolean blnResult = false;
            //TTAttributs MyAttributs = (TTAttributs)obj.GetType().GetProperty(parPropertyName).GetCustomAttribute(typeof(TTAttributs), false);
            TTAttributs MyAttributs = HelperMethod.GetTTAttributes(obj, parPropertyName);
            object      objValue    = HelperMethod.GetPropValue(obj, parPropertyName);

            if (MyAttributs.ParamaterDataType == SqlDbType.NVarChar)
            {
                int parFieldWidth = 50;
                if (MyAttributs.isMemoField == false)
                {
                    parFieldWidth = ((MaxLengthAttribute)(obj.GetType().GetProperty(parPropertyName).GetCustomAttributes(typeof(MaxLengthAttribute), true)[0])).Length;
                    parCMD.Parameters.Add("@" + MyAttributs.FieldName, MyAttributs.ParamaterDataType, parFieldWidth).Value = objValue == null ? string.Empty : objValue;
                }
                else
                {
                    parFieldWidth = objValue == null ? 0 : objValue.ToString().Length;
                    parCMD.Parameters.Add("@" + MyAttributs.FieldName, MyAttributs.ParamaterDataType, parFieldWidth).Value = objValue == null ? string.Empty : objValue;
                }
            }
            else
            {
                parCMD.Parameters.Add("@" + MyAttributs.FieldName, MyAttributs.ParamaterDataType).Value = objValue;
            }
            return(blnResult);
        }
Beispiel #2
0
        public static String PrepairInsertQueryByEntity(object Entity, string parTableName)
        {
            string strQueryResult = "";

            string        strFields = "";;
            string        strValues = "";
            List <string> fieldlist = new List <string>();
            List <object> valuelist = new List <object>();
            List <string> lstParam  = new List <string>();

            PropertyInfo[] properties = Entity.GetType().GetProperties();

            for (int i = 0; i < properties.Count(); i++)
            {
                var el = properties.ElementAt(i);
                if (el != null)
                {
                    TTAttributs tt = HelperMethod.GetTTAttributes(Entity, el.Name);
                    if (tt.isTableField == true)
                    {
                        fieldlist.Add(tt.FieldName);

                        object objValue = HelperMethod.GetPropValue(Entity, tt.FieldName);

                        if (tt.ParamaterDataType == SqlDbType.NVarChar || tt.ParamaterDataType == SqlDbType.UniqueIdentifier || tt.ParamaterDataType == SqlDbType.DateTime || tt.ParamaterDataType == SqlDbType.Date)
                        {
                            valuelist.Add("'" + objValue + "'");
                        }
                        else if (tt.ParamaterDataType == SqlDbType.Bit)
                        {
                            valuelist.Add((bool)objValue ? 1 : 0);
                        }
                        else
                        {
                            valuelist.Add(objValue);
                        }
                    }
                }
            }

            strFields = string.Join(",", fieldlist);
            strValues = string.Join(",", valuelist);

            strFields      = "Insert Into " + parTableName + " ( " + strFields + " ) Values";
            strValues      = "(" + strValues + ")";
            strQueryResult = strFields + strValues;
            return(strQueryResult);
        }