VB.net中的参数化存储过程

问题描述

| 执行\ insertcommand \时出现错误,告诉我缺少存储过程的参数。是否需要像在SQL中一样在过程之后将参数名称放在Sql语句中?我在网上看到了一个示例,该示例只是像我在此处那样添加了参数,但这行不通吗?我还将存储过程的sql状态放在\'AddRepair Sub \'下面
Public Shared Sub AddRepair(ByVal repair As ClubRepair)

    Dim conn As SqlConnection = ClubRentalsDB.getconnection
    Dim insertcommand As New SqlCommand(\"AddRepair\",conn)

    insertcommand.Parameters.AddWithValue(\"@Name\",repair.Name)
    insertcommand.Parameters.AddWithValue(\"@ID\",repair.MemberID)
    insertcommand.Parameters.AddWithValue(\"@Phone\",repair.PhoneNumber)
    insertcommand.Parameters.AddWithValue(\"@Email\",repair.Email)
    insertcommand.Parameters.AddWithValue(\"@Work\",repair.WorkToBeDone)
    insertcommand.Parameters.AddWithValue(\"@Specification\",repair.Specification)
    insertcommand.Parameters.AddWithValue(\"@SoonestDate\",repair.SoonestCompletion)
    insertcommand.Parameters.AddWithValue(\"@PromisedDate\",repair.DatePromised)
    insertcommand.Parameters.AddWithValue(\"@ClubType\",repair.TypeOfClub)
    insertcommand.Parameters.AddWithValue(\"@GripType\",repair.TypeOfGrip)
    insertcommand.Parameters.AddWithValue(\"@NumOfClubs\",repair.NumOfClubs)
    insertcommand.Parameters.AddWithValue(\"@SpecialInstructions\",repair.SpecialInstructions)

    Try
        conn.Open()
        insertcommand.ExecuteReader()
    Catch ex As Exception
        MessageBox.Show(messageBad & ex.ToString)
    Finally
        conn.Close()
    End Try   
End Sub   
USE [ClubRentals]   
GO
 SET ANSI_NULLS ON
GO  
 SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE  [dbo].[AddRepair] (@Name Varchar(50),@ID varchar(20),@Phone varchar(50),@Email varchar(50),@Work varchar(20),@Specification varchar(MAX),@SoonestDate date,@PromisedDate Date,@ClubType varchar(50),@Griptype varchar(50),@NumOfClubs int,@SpecialInstructions varchar(MAX)) as

Insert into ClubRepair(Member_Name,Member_ID,Phone,Email,WorkToBeDone,Specification,SoonestPossibleCompletion,DatePromised,TypeOfClub,TypeOfGrips,NumOfClubs,SpecialInstructions)    

values(@Name,@ID,@Phone,@Email,@Work,@Specification,@SoonestDate,@PromisedDate,@ClubType,@GripType,@NumOfClubs,@SpecialInstructions)

GO
    

解决方法

确认您设置的每个参数值都不是Nothing。没有值的参数可能会导致缺少参数错误。有两个参数的If()版本可以帮助您:
insertcommand.Parameters.AddWithValue(\"@Specification\",If(repair.Specification,\"\"))
这将返回维修。如果不是Nothing,则指定规格,否则为\“ \”。 同样,您应该考虑使用Parameters.Add()。Value()而不是.AddWithValue(),如下所示:
insertcommand.Parameters.Add(\"@ClubType\",SqlDbType.VarChar,50).Value = If(repair.TypeOfClub,\"\")
当您使用字符串以外的类型时,这真的很有用。     ,尝试将sqlcommand命令类型设置为storedprocedure。     ,例如,确保尊重大小写
    insertcommand.Parameters.AddWithValue(\"@GripType\",repair.TypeOfGrip)
是错的
     insertcommand.Parameters.AddWithValue(\"@Griptype\",repair.TypeOfGrip)
是正确的     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...