Han*_*nni 2 c# oracle oracle11g visual-studio
我想在单个 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();
Run Code Online (Sandbox Code Playgroud)
我如何在单个 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";
Run Code Online (Sandbox Code Playgroud)
全部进入_子句
指定
ALL后跟多个insert_into_clauses以执行无条件多表插入。Oracle 数据库对子insert_into_clause查询返回的每一行执行一次。
另一种选择是将所有INSERT语句包装在一个匿名PL/SQL块中:
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;";
Run Code Online (Sandbox Code Playgroud)