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

发布于 2024-11-09 10:32:37 字数 2254 浏览 0 评论 0原文

执行“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

I'm getting an error when the 'insertcommand' is executed telling me that I'm missing the parameters for the stored procedure. Do I need to put the parameter names in the Sql statement after the procedure as if I were calling it in SQL? I saw an example online that just added the parameters like I have here, but this doesn't work? I also put the sql state for the stored procedure below the '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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑轻吟梦一曲 2024-11-16 10:32:37

确认您设置的每个参数值都不是 Nothing。没有值的参数可能会导致缺少参数错误。 If() 的两个参数版本可以提供帮助:

insertcommand.Parameters.AddWithValue("@Specification", If(repair.Specification, ""))

如果不是 Nothing,则返回 Repair.Specification,否则返回 ""。

另外,您应该考虑使用Parameters.Add().Value()而不是.AddWithValue(),如下所示:

insertcommand.Parameters.Add("@ClubType", SqlDbType.VarChar, 50).Value = If(repair.TypeOfClub, "")

当您使用字符串以外的类型时,这非常有用。

Confirm that every parameter value you are setting is not Nothing. Parameters with no value can cause the missing parameter error. There's a two-argument version of If() that helps:

insertcommand.Parameters.AddWithValue("@Specification", If(repair.Specification, ""))

this will return repair.Specification if it is not Nothing, and "" otherwise.

also, you should consider using Parameters.Add().Value() instead of .AddWithValue(), like so:

insertcommand.Parameters.Add("@ClubType", SqlDbType.VarChar, 50).Value = If(repair.TypeOfClub, "")

This is really useful when you're working with types other than string.

表情可笑 2024-11-16 10:32:37

尝试将 sqlcommand 命令类型设置为存储过程。

Try setting sqlcommand commandtype to storedprocedure.

乱世争霸 2024-11-16 10:32:37

确保尊重大小写,例如

    insertcommand.Parameters.AddWithValue("@GripType", repair.TypeOfGrip)

错误

     insertcommand.Parameters.AddWithValue("@Griptype", repair.TypeOfGrip)

就是正确

make sure to respect case sensitivity, for an example

    insertcommand.Parameters.AddWithValue("@GripType", repair.TypeOfGrip)

is wrong

     insertcommand.Parameters.AddWithValue("@Griptype", repair.TypeOfGrip)

is right

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文