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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确认您设置的每个参数值都不是 Nothing。没有值的参数可能会导致缺少参数错误。 If() 的两个参数版本可以提供帮助:
如果不是 Nothing,则返回 Repair.Specification,否则返回 ""。
另外,您应该考虑使用Parameters.Add().Value()而不是.AddWithValue(),如下所示:
当您使用字符串以外的类型时,这非常有用。
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:
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:
This is really useful when you're working with types other than string.
尝试将 sqlcommand 命令类型设置为存储过程。
Try setting sqlcommand commandtype to storedprocedure.
确保尊重大小写,例如
错误
就是正确
make sure to respect case sensitivity, for an example
is wrong
is right