我只想使用SqlTransaction方法获取主要对象。

这是我到目前为止的内容:

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);
}

===============>>#1 票数:1 已采纳

为了插入包括“自动增量”值在内的默认值,您需要在INSERT使用DEFAULT VALUES 另外,您至少需要有一个; 在您的INSERTSELECT SCOPE_IDENTITY() (您的当前代码在两个语句之间没有空格或分号):

string strITStaff = "INSERT INTO ITReq DEFAULT VALUES;  SELECT SCOPE_IDENTITY()";

这会将所有默认值插入ITReq表,然后从该表中选择新插入的IDENTITY

  ask by user3195396 translate from so

未解决问题?本站智能推荐: