简体   繁体   English

创建更新并删除级联

[英]Create on update and delete cascade

i have a query to delete cascade, here the query 我有一个查询要删除级联,在这里查询

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
);

the question is, how to create / add, delete and update cascade with c#?. 问题是,如何使用C#创建/添加,删除和更新级联? so the query is running/crete automatically in this method 因此查询将以这种方式自动运行/创建

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);
}

I think you need to add logic to your CreateTables-method. 我认为您需要向CreateTables方法添加逻辑。 Preferably you add more parameters in which you can input any keys that you want constraints on. 最好添加更多参数,在其中可以输入要约束的任何键。 Cause from what I'm reading at the moment, you are not creating any constraints at all. 由于我目前正在阅读的内容,您根本没有创建任何约束。 This is possible by iterating the same logic you've already used, but for primary and foreign keys respectively - preferable with a boolean variable-flag to mark if they should be cascaded or not. 这可以通过重复使用已经使用过的相同逻辑来实现,但是分别用于主键和外键-最好使用布尔变量标记来标记是否应该级联。

Although, I suggest you do as MSI suggested and use an ORM. 虽然,我建议您按照MSI的建议进行操作并使用ORM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM