插入语句后从 SQL 数据库中检索自动增量 ID
Retrieve Auto Increment ID from SQL Database after Insert Statement
下面是我的代码在将数据插入数据库后检索自动增量 ID。
但是,我在将数据插入数据库之前获取自动增量 ID。
插入数据库后如何获取自增ID?
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RetrievePRReqID();
}
}
//检索ID方法
private void RetrievePRReqID()
{
try
{
string query = "Select IDENT_CURRENT('tblPRRequest')";
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand cmd = new SqlCommand(query, sqlCon);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
int value = int.Parse(reader[0].ToString()) ;
txt_PRNO.Text = value.ToString();
}
}
catch(Exception)
{
throw;
}
finally
{
if(con.State == ConnectionState.Open)
{
con.Close();
}
}
}
//请求按钮方法
protected void btn_Request(object sender, EventArgs e)
{
string insertCmd = "INSERT INTO tblPRRequest (RequestTo,RequestFrom,RequestedByName) " +
"VALUES (@RequestTo,@RequestFrom,@RequestedByName)";
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
using (SqlCommand sqlcmd = new SqlCommand(insertCmd, conn))
{
sqlcmd.Parameters.Clear();
SqlCommand sqlCmd = new SqlCommand(insertCmd, sqlCon);
sqlcmd.Parameters.AddWithValue("@RequestTo", lblPurchasingDept.Text);
sqlcmd.Parameters.AddWithValue("@RequestFrom", ddlDept.SelectedItem.Text);
sqlcmd.Parameters.AddWithValue("@RequestedByName", SUserName.Text);
sqlcmd.ExecuteNonQuery();
}
}
***//After Insert into the table, I want to retrieve latest generated Auto Increment ID in here.***
}
试试这个:
protected void btn_Request(object sender, EventArgs e)
{
string insertCmd = "INSERT INTO tblPRRequest (RequestTo,RequestFrom,RequestedByName) " +
"VALUES (@RequestTo,@RequestFrom,@RequestedByName)";
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
using (SqlCommand sqlcmd = new SqlCommand(insertCmd, conn))
{
sqlcmd.Parameters.Clear();
SqlCommand sqlCmd = new SqlCommand(insertCmd, sqlCon);
sqlcmd.Parameters.AddWithValue("@RequestTo", lblPurchasingDept.Text);
sqlcmd.Parameters.AddWithValue("@RequestFrom", ddlDept.SelectedItem.Text);
sqlcmd.Parameters.AddWithValue("@RequestedByName", SUserName.Text);
sqlcmd.Parameters.Add("@ID", SqlDbType.Int).Direction = ParameterDirection.Output;
sqlcmd.ExecuteNonQuery();
}
}
***//After Insert into the table, I want to retrieve latest generated Auto Increment ID in here.***
sqlcmd.Parameters["@ID"].value; // Access it like this
}
如果你能把ExecuteNonQuery
改成ExecuteScalar
,那就更简单了:What is the difference between ExecuteScalar, ExecuteReader and ExecuteNonQuery?
如 SQL 服务器文档中所述
https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql?view=sql-server-ver15
The OUTPUT clause may be useful to retrieve the value of identity or computed columns after an INSERT or UPDATE operation.
您必须更改您的 SQL 声明
INSERT INTO tblPRRequest (RequestTo,RequestFrom,RequestedByName)
OUTPUT inserted.ID
-------^^^^^^^^_^^
VALUES (@RequestTo,@RequestFrom,@RequestedByName)
现在您可以使用 ExecuteScalar 获取插入的值
protected void btn_Request(object sender, EventArgs e)
{
int id= 0;
string insertCmd = "INSERT INTO tblPRRequest (RequestTo,RequestFrom,RequestedByName) " +
"output inserted.ID" +
"VALUES (@RequestTo,@RequestFrom,@RequestedByName)";
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
using (SqlCommand sqlcmd = new SqlCommand(insertCmd, conn))
{
sqlcmd.Parameters.AddWithValue("@RequestTo", lblPurchasingDept.Text);
sqlcmd.Parameters.AddWithValue("@RequestFrom", ddlDept.SelectedItem.Text);
sqlcmd.Parameters.AddWithValue("@RequestedByName", SUserName.Text);
id = (int)sqlcmd.ExecuteScalar(); //the result is of Object type, cast it safely
}
}
Debug.WriteLine(id.ToString()); // Access it like this
}
通过参考@Mx.Wolf 的示例答案,我做了一些修改以获得正确的答案,下面是有效的代码:
protected void btn_Request(object sender, EventArgs e)
{
object id ;
string insertCmd = "INSERT INTO tblPRRequest (RequestTo,RequestFrom,RequestedByName) " +
"output inserted.PRReqID " +
"VALUES (@RequestTo,@RequestFrom,@RequestedByName)";
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
using (SqlCommand sqlcmd = new SqlCommand(insertCmd, conn))
{
sqlcmd.Parameters.AddWithValue("@RequestTo", lblPurchasingDept.Text);
sqlcmd.Parameters.AddWithValue("@RequestFrom", ddlDept.SelectedItem.Text);
sqlcmd.Parameters.AddWithValue("@RequestedByName", SUserName.Text);
id = sqlcmd.ExecuteScalar(); //the result is of Object type, cast it safely
}
}
Debug.WriteLine(id.ToString()); // Access it like this
下面是我的代码在将数据插入数据库后检索自动增量 ID。
但是,我在将数据插入数据库之前获取自动增量 ID。
插入数据库后如何获取自增ID?
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RetrievePRReqID();
}
}
//检索ID方法
private void RetrievePRReqID()
{
try
{
string query = "Select IDENT_CURRENT('tblPRRequest')";
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand cmd = new SqlCommand(query, sqlCon);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
int value = int.Parse(reader[0].ToString()) ;
txt_PRNO.Text = value.ToString();
}
}
catch(Exception)
{
throw;
}
finally
{
if(con.State == ConnectionState.Open)
{
con.Close();
}
}
}
//请求按钮方法
protected void btn_Request(object sender, EventArgs e)
{
string insertCmd = "INSERT INTO tblPRRequest (RequestTo,RequestFrom,RequestedByName) " +
"VALUES (@RequestTo,@RequestFrom,@RequestedByName)";
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
using (SqlCommand sqlcmd = new SqlCommand(insertCmd, conn))
{
sqlcmd.Parameters.Clear();
SqlCommand sqlCmd = new SqlCommand(insertCmd, sqlCon);
sqlcmd.Parameters.AddWithValue("@RequestTo", lblPurchasingDept.Text);
sqlcmd.Parameters.AddWithValue("@RequestFrom", ddlDept.SelectedItem.Text);
sqlcmd.Parameters.AddWithValue("@RequestedByName", SUserName.Text);
sqlcmd.ExecuteNonQuery();
}
}
***//After Insert into the table, I want to retrieve latest generated Auto Increment ID in here.***
}
试试这个:
protected void btn_Request(object sender, EventArgs e)
{
string insertCmd = "INSERT INTO tblPRRequest (RequestTo,RequestFrom,RequestedByName) " +
"VALUES (@RequestTo,@RequestFrom,@RequestedByName)";
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
using (SqlCommand sqlcmd = new SqlCommand(insertCmd, conn))
{
sqlcmd.Parameters.Clear();
SqlCommand sqlCmd = new SqlCommand(insertCmd, sqlCon);
sqlcmd.Parameters.AddWithValue("@RequestTo", lblPurchasingDept.Text);
sqlcmd.Parameters.AddWithValue("@RequestFrom", ddlDept.SelectedItem.Text);
sqlcmd.Parameters.AddWithValue("@RequestedByName", SUserName.Text);
sqlcmd.Parameters.Add("@ID", SqlDbType.Int).Direction = ParameterDirection.Output;
sqlcmd.ExecuteNonQuery();
}
}
***//After Insert into the table, I want to retrieve latest generated Auto Increment ID in here.***
sqlcmd.Parameters["@ID"].value; // Access it like this
}
如果你能把ExecuteNonQuery
改成ExecuteScalar
,那就更简单了:What is the difference between ExecuteScalar, ExecuteReader and ExecuteNonQuery?
如 SQL 服务器文档中所述 https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql?view=sql-server-ver15
The OUTPUT clause may be useful to retrieve the value of identity or computed columns after an INSERT or UPDATE operation.
您必须更改您的 SQL 声明
INSERT INTO tblPRRequest (RequestTo,RequestFrom,RequestedByName)
OUTPUT inserted.ID
-------^^^^^^^^_^^
VALUES (@RequestTo,@RequestFrom,@RequestedByName)
现在您可以使用 ExecuteScalar 获取插入的值
protected void btn_Request(object sender, EventArgs e)
{
int id= 0;
string insertCmd = "INSERT INTO tblPRRequest (RequestTo,RequestFrom,RequestedByName) " +
"output inserted.ID" +
"VALUES (@RequestTo,@RequestFrom,@RequestedByName)";
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
using (SqlCommand sqlcmd = new SqlCommand(insertCmd, conn))
{
sqlcmd.Parameters.AddWithValue("@RequestTo", lblPurchasingDept.Text);
sqlcmd.Parameters.AddWithValue("@RequestFrom", ddlDept.SelectedItem.Text);
sqlcmd.Parameters.AddWithValue("@RequestedByName", SUserName.Text);
id = (int)sqlcmd.ExecuteScalar(); //the result is of Object type, cast it safely
}
}
Debug.WriteLine(id.ToString()); // Access it like this
}
通过参考@Mx.Wolf 的示例答案,我做了一些修改以获得正确的答案,下面是有效的代码:
protected void btn_Request(object sender, EventArgs e)
{
object id ;
string insertCmd = "INSERT INTO tblPRRequest (RequestTo,RequestFrom,RequestedByName) " +
"output inserted.PRReqID " +
"VALUES (@RequestTo,@RequestFrom,@RequestedByName)";
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
using (SqlCommand sqlcmd = new SqlCommand(insertCmd, conn))
{
sqlcmd.Parameters.AddWithValue("@RequestTo", lblPurchasingDept.Text);
sqlcmd.Parameters.AddWithValue("@RequestFrom", ddlDept.SelectedItem.Text);
sqlcmd.Parameters.AddWithValue("@RequestedByName", SUserName.Text);
id = sqlcmd.ExecuteScalar(); //the result is of Object type, cast it safely
}
}
Debug.WriteLine(id.ToString()); // Access it like this