【问题标题】:insert into postgresql table using c#使用 C# 插入到 postgresql 表中
【发布时间】:2013-05-15 08:50:05
【问题描述】:

我有两个通过外键链接的 postgreSQL 表。 我想将来自第一个表主键的数据插入到第二个表中。 我在我的 c# 应用程序中写了这个:

command1.CommandText = "CREATE TABLE table1(ID CHAR(256) CONSTRAINT id PRIMARY KEY, Title CHAR)";
command1.ExecuteNonQuery();
command1.CommandText = "insert into projects (ID, Title)" + "values(@id, @title);";
command1.Parameters.AddWithValue("@id", ID);
command1.Parameters.AddWithValue("@title",Title);
command1.ExecuteNonQuery();

第二张桌子:

command2.CommandText = "CREATE TABLE table2(table1id CHAR(256), champs1 CHAR(256), foreign key (table1id) references table1(ID))";
command2.ExecuteNonQuery();

问题是我不知道如何在表 2 中插入作为第一个表的主键的第一列。

对此有什么想法吗? 谢谢你的帮助:)

【问题讨论】:

  • 当你想在第二个表中插入值时

标签: c# postgresql foreign-keys


【解决方案1】:

尝试添加以下代码。

cmd3=new SqlCommand("insert into table2 values(@table1id,@champs1,@table1id2)",conn);
cmd3.parameter.AddWithValue("@table1id",ID);//ID=ID of first table
cmd3.parameter.AddWithValue("@champs1",chaps);
cmd3.parameter.AddWithValue("@table1id2",ID);//ID=ID of first table
cmd3.ExecuteNonQuery();

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    相关资源
    最近更新 更多