简体   繁体   English

从 C# 中的插入查询返回 ID

[英]Returning ID from insert query in C#

I had problem that how to return auto incremented id of table when record is inserted in table using C# and MS Access database.我有一个问题,当使用 C# 和 MS Access 数据库将记录插入到表中时,如何返回表的自动递增 id。 The following is the code that I tried.以下是我尝试的代码。

string data1 = string.Format("insert into Enquiry(fname,mname,lname,date_of_enq,address,contact_no,email_id,course_id,other,probable_date,updated_date,status)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')", 
        txtfName.Text, txtmName.Text, txtlName.Text, fullDate.Text, txtaddress.Text, txtContactNo.Text, varemail, CBCourse.SelectedValue.ToString(), txtOther.Text, DTPFeedBackDate.Text, updated_date, status);

I want the id of that inserted record which is auto incremented in table.我想要在表中自动增加的插入记录的 id。 How can I get it?我怎么才能得到它?

You can use following:您可以使用以下内容:

Select Ident_Current(TableName); 

SELECT @@IDENTITY;

SELECT SCOPE_IDENTITY();

SELECT @@IDENTITY It returns the last IDENTITY value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value. SELECT @@IDENTITY 它返回在连接上产生的最后一个 IDENTITY 值,而不管产生该值的表,也不管产生该值的语句的范围。 @@IDENTITY will return the last identity value entered into a table in your current session. @@IDENTITY 将返回在当前会话中输入到表中的最后一个标识值。 While @@IDENTITY is limited to the current session, it is not limited to the current scope.虽然@@IDENTITY 仅限于当前会话,但不限于当前范围。 If you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.如果表上的触发器导致在另一个表中创建标识,则您将获得最后创建的标识,即使它是创建它的触发器。

SELECT SCOPE_IDENTITY() It returns the last IDENTITY value produced on a connection and by a statement in the same scope, regardless of the table that produced the value. SELECT SCOPE_IDENTITY() 它返回在连接上和由同一范围内的语句生成的最后一个 IDENTITY 值,而不管生成该值的表如何。 SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. SCOPE_IDENTITY() 与@@IDENTITY 一样,将返回在当前会话中创建的最后一个标识值,但它也会将其限制在您当前的范围内。 In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function.换句话说,它将返回您明确创建的最后一个标识值,而不是由触发器或用户定义的函数创建的任何标识。

SELECT IDENT_CURRENT('tablename') It returns the last IDENTITY value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value. SELECT IDENT_CURRENT('tablename') 它返回表中生成的最后一个 IDENTITY 值,无论创建该值的连接如何,也无论生成该值的语句的范围如何。 IDENT_CURRENT is not limited by scope and session; IDENT_CURRENT 不受范围和会话的限制; it is limited to a specified table.它仅限于指定的表。 IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope. IDENT_CURRENT 返回为任何会话和任何范围内的特定表生成的标识值。

To avoid the potential problems associated with adding a trigger later on, always use SCOPE_IDENTITY() to return the identity of the recently added row in your T SQL Statement or Stored Procedure.为避免与稍后添加触发器相关的潜在问题,请始终使用 SCOPE_IDENTITY() 返回 T SQL 语句或存储过程中最近添加的行的标识。

In MsAccess在 MsAccess 中

// Include a variable and a command to retrieve the identity value from the Access database.
int newID = 0;
OleDbCommand idCMD = new OleDbCommand("SELECT @@IDENTITY", nwindConn);
// Retrieve the identity value and store it in the CategoryID column.
newID = (int)idCMD.ExecuteScalar();
return newID;

just add eg SELECT @@IDENTITY to your query, and execute this as a scalar (be aware, that there are other ways, depending on the scope of your identity)只需将例如SELECT @@IDENTITY添加到您的查询中,并将其作为标量执行(请注意,还有其他方法,具体取决于您的身份范围)

this applies to mssql - for other systems i am awaiting your answer on my comment!这适用于 mssql - 对于其他系统,我正在等待您对我的评论的回答!

btw: i would not execute a query in this way - the keyword is sql-injection.顺便说一句:我不会以这种方式执行查询 - 关键字是 sql-injection。 rather go for parameters!而是去寻找参数!

this C# code worked for me using the Oracle.ManagedDataAccess 4.121.1.0 library:这个 C# 代码使用Oracle.ManagedDataAccess 4.121.1.0 库为我工作:

OracleConnection conn = oracleConnLicen();
OracleCommand cmd = conn.CreateCommand();

conn.Open();
cmd.CommandText = "INSERT INTO account (name) VALUES ('Adam') RETURNING id INTO :p_id";

Then before ExecuteNonQuery:然后在 ExecuteNonQuery 之前:

OracleParameter pId = new OracleParameter("p_id", OracleDbType.Int32)
    {
        Direction = ParameterDirection.Output
    };
cmd.Parameters.Add(pId);

cmd.ExecuteNonQuery();
return Convert.ToInt32(pId.Value.ToString());

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

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