예제 #1
0
        /// <summary>
        /// 通过 tableName 生成 Insert Sql
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static String GenerateInsertDbSql(String tableName, List <ValAndFieldName> data)
        {
            StringBuilder cols      = new StringBuilder();
            StringBuilder vals      = new StringBuilder();
            String        sqlPrefix = "INSERT `" + tableName + "` (";

            for (int i = 0; i < data.Count(); ++i)
            {
                ValAndFieldName temp = data[i];
                cols.Append("`" + temp.Name + "`,");
                String val = DbGenerateUtil.ParseType(temp.Val);
                vals.Append(val + ",");
            }
            //去掉最后一个 ,
            cols.Remove(cols.Length - 1, 1);
            vals.Remove(vals.Length - 1, 1);
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append(sqlPrefix);
            stringBuilder.Append(cols);
            stringBuilder.Append(" ) VALUES ( ");
            stringBuilder.Append(vals);
            stringBuilder.Append(" ) ;");
            return(stringBuilder.ToString());
        }
예제 #2
0
파일: ModelUtil.cs 프로젝트: clbouc/test
        //
        public static ValAndFieldName[] FieldToValAndFieldArray(Object obj)
        {
            Type type       = obj.GetType();
            var  fieldinfos = type.GetFields();

            ValAndFieldName[] array = new ValAndFieldName[fieldinfos.Length];
            int i = 0;

            foreach (var info in fieldinfos)
            {
                ValAndFieldName temp = new ValAndFieldName();
                temp.Name  = info.Name;
                temp.Val   = info.GetValue(obj);
                array[i++] = temp;
            }
            return(array);
        }