0
我一直在試圖將最後一個插入的ID插入到我的代碼中,但是如果參數類型分別設置爲int 32或int64,則返回11或12作爲返回的ID。獲取最後插入的行的ID
這是我使用
CREATE procedure [dbo].[Sophia_FND_SalesOrder_CREATE](
@contact int,
@deliveryAddress varchar(50),
@Charge float,
@GrossTotal float,
@PaymentMode varchar(50),
@organisationId int,
@userid varchar(50),
@amtpaid float,
@SalesOrderId int OUTPUT
)
as
begin
insert into Sophia_FND_SalesOrder(ContactId, deliveryAddress, Charge, GrossTotal, PaymentMode, organisationId,userid, AmountPaid)
values(@contact,@deliveryAddress, @Charge, @GrossTotal, @PaymentMode, @organisationId, @userid, @amtpaid)
SET @SalesOrderId = SCOPE_IDENTITY()
RETURN @SalesOrderId
end
這是我用在我的課的功能的過程
Public Shared Function Create_salesOrder(ByVal contact As Integer, ByVal deliveryAddress As String, ByVal Charge As Double, ByVal GrossTotal As Double, ByVal PaymentMode As String, ByVal organisationId As Integer, ByVal userid As String, ByVal amtpaid As Double) As Integer
Dim comm As DbCommand = CreateCommand17()
comm.CommandText = "Sophia_FND_SalesOrder_CREATE"
AddParameter(comm, "@contact", contact, DbType.Int64)
AddParameter(comm, "@deliveryAddress", deliveryAddress, DbType.String)
AddParameter(comm, "@Charge", Charge, DbType.Double)
AddParameter(comm, "@GrossTotal", GrossTotal, DbType.Double)
AddParameter(comm, "@PaymentMode", PaymentMode, DbType.String)
AddParameter(comm, "@organisationId", organisationId, DbType.Int64)
AddParameter(comm, "@userid", userid, DbType.String)
AddParameter(comm, "@amtpaid", amtpaid, DbType.Double)
AddParameter(comm, "@SalesOrderId", DbType.Int64, ParameterDirection.Output)
Dim id As Integer
Dim insertResult As Int32 = -1
Try
insertResult = ExecuteNonQuery6(comm)
id = Convert.ToInt64(comm.Parameters("@SalesOrderId").Value)
Catch ex As Exception
Throw ex
End Try
'Return insertResult <> -1
Return id
End Function
這是我後面把它在我的代碼
Dim sale_id As Integer
If IsNothing(Session("soid")) Then
sale_id = Create_salesOrder(CInt(drpcontact.SelectedValue), txtdelivery.Text, lblCharge_unformated.Text, total, drpPayMode.SelectedItem.Text, SessionWrapper.OrganisationId, SessionWrapper.LoggedInUserID, CDbl(txtpayment.Text))
If Not IsNothing(sale_id) Then
Session("soid") = sale_id
Me.MessageBoard1.MessagePanel.CssClass = "errorMessage"
Me.MessageBoard1.MessagePanel.Visible = True
Me.MessageBoard1.MessageLabel.ForeColor = Drawing.Color.Green
Me.MessageBoard1.MessageLabel.Text = "done"
FillGridview()
Else
Me.MessageBoard1.MessagePanel.CssClass = "errorMessage"
Me.MessageBoard1.MessagePanel.Visible = True
Me.MessageBoard1.MessageLabel.ForeColor = Drawing.Color.Red
Me.MessageBoard1.MessageLabel.Text = "Error Occured on creating sales"
End If
end if
我發現了一種工作方式,但語法完全不同,我想堅持我的語法。這個問題應該在我的班級職能,但我似乎不知道我犯了什麼錯誤。
每當我運行此,我要麼得到11或12返回ID這是完全錯誤的
感謝
我沒有看到任何關於此代碼的問題。你能詳細解釋一下這段代碼的問題嗎? – Praveen
我在SQL Studio測試,一切運作良好,但是當我測試的功能, 我要麼得到11或12返回的ID始終是11,如果參數類型('AddParameter(COMM,「@SalesOrderId」的DbType .Int32,ParameterDirection.Output)' )被設置爲int32,它是12如果參數類型('AddParameter(COMM 「@SalesOrderId」,DbType.Int64,ParameterDirection.Output)' )的INT64 @praveen – Nipek
你好Nipek,使用ExecuteScaler而不是ExecuteNonQuery並試試,希望它能起作用。 –