日期:2014-05-17  浏览次数:20452 次

ACCESS 怎样获得刚插入数据的ID
C# code
           
 sql = string.Format(" insert into neirong(lindex,title,contents,creattime,readcount,Tag)values('{0}','{1}','{2}','{3}',{4},'{5}') ", lindex, title, contents, DateTime.Now.ToString("yyyy-MM-dd"), 0, tag);
            ret = ExcuteSQL(sql);
            return ret;[code=C#]
[/code]


怎样获刚插入数据的ID

------解决方案--------------------
你可以这样
C# code
/C#

string query = "Insert Into neirong(xx,xx) 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("", xxx.Text);
    cmd.Parameters.AddWithValue("", xxx.Text);
    conn.Open();

    cmd.ExecuteNonQuery();

    cmd.CommandText = query2;

    ID = (int)cmd.ExecuteScalar();

  }

}