2012-01-31 159 views
2

好吧伙计们,更新DataGrid更改为数据库?

经过许多阅读和找到解决方案,我只需要问这个。 我已经寻找它,但它并没有帮助我很好,因为它应该理清了这一点。 此外,原谅我的语法错误,如果任何...

什么问题? 我加载了一个组合框一个WPF的形​​式。这种组合框在我的数据库中的所有表中获取所有的名字。我选择一个表名并按下一个按钮来填充所选表的DataGrid(WPF)。这一切都完美。但是,当我更改单元格或添加/删除行或列时,我必须将其更新到数据库。 这是我卡住的地方。我通过一个不太理想的方式来工作。这就是为什么我问是否有更好的解决方案。

//fill the datagrid with data from chosen table in combobox!    
selectedTable = cboxTables.SelectedItem.ToString(); 
dataset = db.getDataFromTable(selectedTable); 
datatable = dataset.Tables[selectedTable]; 
datagridAdmin.ItemsSource = datatable.DefaultView; 

当DataGrid中的选择发生了变化,我设置了“提交”按钮为活动至极调用此代码:

db.updateTable(selectedTable, datatable); 

注意,“DB”是从我databaseclass一个实例。方法如下:

public bool updateTable(String tableName, DataTable datatable) 
{ 
    using (SqlBulkCopy bulkcopy = new SqlBulkCopy(thisConnection.ConnectionString, SqlBulkCopyOptions.CheckConstraints)) 
    { 
     //Set destination table name 
     //to table previously created. 
     bulkcopy.DestinationTableName = tableName; 

     try 
     { 
      thisCommand = new SqlCommand("DELETE FROM " + tableName, thisConnection); 
      thisCommand.ExecuteNonQuery(); 
      bulkcopy.WriteToServer(datatable); 
     } 
     catch (Exception ex) 
     { 
      logger.WriteLine(applicationName, ex.Message); 
     } 
    } 
} 

但问题是,第一列是一个自动增量ID,每次提交更改的DataGrid时都会不断提高。 没有更好的方法来做到这一点?

谢谢!

PS:我编码在Visual Studio 2010中使用C#中的WPF。

回答

1

总之你已经给了你会致电您的批量复制前,最好不要使用截断表法的方法,这将标识列重置为0:

thisCommand = new SqlCommand("TRUNCATE TABLE " + tableName, thisConnection); 

但是这两种方法是怎么回事导致数据库中的外键出现问题。我不知道如何在数据库类中检索数据,但我会考虑使用SqlCommandBuilder来更新数据库,而不是在每次更新时删除和重新插入所有数据。

编辑

为了进一步说明我的SqlCommandBuilder的建议。

一个ViewModel如下面应该允许使用SqlCommandBuilder的(注:这是巨大的削减,并在实际中,这一要求更好的验证,异常和事件处理,但粗略的想法是存在的):

public class YourViewModel 
{ 
    private SqlDataAdapter adapter; 
    private DataTable table; 
    private SqlCommandBuilder commandBuilder; 
    public DataTable Table { get { return table; } set { table = value; } } 
    public void LoadTable(string connectionString, string tableName, int[] primaryKeyColumns) 
    { 
     adapter = new SqlDataAdapter(string.Format("SELECT * FROM {0}", tableName), connectionString); 
     table = new DataTable(); 
     adapter.Fill(table); 
     List<DataColumn> primaryKeys = new List<DataColumn>(); 
     for (int i = 0; i < primaryKeyColumns.Length; i++) 
     { 
      primaryKeys.Add(table.Columns[primaryKeyColumns[i]]); 
     } 
     table.PrimaryKey = primaryKeys.ToArray(); 
     commandBuilder = new SqlCommandBuilder(adapter); 
     commandBuilder.GetUpdateCommand(); 
    } 
    public int Update() 
    { 
     if (table == null || table.Rows.Count == 0 || adapter == null) 
     { 
      return 0; 
     } 
     if (table.PrimaryKey.Length == 0) 
     { 
      throw new Exception("Primary Keys must be defined before an update of this nature can be performed"); 
     } 
     else 
     { 
      return adapter.Update(table); 
     } 
    } 
} 

绑定表属性的网格视图中,然后在需要时调用Update方法。您甚至可以将更新绑定到表的rowchanged事件等来自动更新数据库。 Code Project在WPF,数据网格和数据库集成方面有相当不错的文章。

+0

你说不行的TRUNCATE选项。它表示更新成功,但刷新后仍然存在旧值。 而关于SqlCommandBuilder,我试图实现它,但我怎么才能让它生成一个UPDATE命令,当它需要一个SqlCommand,我不更新之前使用? – Joey 2012-01-31 18:23:50

+0

我纠正了我的truncate语句,并尝试通过使用SqlDataAdapter来扩展我的意思。虽然很难说如何在没有看到你的源代码的情况下使用它,但我试图尽可能通用。 – GarethD 2012-02-01 00:12:06

+0

非常感谢。这将做足够我认为。将在下周尝试制定你的榜样。如果它不起作用,我会回来解释问题。 – Joey 2012-02-01 07:51:19