VB.Net执行存储过程错误消息

时间:2015-12-05 01:58:11

标签: sql-server vb.net stored-procedures

注意:感谢这里的反馈是我的更正/工作代码: 我正在Visual Studio VB.NET中执行SQL Server存储过程,该过程应该采用我的表单文本字段并将信息添加到数据库中。

我收到状态的错误消息

  

过程spVehicleAddNew没有提供参数和参数。

以下是我的存储过程以及可视化基本代码的代码。我是新手执行存储过程,并感谢任何帮助搞清楚此错误消息。谢谢!

Private Sub btnAddToInventory_Click(sender As Object, e As EventArgs) Handles btnAddToInventory.Click
        Try
            Dim sqlConnection1 As New SqlConnection("Data Source=GALE-PC1\SQLEXPRESS2012;Initial Catalog=VehicleCheckout;Integrated Security=True")
            Dim cmd As New SqlCommand
            cmd.Parameters.Clear()
            cmd.CommandText = "spVehicleAddNew"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = sqlConnection1
            cmd.Parameters.AddWithValue("@VehicleTagNo", txtVehicleTagNo.Text)
            cmd.Parameters.AddWithValue("@VehicleVIN", txtVehicleVIN.Text)
            cmd.Parameters.AddWithValue("@VehicleLicense", txtVehicleLicense.Text)
            cmd.Parameters.AddWithValue("@Make", txtMake.Text)
            cmd.Parameters.AddWithValue("@Model", txtModel.Text)
            cmd.Parameters.AddWithValue("@VehicleYear", txtYear.Text)
            cmd.Parameters.AddWithValue("@Color", txtColor.Text)
            cmd.Parameters.AddWithValue("@Seats", txtSeats.Text)
            cmd.Parameters.AddWithValue("@Available", ckbAvailable.Text)
            cmd.Parameters.AddWithValue("@VehicleComments", txtAvailableComments.Text)
            cmd.Parameters.AddWithValue("@VehicleNotes", txtVehicleNotes.Text)
            cmd.Parameters.AddWithValue("@CurrentMileage", txtCurrentMileage.Text)
            cmd.Parameters.AddWithValue("@NextServiceMileage", txtNextServiceMileage.Text)
            cmd.Parameters.AddWithValue("@Scrap", ckbScrap.Text)
            cmd.Parameters.AddWithValue("@GPS", ckbGPS.Text)
            cmd.Parameters.AddWithValue("@CassettePlayer", ckbCassettePlayer.Text)
            cmd.Parameters.AddWithValue("@CDPlayer", ckbCDPlayer.Text)
            cmd.Parameters.AddWithValue("@DVDPlayer", ckbDVDPlayer.Text)
            cmd.Parameters.AddWithValue("@Convertible", ckbConvertible.Text)
            sqlConnection1.Open()
            cmd.ExecuteNonQuery()
            sqlConnection1.Close()
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

存储过程:

Create PROC spVehicleAddNew
    @VehicleTagNo varchar(30),
    @VehicleVIN varchar(100),
    @VehicleLicense varchar(50),
    @Make varchar(30),
    @Model varchar(30),
    @VehicleYear int,
    @Color varchar(20),
    @Seats int,
    @Available bit,
    @AvailableComments varchar(1000),
    @VehicleNotes varchar(1050),
    @CurrentMileage int,
    @NextServiceMileage int,
    @Scrap bit,
    @GPS bit,
    @CassettePlayer bit,
    @CDPlayer bit,
    @DVDPlayer bit,
    @Convertible bit,
    @VehicleID int
   AS   
   BEGIN TRY
    BEGIN TRAN;
        INSERT Vehicle
        (VehicleTagNo, VehicleVIN, VehicleLicense, Make, Model, VehicleYear, Color, Seats, Available, AvailableComments, VehicleNotes, CurrentMileage, NextServiceMileage, Scrap)
        VALUES(@VehicleTagNo, @VehicleVIN, @VehicleLicense, @Make, @Model, @VehicleYear, @Color, @Seats, @Available, @AvailableComments, @VehicleNotes, @CurrentMileage, @NextServiceMileage, @Scrap);
        SET @VehicleID = @@IDENTITY;
        INSERT Amenities
        (GPS, CassettePlayer, CDPlayer, DVDPlayer, Convertible, VehicleID)
        VALUES(@GPS, @CassettePlayer,@CDPlayer,@DVDPlayer,@Convertible, @VehicleID);
            COMMIT TRAN;
END TRY
BEGIN CATCH
    ROLLBACK TRAN;
END CATCH;`

1 个答案:

答案 0 :(得分:1)

您的存储过程不包含任何参数...您错误地将它们声明为过程中的变量而是... 此外,您不需要将VehicleId定义为参数,因为您将其用作过程中的标识列... 它应该被定义为

Create PROC spVehicleAddNew(
    @VehicleTagNo varchar(30),
    @VehicleVIN varchar(100),
    @VehicleLicense varchar(50),
    @Make varchar(30),
    @Model varchar(30),
    @VehicleYear int,
    @Color varchar(20),
    @Seats int,
    @Available bit,
    @AvailableComments varchar(1000),
    @VehicleNotes varchar(1050),
    @CurrentMileage int,
    @NextServiceMileage int,
    @Scrap bit,
    @GPS bit,
    @CassettePlayer bit,
    @CDPlayer bit,
    @DVDPlayer bit,
    @Convertible bit)
as
    BEGIN TRY
Declare @VehicleID int

    BEGIN TRAN;
        INSERT Vehicle
        (VehicleTagNo, VehicleVIN, VehicleLicense, Make, Model, VehicleYear, Color, Seats, Available, AvailableComments, VehicleNotes, CurrentMileage, NextServiceMileage, Scrap)
        VALUES(@VehicleTagNo, @VehicleVIN, @VehicleLicense, @Make, @Model, @VehicleYear, @Color, @Seats, @Available, @AvailableComments, @VehicleNotes, @CurrentMileage, @NextServiceMileage, @Scrap);
        SET @VehicleID = @@IDENTITY;
        INSERT Amenities
        (GPS, CassettePlayer, CDPlayer, DVDPlayer, Convertible, VehicleID)
        VALUES(@GPS, @CassettePlayer,@CDPlayer,@DVDPlayer,@Convertible, @VehicleID);
            COMMIT TRAN;
END TRY
BEGIN CATCH
    ROLLBACK TRAN;
END CATCH;