Exemple #1
0
        public int ExecuteUpdate <T>(SqlConnection conn, IList <T> objs, SqlTransaction tran) where T : IdentityObj
        {
            InitDbTypeMap();
            if (objs.Count == 0)
            {
                return(0);
            }
            T t = objs[0];
            SqlDataAdapter sd        = new SqlDataAdapter();
            CreateSql      createsql = new CreateUpdateSql();

            sd.InsertCommand = new SqlCommand(createsql.Create <T>(t), conn, tran);
            sd.UpdateCommand = new SqlCommand(createsql.Create <T>(t), conn, tran);
            DataTable dt = new DataTable();
            Dictionary <TableField, PropertyInfo> fields = t.GetRelation();

            foreach (var field in fields)
            {
                dt.Columns.Add(field.Key.Name, field.Value.PropertyType);//.Add(new SqlParameter(string.Format("@{0}", item.Key.Name), item.Value.GetValue(obj)));
                //.Add(string.Format("@{0}", field.Key.Name), field.Value.PropertyType,16);
                //sd.UpdateCommand.Parameters.Add(string.Format("@{0}", field.Key.Name), dbtypeMap[field.Value.PropertyType]);
                //sd.InsertCommand.Parameters.Add(string.Format("@{0}", field.Key.Name), dbtypeMap[field.Value.PropertyType]);
            }
            dt.Columns.Add(t.GetTableKey(), typeof(object));
            dt.PrimaryKey = new DataColumn[] { dt.Columns[t.GetTableKey()] };
            //sd.UpdateCommand.Parameters.Add(string.Format("@{0}", t.GetTableKey()), dbtypeMap[typeof(object)]);
            //sd.InsertCommand.Parameters.Add(string.Format("@{0}", t.GetTableKey()), dbtypeMap[typeof(object)]);
            //sd.UpdateCommand.Parameters.Add(new SqlParameter(string.Format("@{0}", t.GetTableKey()),typeof(object)));
            foreach (T obj in objs)
            {
                DataRow dr = dt.NewRow();
                foreach (var item in fields)
                {
                    dr[item.Key.Name] = item.Value.GetValue(obj);
                }
                dr[t.GetTableKey()] = obj.ID;
                dt.Rows.Add(dr);
            }
            //sd.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
            sd.Update(dt);
            //using (BulkOperation bulk = new BulkOperation(conn))
            //{
            //    bulk.Transaction = tran;
            //    bulk.BatchSize = 300;
            //    bulk.DestinationTableName = t.GetTable();
            //    bulk.BulkUpdate(dt);
            //}
            return(objs.Count);
        }
Exemple #2
0
        public int ExecuteUpdate <T>(SqlConnection conn, T obj, SqlTransaction tran) where T : IdentityObj
        {
            CreateSql           createsql = new CreateUpdateSql();
            List <SqlParameter> sqlParams = new List <SqlParameter>();
            Dictionary <TableField, PropertyInfo> fields = obj.GetRelation();

            foreach (var item in fields)
            {
                sqlParams.Add(new SqlParameter(string.Format("@{0}", item.Key.Name), SqlNull(item.Value.GetValue(obj))));
            }
            SqlCommand cmd = conn.CreateCommand();

            cmd             = conn.CreateCommand();
            cmd.Transaction = tran;
            cmd.Parameters.AddRange(sqlParams.ToArray());
            for (int i = 0; i < cmd.Parameters.Count; i++)
            {
                cmd.Parameters[i].IsNullable = true;
            }
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = createsql.Create <T>(obj);
            int result = cmd.ExecuteNonQuery();

            cmd.Dispose();
            return(result);
        }