但若需要用到新增的索引值寫入到別的地方時該怎麼做呢?
可以使用MYSQL中的LAST_INSERT_ID()
SELECT LAST_INSERT_ID()
string sqlIns = "INSERT INTO table (a,b) VALUES ('c','d')";
OdbcCommand cmdIns = new OdbcCommand(sqlIns, myConn);
cmdIns.ExecuteNonQuery();
cmdIns.Parameters.Clear();
cmdIns.CommandText = "SELECT LAST_INSERT_ID()";
// Get the last inserted id.
int insertID = Convert.ToInt32(cmdIns.ExecuteScalar());
沒有留言:
張貼留言