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