如何在訪問中將記錄插入數據庫后獲取最后一個記錄號

[英]how to get the last record number after inserting record to database in access


i have database in access with auto increase field (ID).

我有訪問自動增加字段(ID)的數據庫。

i insert record like this (in C#)

我插入這樣的記錄(在C#中)

SQL = "insert into TermNumTbl (DeviceID,IP) values ('" + DeviceID + "','" + DeviceIP + "') ";
OleDbCommand Cmd = new OleDbCommand(SQL, Conn);
Cmd.ExecuteNonQuery();
Cmd.Dispose();
Conn.Close();

how to get the last inserting number ?

如何獲得最后一個插入號碼?

i dont want to run new query i know that in sql there is something like SELECT @@IDENTITY

我不想運行新的查詢我知道在sql中有類似SELECT @@ IDENTITY的東西

but i dont know how to use it

但我不知道如何使用它

thanks in advance

提前致謝

5 个解决方案

#1


13  

More about this : Getting the identity of the most recently added record

更多相關信息:獲取最近添加的記錄的標識

The Jet 4.0 provider supports @@Identity

Jet 4.0提供程序支持@@ Identity

string query = "Insert Into Categories (CategoryName) Values (?)";
string query2 = "Select @@Identity";
int ID;
string connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Northwind.mdb";
using (OleDbConnection conn = new OleDbConnection(connect))
{
  using (OleDbCommand cmd = new OleDbCommand(query, conn))

  {
    cmd.Parameters.AddWithValue("", Category.Text);
    conn.Open();
    cmd.ExecuteNonQuery();
    cmd.CommandText = query2;
    ID = (int)cmd.ExecuteScalar();
  }
}

#2


3  

I guess you could even write an extension method for OleDbConnection...

我想你甚至可以為OleDbConnection寫一個擴展方法......

public static int GetLatestAutonumber(
    this OleDbConnection connection)
{
    using (OleDbCommand command = new OleDbCommand("SELECT @@IDENTITY;", connection))
    {
        return (int)command.ExecuteScalar();
    }
}

#3


0  

I like more indicate the type of command is very similar to the good solution provided by Pranay Rana

我更喜歡指出命令的類型與Pranay Rana提供的良好解決方案非常相似

using (OleDbCommand cmd = new OleDbCommand())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = sql_Insert;
                        cmd.ExecuteNonQuery();

                        cmd.CommandText = sql_obtainID;
                        resultado = (int)comando.ExecuteScalar();
                    }

#4


0  

 query = "Insert Into jobs (jobname,daterecieved,custid) Values ('" & ProjectNAme & "','" & FormatDateTime(Now, DateFormat.ShortDate) & "'," & Me.CustomerID.EditValue & ");"'Select Scope_Identity()"
        ' Using cn As New SqlConnection(connect)

          Using cmd As New OleDb.OleDbCommand(query, cnPTA)
                cmd.Parameters.AddWithValue("@CategoryName", OleDb.OleDbType.Integer)
                If cnPTA.State = ConnectionState.Closed Then cnPTA.Open()
                ID = cmd.ExecuteNonQuery
          End Using

#5


0  

Using @Lee.J.Baxter 's method (Which was great as the others id not work for me!) I escaped the Extension Method and just added it inline within the form itself:

使用@ Lee.J.Baxter的方法(這很好,因為其他人不適合我!)我逃脫了擴展方法,只是在表單內部添加內聯:

OleDbConnection con = new OleDbConnection(string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}'", DBPath));
OleDbCommand cmd = con.CreateCommand();
con.Open();
cmd.CommandText = string.Format("INSERT INTO Tasks (TaskName, Task, CreatedBy, CreatedByEmail, CreatedDate, EmailTo, EmailCC) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", subject, ConvertHtmlToRtf(htmlBody), fromName, fromEmail, sentOn, emailTo, emailCC);
cmd.Connection = con;
cmd.ExecuteScalar();
using (OleDbCommand command = new OleDbCommand("SELECT @@IDENTITY;", con))
{
    ReturnIDCast =(int)command.ExecuteScalar();
}

NOTE: In most cases you should use Parameters instead of the string.Format() method I used here. I just did so this time as it was quicker and my insertion values are not coming from a user's input so it should be safe.

注意:在大多數情況下,您應該使用參數而不是我在這里使用的string.Format()方法。我剛剛這樣做,因為它更快,我的插入值不是來自用戶的輸入,所以它應該是安全的。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/08/29/729a291ecd341076a89a0bdc0eb4f6b1.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com