在存储过程中同时插入和选择

时间:2013-05-25 07:43:34

标签: sql database vb.net stored-procedures

我有以下存储过程:

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的select语句有关。

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

任何建议家伙!感谢

2 个答案:

答案 0 :(得分:1)

我认为ID表的Maintainance应该是自动递增的。为实现这一目标,您可以将Is Identity设置为Yes

在插入的最后,如果你想要ID,那么

Select SCOPE_IDENTITY()

如果您想手动增加ID,请尝试以下操作:

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

然后使用@newid进行插入,最后使用Select @newid

答案 1 :(得分: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()