-1

我有以下存储过程:

create procedure Insert_Maintenance
(
@PlateNo nvarchar(10),
@MaintenanceType nvarchar(150),
@Name nvarchar(300),
@MaintenanceDate date,
@Qty int,
@IndivisualVal float,
@TotalVal float,
@Notes nvarchar(300)
)
as
insert into Maintenance(MaintenanceNo, PlateNo, MaintenanceType, AutoCenterNo,MaintenanceDate, Qty, IndivisualVal, TotalVal, Notes)
values ((1+(select Max(MaintenanceNo) from Maintenance)), @PlateNo, @MaintenanceType, (select AutoCenterNo from AutoCenter where Name = @Name), @MaintenanceDate, @Qty, @IndivisualVal, @TotalVal, @Notes)
Select Max(MaintenanceNo) AS MNo from Maintenance

用户将通过 VB.NET 接口插入新的维护操作。维护的信息将被插入到数据库中。维护的ID会增加1;如代码所示。既然是这种情况,在插入信息后,我想显示一条消息,告诉用户信息输入正确,并且此操作的 ID 是MNo.

基本上,问题不在于 VB.NET 代码,因为消息可以正常工作,但问题与向用户显示维护 ID 的选择语句有关。

VB.NET 中的代码

Dim ConnectString As String
        ConnectString = ""
        Dim connection As New SqlConnection(ConnectString)

        'Dim parm1 As SqlParameter
        'parm1 = New SqlParameter("@MaintenanceNo", SqlDbType.Int)
        'parm1.Value = MaintenanceNoTextBox.Text

        Dim parm2 As SqlParameter
        parm2 = New SqlParameter("@PlateNo", SqlDbType.NVarChar)
        parm2.Value = PlateNoComboBox.Text

        Dim parm3 As SqlParameter
        parm3 = New SqlParameter("@MaintenanceType", SqlDbType.NVarChar)
        parm3.Value = MaintenanceTypeTextBox.Text

        Dim parm4 As SqlParameter
        parm4 = New SqlParameter("@Name", SqlDbType.NVarChar)
        parm4.Value = NameComboBox.Text

        Dim parm5 As SqlParameter
        parm5 = New SqlParameter("@MaintenanceDate", SqlDbType.Date)
        parm5.Value = MaintenanceDateDateTimePicker.Value

        Dim parm6 As SqlParameter
        parm6 = New SqlParameter("@Qty", SqlDbType.Int)
        parm6.Value = QtyTextBox.Text

        Dim parm7 As SqlParameter
        parm7 = New SqlParameter("@IndivisualVal", SqlDbType.Float)
        parm7.Value = IndivisualValTextBox.Text

        Dim parm8 As SqlParameter
        parm8 = New SqlParameter("@TotalVal", SqlDbType.Float)
        parm8.Value = TotalValTextBox.Text

        Dim parm9 As SqlParameter
        parm9 = New SqlParameter("@Notes", SqlDbType.NVarChar)
        parm9.Value = NotesTextBox.Text

        Dim cmd As SqlCommand = New SqlCommand()
        cmd.Connection = connection
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "Insert_Maintenance"
        'cmd.Parameters.Add(parm1)
        cmd.Parameters.Add(parm2)
        cmd.Parameters.Add(parm3)
        cmd.Parameters.Add(parm4)
        cmd.Parameters.Add(parm5)
        cmd.Parameters.Add(parm6)
        cmd.Parameters.Add(parm7)
        cmd.Parameters.Add(parm8)
        cmd.Parameters.Add(parm9)

        Try
            connection.Open()
            cmd.ExecuteNonQuery()
            Dim dreader As SqlDataReader
            dreader = cmd.ExecuteReader()
            Dim a As String
            a = dreader("MNo").ToString
            MessageBox.Show("Information entered, ID is " + a)
            dreader.Close()
            'MaintenanceNoTextBox.Text = ""
            PlateNoComboBox.Text = ""
            MaintenanceTypeTextBox.Text = ""
            NameComboBox.Text = ""
            'MaintenanceDateDateTimePicker.Value = ""
            QtyTextBox.Text = ""
            IndivisualValTextBox.Text = ""
            TotalValTextBox.Text = ""
            NotesTextBox.Text = ""
        Catch ex As Exception
            MessageBox.Show("Something wrong  (" + ex.Message + ")")
        Finally
            connection.Close()
        End Try

任何建议家伙!谢谢

4

2 回答 2

1

我认为表应该是自动增量的IDMaintainance为此,您可以设置Is IdentityYes

在插入结束时,如果您想要 ID,则可以通过

Select SCOPE_IDENTITY()

如果你想ID手动增加,那么试试这个:

declare @newid int
Select  @newid = Max(ISNULL(MaintenanceNo, 0)) from Maintenance 
set @newid = @newid + 1

然后@newid用于插入并最后Select @newid

于 2013-05-25T08:02:08.317 回答
1

要解决您的问题,您只需更改一行并删除一堆不必要的代码

Dim cmd As SqlCommand = New SqlCommand() 
cmd.Connection = connection cmd.CommandType = CommandType.StoredProcedure 
cmd.CommandText = "Insert_Maintenance" 
.... you need to build the parameters collection here .....
Try 
     connection.Open() 
     Dim result = cmd.ExecuteScalar() ' <- Instead of ExecuteNonQuery '
     if result IsNot Nothing Then
        Console.WriteLine("The Mno is:" & Convert.ToInt32(result)
     End If

ExecuteScalar返回结果中第一行的第一列。这应该解决代码的 VB.NET 方面。当然,如果您不需要控制插入到列 MaintenanceNo 中的值,那么下一步要做的就是在字段属性中将 Identity 属性设置为 Yes。通过这种方式,您可以删除存储过程的最后几行,而忘记为 MaintenaceNo 字段传递任何内容

 INSERT INTO Maintenance(PlateNo, MaintenanceType, AutoCenterNo,MaintenanceDate, Qty, 
                         IndivisualVal, TotalVal, Notes)
           VALUES (@PlateNo, @MaintenanceType, 
                   (select AutoCenterNo from AutoCenter where Name = @Name), 
                   @MaintenanceDate, @Qty, @IndivisualVal, @TotalVal, @Notes)

 SELECT SCOPE_IDENTITY()
于 2013-05-25T08:06:05.037 回答