当前位置: 首页>>代码示例>>C#>>正文


C# IDbContext.Add方法代码示例

本文整理汇总了C#中IDbContext.Add方法的典型用法代码示例。如果您正苦于以下问题:C# IDbContext.Add方法的具体用法?C# IDbContext.Add怎么用?C# IDbContext.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IDbContext的用法示例。


在下文中一共展示了IDbContext.Add方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Insert

        public IDbContext Insert(IDbContext ctx, object obj, bool isAuditLog)
        {
            string sqlInsert;
            string fieldName = "";
            string parameter = "";

            //Simpan informasi nama table di session untuk informasi error handling bila terjadi duplicate key
            if (HttpContext.Current != null)
            {
                HttpContext.Current.Session["_LastSqlProcessTable"] = "";
                HttpContext.Current.Session["_LastSqlProcessPKField"] = "";
                HttpContext.Current.Session["_LastSqlProcessPKValue"] = "";
            }

            if (obj is DbDataModel)
            {
                Type type = obj.GetType();
                PropertyInfo[] propInfs = type.GetProperties();
                foreach (PropertyInfo prop in propInfs)
                {
                    object[] custAttr = prop.GetCustomAttributes(false);
                    foreach (Attribute attrib in custAttr)
                    {
                        ColumnAttribute schema = attrib as ColumnAttribute;
                        if (schema != null && !schema.IsComputed
                            && !schema.IsIdentity
                            && !schema.IsTimeStamp)
                        {
                            object fieldValue = prop.GetValue(obj, null);
                            if (!schema.IsNullable)
                                fieldValue = CheckIsNull(fieldValue, prop.PropertyType);

                            //if (fieldValue != null &&
                            //    !(schema.IsNullable && IsFieldNullAbleNotAssignValue(fieldValue, prop.PropertyType)))
                            //{
                            //    ctx.Add(string.Format("{0}{1}", _prefixParameter, schema.Name), fieldValue);
                            //    fieldName += string.Format(", {0}", schema.Name);
                            //    parameter += string.Format(", {0}{1}", _prefixParameter, schema.Name);

                            //    //Simpan informasi primary key di session untuk informasi error handling bila terjadi duplicate key
                            //    if (schema.IsPrimaryKey)
                            //    {
                            //        HttpContext.Current.Session["_LastSqlProcessPKField"] += ";" + schema.Name;
                            //        HttpContext.Current.Session["_LastSqlProcessPKValue"] += ";" + fieldValue;
                            //    }
                            //}

                            fieldName += string.Format(", {0}", schema.Name);
                            if (fieldValue != null &&
                               !(schema.IsNullable && IsFieldNullAbleNotAssignValue(fieldValue, prop.PropertyType)))
                            {
                                parameter += string.Format(", {0}{1}", _prefixParameter, schema.Name);

                                if (fieldValue.GetType().FullName.Contains("DateTime"))
                                {
                                    if (fieldValue is DBNull || Convert.ToDateTime(fieldValue).Year < 1900)
                                        fieldValue = new DateTime(1900, 1, 1);
                                }
                                ctx.Add(string.Format("{0}{1}", _prefixParameter, schema.Name), fieldValue);

                            }
                            else
                            {
                                parameter += ", NULL";
                            }

                            //Simpan informasi primary key di session untuk informasi error handling bila terjadi duplicate key
                            if (schema.IsPrimaryKey && HttpContext.Current != null)
                            {
                                HttpContext.Current.Session["_LastSqlProcessPKField"] += ";" + schema.Name;
                                HttpContext.Current.Session["_LastSqlProcessPKValue"] += ";" + fieldValue;
                            }

                        }
                    }
                }
            }

            //Simpan informasi nama table di session untuk informasi error handling bila terjadi duplicate key
            if (HttpContext.Current != null)
            {
                HttpContext.Current.Session["_LastSqlProcessTable"] = _tableName;

                if (HttpContext.Current.Session["_LastSqlProcessPKField"].ToString().Trim() != string.Empty)
                {
                    HttpContext.Current.Session["_LastSqlProcessPKField"] =
                        HttpContext.Current.Session["_LastSqlProcessPKField"].ToString().Substring(1);
                    HttpContext.Current.Session["_LastSqlProcessPKValue"] =
                        HttpContext.Current.Session["_LastSqlProcessPKValue"].ToString().Substring(1);
                }
            }
            fieldName = (fieldName.Length > 2) ? fieldName.Substring(2) : fieldName;
            parameter = (parameter.Length > 2) ? parameter.Substring(2) : parameter;

            sqlInsert = string.Format("INSERT INTO {0} ", _tableName);
            sqlInsert += string.Format("( {0}) ", fieldName);
            sqlInsert += string.Format(" {0} ", "VALUES");
            sqlInsert += string.Format("( {0} )", parameter);

            if (isAuditLog)
//.........这里部分代码省略.........
开发者ID:tjhaihen,项目名称:Basecamp,代码行数:101,代码来源:DbHelper.cs

示例2: Update

        public IDbContext Update(IDbContext ctx, object obj, bool isAuditLog)
        {
            ctx.CommandText = "";
            if (obj != null && obj is DbDataModel)
            {
                Type type = obj.GetType();
                Hashtable hash = ((DbDataModel) obj).OriginalValue;

                string fields = "";

                foreach (PropertyInfo prop in type.GetProperties())
                {
                    foreach (Attribute attrib in prop.GetCustomAttributes(false))
                    {
                        ColumnAttribute schema = attrib as ColumnAttribute;
                        if (schema != null && !schema.IsPrimaryKey)
                        {
                            object oriValue = hash != null ? hash[schema.Name] : null;
                            object newValue = prop.GetValue(obj, null);
                            if (schema.IsNullable && IsFieldNullAbleNotAssignValue(newValue, prop.PropertyType))
                            //(newValue == null && schema.IsNullable && oriValue != null)
                            {
                                //Sql Script Update
                                fields += string.Format(", {0} = null", schema.Name);
                            }
                            else if (newValue != null && !newValue.Equals(oriValue))
                            {
                                //Sql Script Update
                                fields += string.Format(", {0} = {1}{0}", schema.Name, _prefixParameter);
                                //Parameter
                                ctx.Add(string.Format("{0}{1}", _prefixParameter, schema.Name), newValue);
                            }

                            //newValue = CheckIsNull(newValue, prop.PropertyType);
                            //if (newValue.GetType().FullName.Contains("Int64"))
                            //    if (Convert.ToInt64(newValue) == 0)
                            //        newValue = null;
                            //if (newValue == null && schema.IsNullable)
                            //{
                            //    //Sql Script Update
                            //    fields += string.Format(", {0} = null", schema.Name);
                            //}
                            //else if (!newValue.Equals(oriValue))
                            //{
                            //    ////if (newValue.GetType().FullName.Contains("DateTime"))
                            //    ////    if (Convert.ToDateTime(newValue).Year < 1900)
                            //    ////        newValue = new DateTime(1900, 1, 1);
                            //    newValue = CheckIsNull(newValue, prop.PropertyType);
                            //    //Sql Script Update
                            //    fields += string.Format(", {0} = {1}{0}", schema.Name, _prefixParameter);
                            //    //Parameter
                            //    ctx.Add(string.Format("{0}{1}", _prefixParameter, schema.Name), newValue);
                            //}
                        }
                    }
                }
                if (!fields.Equals(string.Empty))
                {
                    string keys = "";
                    foreach (DbFieldValue dbValue in GetKeyValues(obj))
                    {
                        keys += string.Format(" AND {0} = {1}{0}", dbValue.FieldName, _prefixParameter);
                        ctx.Add(string.Format("{0}{1}", _prefixParameter, dbValue.FieldName), dbValue.Current);
                    }

                    string query;
                    query = string.Format("UPDATE {0} SET ", _tableName);
                    query += string.Format("{0} ", fields.Substring(2));
                    query += string.Format("WHERE {0} ", keys.Substring(5));

                    if (isAuditLog)
                        ctx.CommandText = GetUpdateAuditLogScript(obj) + "; " + query;
                    else
                        ctx.CommandText = query;

                    //if (!_tableName.ToLower().Contains("systranslation"))
                    //{
                    //    SiAuto.Main.LogSql("Update " + _tableName, ctx.CommandText);
                    //    SiAuto.Main.LogObject(_tableName, obj);
                    //}
                }
            }

            return ctx;
        }
开发者ID:tjhaihen,项目名称:Basecamp,代码行数:85,代码来源:DbHelper.cs

示例3: Delete

        public IDbContext Delete(IDbContext ctx, object obj, bool isAuditLog)
        {
            ctx.CommandText = "";
            if (obj != null && obj is DbDataModel)
            {
                string keys = "";
                foreach (DbFieldValue dbValue in GetKeyValues(obj))
                {
                    keys += string.Format(" AND {0} = {1}{0}", dbValue.FieldName, _prefixParameter);
                    ctx.Add(string.Format("{0}{1}", _prefixParameter, dbValue.FieldName), dbValue.Current);
                }
                if (!keys.Equals(string.Empty))
                {
                    string query;
                    query = string.Format("DELETE {0} ", _tableName);
                    query += string.Format("WHERE {0} ", keys.Substring(5));

                    if (isAuditLog)
                        ctx.CommandText = GetDeleteAuditLogScript(obj) + "; " + query;
                    else
                        ctx.CommandText = query;

                    //if (!_tableName.ToLower().Contains("systranslation"))
                    //{
                    //    SiAuto.Main.LogSql("Delete " + _tableName, ctx.CommandText);
                    //    SiAuto.Main.LogObject(_tableName, obj);
                    //}
                }
            }

            return ctx;
        }
开发者ID:tjhaihen,项目名称:Basecamp,代码行数:32,代码来源:DbHelper.cs


注:本文中的IDbContext.Add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。