2012-01-31 158 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