[英]How to get the sequence/trigger primary key that is inserted into the database and insert it into my class parameter id C#
I have the following issue. 我有以下问题。 I have a sequence and trigger in the database and they work.
我在数据库中有一个序列和触发器,它们可以工作。 This means that my insert function in C# doesn't insert the ID but the rest of the columns.
这意味着我在C#中的插入函数不会插入ID,而是插入其余的列。 The problem is that now I can't bind the ID to a parameter.
问题在于,现在我无法将ID绑定到参数。 I have another function that selects everything from the table that hits after insert as a sort of refresh and even though that function gets the correct ID it still gets lost later I believe because it is not binded on insert to the parameter in the class.
我还有另一个函数,可以从插入后命中的表中选择所有内容,以进行某种刷新,即使该函数获得了正确的ID,但以后仍然会丢失,因为它不会在插入时绑定到类中的参数。
So here is my Insert Into: 这是我的插入内容:
transaction = oleCon.BeginTransaction();
oleComd = new OleDbCommand();
oleComd.Connection = oleCon;
oleComd.Transaction = transaction;
oleComd.CommandText = "Insert into OBJEKTI" +
"(NAZIV_OBJEKTA, SIFRA_MJESTA, SIFRA_REGION)" + "Values(:naziv, :mjesto, :region)";
oleComd.Parameters.AddWithValue(":naziv", objekti.NazivObjekta);
oleComd.Parameters.AddWithValue(":mjesto", objekti.Sifra_Mjesta);
oleComd.Parameters.AddWithValue(":region", objekti.Sifra_Region);
oleComd.ExecuteNonQuery();
transaction.Commit();
oleCon.Close();
And here is my Trigger: 这是我的触发器:
create or replace
TRIGGER "OBJEKTI_ID_TRIG"
BEFORE INSERT ON "OBJEKTI"
FOR EACH ROW
DECLARE
BEGIN
SELECT OBJEKAT_ID_SEQ.NEXTVAL
INTO :NEW.OBJEKAT_ID
FROM DUAL;
END;
Anyway I really hope you guys can help me. 无论如何,我真的希望你们能帮助我。
I also tried the RETURNING statement just now but even though it compiles and goes through with no error, it still gives me my key as 0 (even though in the database it isn't, I checked) 我现在也尝试了RETURNING语句,但是即使它可以编译并顺利通过,它仍然为我提供了0(即使在数据库中也没有,我检查了)
oleComd.CommandText = "Insert into OBJEKTI" +
"(NAZIV_OBJEKTA, SIFRA_MJESTA, SIFRA_REGION)" + "Values(:naziv, :mjesto, :region) RETURNING (OBJEKAT_ID) into (:id)";
oleComd.Parameters.AddWithValue(":naziv", objekti.NazivObjekta);
oleComd.Parameters.AddWithValue(":mjesto", objekti.Sifra_Mjesta);
oleComd.Parameters.AddWithValue(":region", objekti.Sifra_Region);
oleComd.Parameters.AddWithValue(":id", objekti.ObjekatId);
EDIT: 编辑:
I tested this bit of code: 我测试了这段代码:
set serveroutput ON;
DECLARE
varid NUMBER;
BEGIN
Insert into OBJEKTI (NAZIV_OBJEKTA, SIFRA_MJESTA, SIFRA_REGION) Values('dino', 1, 1)
RETURNING OBJEKAT_ID INTO varid;
dbms_output.put_line (varid);
END;
And this works, varid shows what it should which tells me that using returning in my insert into should work as well but it is not because it still puts a 0 in there. 这项工作奏效了,varid显示了应该告诉我的内容,即告诉我在插入中使用return也应该起作用,但这不是因为它仍然在其中放置了0。
So the problem has to be the 所以问题必须是
OleComd.Parameters.AddWithValue
function but I don't know what other function to use to get this to work. 函数,但我不知道要使用什么其他函数来使其正常工作。
EDIT2: 编辑2:
So after some help from the Oracle forums I also tried this: 因此,在获得Oracle论坛的帮助之后,我也尝试了以下方法:
oleComd.CommandText = "Insert into OBJEKTI" +
"(NAZIV_OBJEKTA, SIFRA_MJESTA, SIFRA_REGION)" + " Values(:naziv, :mjesto, :region ) RETURNING OBJEKAT_ID INTO :id";
oleComd.Parameters.AddWithValue(":naziv", objekti.NazivObjekta);
oleComd.Parameters.AddWithValue(":mjesto", objekti.Sifra_Mjesta);
oleComd.Parameters.AddWithValue(":region", objekti.Sifra_Region);
//oleComd.Parameters.Add(":id", OleDbType.).Direction = ParameterDirection.Output;
OleDbParameter id = new OleDbParameter();
id.ParameterName = "id";
id.OleDbType = OleDbType.Integer;
id.Direction = ParameterDirection.Output;
oleComd.Parameters.Add(id);
objekti.ObjekatId = Convert.ToInt32(id.Value);
But once again I got nowhere. 但是我再也无处可去。 It is reporting the value of parameter id as null.
它报告参数id的值为null。 Which I don't get.
我不明白。
Hoping someone out there can help 希望有人可以帮助您
EDIT3 编辑3
My latest attempt, this fails with an error on the reader: "no data exists on the row/column" 我的最新尝试失败,并在阅读器上显示错误:“行/列中没有数据”
transaction = oleCon.BeginTransaction();
oleComd = new OleDbCommand();
oleComd.Connection = oleCon;
oleComd.Transaction = transaction;
oleComd.CommandText = "Insert into OBJEKTI" +
"(NAZIV_OBJEKTA, SIFRA_MJESTA, SIFRA_REGION)" + " Values(:naziv, :mjesto, :region )";
oleComd.Parameters.AddWithValue(":naziv", objekti.NazivObjekta);
oleComd.Parameters.AddWithValue(":mjesto", objekti.Sifra_Mjesta);
oleComd.Parameters.AddWithValue(":region", objekti.Sifra_Region);
oleComd.ExecuteNonQuery();
transaction.Commit();
//oleCon2.Open();
transaction2 = oleCon.BeginTransaction();
oleComd2 = new OleDbCommand();
oleComd2.Connection = oleCon;
oleComd2.Transaction = transaction2;
oleComd2.CommandText = "select Objekat_ID from OBJEKTI where NAZIV_OBJEKTA=:naziv";
oleComd2.Parameters.AddWithValue(":naziv", objekti.NazivObjekta);
OleDbDataReader Reader = oleComd2.ExecuteReader();
objekti.ObjekatId = Convert.ToInt32(Reader["OBJEKAT_ID"]);
Reader.Close();
oleComd2.ExecuteNonQuery();
transaction2.Commit();
The solution 解决方案
OleDbParameter id = new OleDbParameter();
id.ParameterName = "id";
id.OleDbType = OleDbType.Integer;
id.Direction = ParameterDirection.ReturnValue;
oleComd.Parameters.Add(id);
oleComd.ExecuteNonQuery();
transaction.Commit();
objekti.ObjekatId=Convert.ToInt32(oleComd.Parameters["id"].Value);
oleCon.Close();
Thank you everyone 谢谢大家
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.