当前位置: 代码迷 >> ASP.NET >> ACCESS 怎的获得刚插入数据的ID
  详细解决方案

ACCESS 怎的获得刚插入数据的ID

热度:8656   发布时间:2013-02-25 00:00:00.0
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();  }}
  相关解决方案