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

前端 未结 5 714
[愿得一人]
[愿得一人] 2021-02-05 21:40

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

i insert record like this (in C#)

SQL = \"insert into TermNumTbl (DeviceID,IP) valu         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-05 22:16

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

    The Jet 4.0 provider supports @@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();
      }
    }
    

提交回复
热议问题