gpt4 book ai didi

c# - 在单个 Oracle 命令 C# 中执行多个插入表命令

转载 作者:行者123 更新时间:2023-11-30 20:41:44 26 4
gpt4 key购买 nike

我想在单个 Oracle 命令中执行以下所有插入表语句,这可能吗?

OracleCommand cmd = new OracleCommand();
var parameter = cmd.Parameters;
string insrtPInfo = "Insert into PersonalInfo(Name, ContactNum, EmailID, Address, Gender, DOB) VALUES(:pName, :contactNum, :emailId, :address, :gender, :dob )";
string insrtEdu = "Insert into PersonalInfo(Degree, Institution, Year, CGPA_Marks) VALUES(:degree, :Instituition, :year, :marks)";
string insrtExprnce = "Insert into PersonalInfo(Organization, Organization, Desigination) VALUES(:organization, :duration, :desigination)";
string insrtSkils = "Insert into PersonalInfo(Programming_languages, Softwares, OS) VALUES(:progLang, :softwares, OS)";
string insrtProj = "Insert into PersonalInfo(FYP, Other_Projects) VALUES(:fyp, otherProj)";
// T1
parameter.Add("pName", pName);
parameter.Add("contactNum", contactNum);
parameter.Add("emailId", emailId);
parameter.Add("address", address);
parameter.Add("gender", gender);
parameter.Add("dob", dob);
// T2
parameter.Add("degree", degree);
parameter.Add("Instituition", Instituition);
parameter.Add("year", year);
parameter.Add("marks", marks);
// T3
parameter.Add("organization", organization);
parameter.Add("duration", duration);
parameter.Add("desigination", desigination);
// T4
parameter.Add("progLang", progLang);
parameter.Add("softwares", softwares);
parameter.Add("OS", OS);
// T5
parameter.Add("fyp", fyp);
parameter.Add("otherProj", otherProj);
cmd.CommandText = insrtPInfo;
cmd.ExecuteNonQuery();

如何在单个 Oracle 命令中执行所有这些语句。或者任何其他最好的方法来做到这一点。我使用 Visual Studio 2013 和 Oracle 11g 作为数据库。

最佳答案

一种选择是使用 Oracle 的 INSERT ALL 语法:

string sql = @"Insert all
into PersonalInfo(Name, ContactNum, EmailID, Address, Gender, DOB) VALUES(:pName, :contactNum, :emailId, :address, :gender, :dob )
into PersonalInfo(Degree, Institution, Year, CGPA_Marks) VALUES(:degree, :Instituition, :year, :marks)
into PersonalInfo(Organization, Organization, Desigination) VALUES(:organization, :duration, :desigination)
into PersonalInfo(Programming_languages, Softwares, OS) VALUES(:progLang, :softwares, :OS)
into PersonalInfo(FYP, Other_Projects) VALUES(:fyp, :otherProj)
select * from dual";

文档:multi_table_insert

ALL into_clause

Specify ALL followed by multiple insert_into_clauses to perform an unconditional multitable insert. Oracle Database executes each insert_into_clause once for each row returned by the subquery.


另一个选项是将所有 INSERT 语句包装在匿名 PL/SQL block 中:

string sql = @"begin
insert into PersonalInfo(Name, ContactNum, EmailID, Address, Gender, DOB) VALUES(:pName, :contactNum, :emailId, :address, :gender, :dob );
insert into PersonalInfo(Degree, Institution, Year, CGPA_Marks) VALUES(:degree, :Instituition, :year, :marks);
insert into PersonalInfo(Organization, Organization, Desigination) VALUES(:organization, :duration, :desigination);
insert into PersonalInfo(Programming_languages, Softwares, OS) VALUES(:progLang, :softwares, :OS);
insert into PersonalInfo(FYP, Other_Projects) VALUES(:fyp, :otherProj);
end;";

关于c# - 在单个 Oracle 命令 C# 中执行多个插入表命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32103322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com