SqlClient.SqlCommand 轰炸
对 .NET 来说还是个新手,但这对我来说看起来不错。我不明白发生了什么事。我正在尝试向 SqlCommand 添加 1 个参数。它是 SQL Server 中的一个整数,我将一个整数传递给它。
它挂在 Command.Parameters.Add(PolicyNo)
代码行上,并抛出“由于内部错误而无法处理”的消息。
我也尝试过
Command.Parameters.AddWithValue("@PolicyNumber", PolicyNo)
但无济于事。它会发出相同的错误消息。这是代码:
Public Function GetPolicyObj(ByVal PolicyNo As Integer) As PolicyInfo Implements ILetterWriter.GetPolicyObj
Dim SQLcon As New SqlClient.SqlConnection
'Establish the connection
SQLcon.ConnectionString = "Data Source=Hosed;Initial Catalog=MyCat;User ID=Goofy;Password=OpenSesame;"
SQLcon.Open()
Using Command As New SqlClient.SqlCommand("sp_GetPolicyInfo", SQLcon)
Command.CommandType = CommandType.StoredProcedure
Command.Parameters.Add(PolicyNo) 'Bombs on this line
Command.Connection.Open()
Dim reader As SqlClient.SqlDataReader = Command.ExecuteReader
有什么想法吗?
谢谢, 贾森
Still new to .NET, but this looks OK to me. I can't figure out what's going on. I'm trying to add 1 parameter to a SqlCommand. It is an integer in SQL Server, and I'm passing an integer to it.
It hangs on the Command.Parameters.Add(PolicyNo)
code line and kicks out an "unable to process due to internal error".
I've also tried
Command.Parameters.AddWithValue("@PolicyNumber", PolicyNo)
But to no avail. It bombs out with the same error message. Here's the code:
Public Function GetPolicyObj(ByVal PolicyNo As Integer) As PolicyInfo Implements ILetterWriter.GetPolicyObj
Dim SQLcon As New SqlClient.SqlConnection
'Establish the connection
SQLcon.ConnectionString = "Data Source=Hosed;Initial Catalog=MyCat;User ID=Goofy;Password=OpenSesame;"
SQLcon.Open()
Using Command As New SqlClient.SqlCommand("sp_GetPolicyInfo", SQLcon)
Command.CommandType = CommandType.StoredProcedure
Command.Parameters.Add(PolicyNo) 'Bombs on this line
Command.Connection.Open()
Dim reader As SqlClient.SqlDataReader = Command.ExecuteReader
Any Ideas?
Thanks,
Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否尝试过像这样创建参数:
Have you tried creating the parameter like this:
不完全确定这是否是问题所在,但是您没有为参数对象指定名称,仅传递将引发异常的值,您还必须为参数指定名称
Not entirely sure if this is the problem should be, but you are not giving the parameter object a name, only passing the value which will thrown an exception, you have to give the parameter a name as well
Command.Parameters.Add() 采用 SqlParameter 对象作为其参数。所以你不能只发送整数。
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx
Command.Parameters.Add() takes a SqlParameter object as its parameter. So you can't just send the integer.
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx
尝试:
Try:
也许您可以尝试显式键入您的参数:
并确保您的参数名称和数据类型与存储过程中的内容匹配。
Maybe you could try explicitly typing your parameter:
And make sure your parameter name and datatype match what's in the stored procedure.