[英]How do I insert an empty row in a table with an auto-Increment primary key?
I just want to grab the primary using the SqlTransaction
method. 我只想使用
SqlTransaction
方法获取主要对象。
This is what I have so far: 这是我到目前为止的内容:
using (SqlTransaction transaction = connection.BeginTransaction())
{
// insert IT Requirements
string strITStaff = "INSERT INTO ITReq";
strITStaff += "SELECT SCOPE_IDENTITY()";
SqlCommand cmdITStaff = new SqlCommand(strITStaff, connection, transaction);
ddlITSupport.SelectedValue.ToString());
int ITStaffReq = Convert.ToInt32(cmdITStaff.ExecuteScalar());
// update lanGaymen
string strUpdatePhaseSix = "UPDATE lanGaymen SET ";
strUpdatePhaseSix += "itReqID = @updateITReq ";
strUpdatePhaseSix += "WHERE seasonID = @compareSeason";
SqlCommand cmdUpdatePhaseSix = new SqlCommand(strUpdatePhaseSix, connection, transaction);
cmdUpdatePhaseSix.Parameters.AddWithValue("@updateITReq", ITStaffReq);
}
In order to insert the default values including the "auto-increment" value, you need to use DEFAULT VALUES
in your INSERT
. 为了插入包括“自动增量”值在内的默认值,您需要在
INSERT
使用DEFAULT VALUES
。 Also, you need to have at least a ;
另外,您至少需要有一个
;
between your INSERT
and your SELECT SCOPE_IDENTITY()
(your current code doesn't have a space nor a semicolon between the two statements): 在您的
INSERT
和SELECT SCOPE_IDENTITY()
(您的当前代码在两个语句之间没有空格或分号):
string strITStaff = "INSERT INTO ITReq DEFAULT VALUES; SELECT SCOPE_IDENTITY()";
This will insert all default values into the ITReq
table, and then select the newly inserted IDENTITY
from that table. 这会将所有默认值插入
ITReq
表,然后从该表中选择新插入的IDENTITY
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.