2013-07-08 40 views
1

我有一個查詢刪除級聯,在這裏查詢創建的更新和刪除級聯

create table satuan_type 
(
    id int identity(1,1), 
    satuan_id int(200), 
    type_id int, 
    primary key(id), 
    foreign key(satuan_id) references satuan(id) on update cascade on delete cascade 
); 

的問題是,如何創建/添加,刪除和用C#級聯更新?所以查詢是運行/克里特自動在這種方法

Dictionary<String, String> dic = new Dictionary<string, string>(); 
dic.Add("id", "INT PRIMARY KEY IDENTITY"); 
dic.Add("satuan_id", "INT"); 
dic.Add("type_id", "INT"); 
cDatabaseSQLServer.CreateTables("satuan_type", dic); 

public int CreateTables(String tableName, Dictionary<String, String> data) 
{ 
    switch (sqlType) 
    { 
     case DATABASE_SQL_TYPE.DATABASE_SQL_TYPE_SQLITE: 
      return cSQLite.CreateTables(tableName, data); 
     case DATABASE_SQL_TYPE.DATABASE_SQL_TYPE_MSSQL: 
      return cSQL.CreateTables(tableName, data); 
    } 
    return 0; 
} 

public int CreateTables(String tableName, Dictionary<String, String> data) 
{ 
    string s = "CREATE TABLE " + tableName + " ("; 
    bool first = true; 
    foreach (KeyValuePair<String, String> val in data) 
    { 
     if (first) 
     { 
      first = false; 
     } 
     else 
     { 
      s = s + ","; 
     } 
     string s1; 
     s1 = String.Format("{0} {1}", val.Key, val.Value); 
     s = s + s1; 
    } 
    s = s + ")"; 
    return this.ExecuteNonQuery(s); 
} 
+1

刪除級聯往往不是一個壞主意。 –

+0

如果你要這麼長,使用ORM。做事更容易,更好。 – MSI

回答

0

我認爲你需要添加邏輯到你的CreateTables方法。最好你添加更多的參數,你可以在其中輸入你想約束的任何鍵。因爲我目前正在閱讀的內容,你根本沒有創建任何約束。這可以通過迭代已經使用的相同邏輯來實現,但是分別用於主鍵和外鍵 - 最好使用布爾變量標誌來標記它們是否應級聯。

雖然,我建議你按照MSI的建議做並使用ORM。