获取最后插入行的id

时间:2017-06-20 09:18:26

标签: asp.net vb.net sql-server-2008 sql-insert

我一直在尝试将最后插入的id插入到我的代码中,但如果参数类型分别设置为int 32或int64,则返回11或12作为返回的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,这是完全错误的

感谢

1 个答案:

答案 0 :(得分:0)

不得不改变把它放在我的代码背后,这似乎对我有用。感谢

 Dim sale_id As Integer
            '  Dim saleslastid As Integer
            Dim salecreate_line As Boolean
            If IsNothing(Session("soid")) Then
                '  slastid = SalesOrder_id()
                '  saleslastid = StringHelper_2_0.ClearDBNullError(slastid.Rows(0)("id"))
                'sale_id = Create_salesOrder(CInt(drpcontact.SelectedValue), txtdelivery.Text, lblCharge_unformated.Text, total, drpPayMode.SelectedItem.Text, SessionWrapper.OrganisationId, SessionWrapper.LoggedInUserID, CDbl(txtpayment.Text))
                Dim strConnString As String = ConfigurationManager.ConnectionStrings("SophiaERP_Foundation_4").ConnectionString
                Dim con As New SqlConnection(strConnString)
                Dim cmd As New SqlCommand()
                cmd.CommandType = CommandType.StoredProcedure
                cmd.CommandText = "Sophia_FND_SalesOrder_CREATE"
                cmd.Parameters.Add("@contact", SqlDbType.Int).Value = CInt(drpcontact.SelectedValue)
                cmd.Parameters.Add("@deliveryAddress", SqlDbType.VarChar).Value = txtdelivery.Text
                cmd.Parameters.Add("@Charge", SqlDbType.Float).Value = lblCharge_unformated.Text
                cmd.Parameters.Add("@GrossTotal", SqlDbType.Float).Value = total
                cmd.Parameters.Add("@PaymentMode", SqlDbType.VarChar).Value = drpPayMode.SelectedItem.Text
                cmd.Parameters.Add("@organisationId", SqlDbType.Int).Value = SessionWrapper.OrganisationId
                cmd.Parameters.Add("@userid", SqlDbType.VarChar).Value = SessionWrapper.LoggedInUserID
                cmd.Parameters.Add("@amtpaid", SqlDbType.Float).Value = 0
                cmd.Parameters.Add("@duration", SqlDbType.Int).Value = CInt(lblduration.Text)
                cmd.Parameters.Add("@SalesOrderId", SqlDbType.Int).Direction = ParameterDirection.Output
                cmd.Connection = con
                Try
                    con.Open()
                    cmd.ExecuteNonQuery()

                    sale_id = cmd.Parameters("@SalesOrderId").Value


                Catch ex As Exception
                    Throw ex
                Finally
                    con.Close()
                    con.Dispose()
                End Try
end if