Beispiel #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dtValue"></param>
        /// <param name="opera"></param>
        /// <returns></returns>
        protected virtual int OnBatchUpdate(string tableName, DataTable dtValue, BuildCommandOpera opera)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException("tableName");
            }
            if (dtValue == null)
            {
                throw new ArgumentNullException("dtValue");
            }
            DataColumnCollection cols = dtValue.Columns;
            DataColumn[] pks = dtValue.PrimaryKey;

            #region ����������״̬��
            dtValue.AcceptChanges();
            foreach (DataRow row in dtValue.Rows)
            {
                if (opera == BuildCommandOpera.Insert)
                {
                    row.SetAdded();
                }
                else if (opera == BuildCommandOpera.Update)
                {
                    row.SetModified();
                }
                else if (opera == BuildCommandOpera.Delete)
                {
                    row.Delete();
                }
            }
            #endregion

            DbDataAdapter adapter = this.CreateAdapter();
            if (adapter != null)
            {
                DbCommand cmd = this.BuildCommand(tableName, cols, pks, opera);
                cmd.UpdatedRowSource = UpdateRowSource.None;
                if (opera == BuildCommandOpera.Insert)
                {
                    adapter.InsertCommand = cmd;
                }
                else if (opera == BuildCommandOpera.Update)
                {
                    adapter.UpdateCommand = cmd;
                }
                else if (opera == BuildCommandOpera.Delete)
                {
                    adapter.DeleteCommand = cmd;
                }
                int count = dtValue.Rows.Count;
                adapter.UpdateBatchSize = count < 500 ? count : 500;
                return adapter.Update(dtValue);
            }
            return 0;
        }
Beispiel #2
0
 /// <summary>
 /// ����Command����
 /// </summary>
 /// <param name="tableName"></param>
 /// <param name="cols"></param>
 /// <param name="pks"></param>
 /// <param name="opera"></param>
 /// <returns></returns>
 protected abstract DbCommand BuildCommand(string tableName, DataColumnCollection cols, DataColumn[] pks, BuildCommandOpera opera);