我是一个在表中插入记录的函数,我想获取插入的 ID。我能怎么做?我必须进行新的查询?
using (NpgsqlConnection conn = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["AxWaveConnection"].ToString()))
{
conn.Open();
NpgsqlTransaction transaction = conn.BeginTransaction();
String query =
"INSERT INTO Video (youtubeidvideo, title, rating, viewcount, duration, dataadded, errorflag, schedulingflag, schedulingdate, convertdate) " +
"VALUES ('" + youtubeIdVideo + "','" + title + "'," + rating.ToString().Replace(',', '.') + "," + viewCount.ToString() + "," +
duration.ToString() + ", now(), false, false, null, null); ";
NpgsqlCommand cmd = new NpgsqlCommand(query, conn, transaction);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
scopeID = -1; //Duplicate Record
}
transaction.Commit();
conn.Close();
}