All pastes #2073595 Raw Edit

Anonymous

public text v1 · immutable
#2073595 ·published 2011-06-02 07:36 UTC
rendered paste body
         public int UpdateDataBaseTable(SQLTable dataTable, bool updateCommand) // updates table in database
        {
            if (dataTable.TableName == "Procedure")
                dataTable.TableName = string.Format("[{0}]", dataTable.TableName);
            int y = 0;
            SqlCommandBuilder cb = new SqlCommandBuilder(dataTable.DataAdapter);
            dataTable.DataAdapter.RowUpdated += new SqlRowUpdatedEventHandler(DataAdapter_RowUpdated);
            dataTable.DataAdapter.RowUpdating += new SqlRowUpdatingEventHandler(DataAdapter_RowUpdating);
            try
            {

                if (dataTable.PrimaryKey.Length != 0 && updateCommand)
                {
                    cb.ConflictOption = ConflictOption.OverwriteChanges;
                    sqlCommand = cb.GetUpdateCommand(true);
                    if (sqlCommand != null)
                    {
                        sqlCommand = sqlCommand.Clone();
                        sqlCommand.CommandTimeout = Settings.Convert.Default.ConnectionTimeOut;
                        dataTable.DataAdapter.UpdateCommand = sqlCommand;
                    }
                }
                else if (updateCommand)
                {
                    cb.ConflictOption = ConflictOption.CompareAllSearchableValues;
                    cb.SetAllValues = true;
                    cb.RefreshSchema();
                    sqlCommand = cb.GetUpdateCommand(); //GetUpdateCommand(true).Clone();
                    sqlCommand.CommandTimeout = Settings.Convert.Default.ConnectionTimeOut;
                    dataTable.DataAdapter.UpdateCommand = sqlCommand;
                }
                else
                {
                    sqlCommand = cb.GetInsertCommand(true).Clone();
                    sqlCommand.CommandTimeout = Settings.Convert.Default.ConnectionTimeOut;
                    dataTable.DataAdapter.InsertCommand = sqlCommand;
                }
                DataTable newTable = dataTable.GetChanges();

                if (newTable == null || newTable.Rows.Count == 0)
                    return 0;

                y = dataTable.DataAdapter.Update(newTable);

            }
            catch (Exception e)
            {
                AddError(e.Message + "\n\t Table name: " + dataTable.TableName, true, Settings.ErrorLog.Default.ShowMessageBox);
                return -1;
            }
            finally
            {
                con.Close();
            }
            return y;
        }