0
我需要根据刚刚创建的ID重定向我的页面。 这是我的插入语句,它生成我需要为重定向获取的ID。使用刚刚创建的ID重定向
'SQL Insert: Product table
Dim sqlInsertProduct As String = "INSERT INTO Product
(ProductName, Status, CreateDate, ModifyDate,
CreateUser, ModifyUser, Price)
VALUES (@ProductName, @Status ,getdate(),
getdate(), @CreateUser, @ModifyUser, @Price)
;SELECT SCOPE_IDENTITY();"
Using cmd As New SqlCommand(sqlInsertProduct, cn)
Dim ProductID As String
cmd.Parameters.Add(New SqlParameter("@ProductName", txtNewProductName.Text))
cmd.Parameters.Add(New SqlParameter("@Status", StatusCheckbox.Checked))
cmd.Parameters.Add(New SqlParameter("@Price", txtPrice.Text))
cmd.Parameters.Add(New SqlParameter("@CreateUser",
System.Web.HttpContext.Current.User.Identity.Name))
cmd.Parameters.Add(New SqlParameter("@ModifyUser",
System.Web.HttpContext.Current.User.Identity.Name))
cn.Open()
ProductID = cmd.ExecuteScalar()
End Using
Response.Redirect("Product/Default.aspx?id=" + ProductID)
Response.Redirect在3个其他表插入3个不同的表后,一直处于页面底部。我认为做这个工作的唯一方法就是将重定向放入使用中,但是我不能,因为我需要插入其他表,并且在其他插入之前重定向并不合理。 。
我想如果我可以,但我花了很长时间研究'OUTPUT',我不明白这一点。 'OUTPUT'是我可以做这个重定向的唯一方法吗? – jlg
你不需要使用'OUTPUT'。只需执行上面的查询,将返回值分配给一个变量。 ExecuteScalar将返回第一行的第一列,在这种情况下,您将只返回一个值。你会传递两个查询。一个插入,另一个插入最后插入的标识。 –
我使用ExecuteScalar()编辑了上述内容,但我的应用程序无法看到ProductID。我认为这可能是因为连接在插入后不久被关闭,因为插入到此页面的其他表中。 – jlg