SqlClient.SqlCommand 轰炸

发布于 2024-11-05 01:27:43 字数 1032 浏览 0 评论 0原文

对 .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 技术交流群。

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

发布评论

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

评论(5

伴梦长久 2024-11-12 01:27:43

您是否尝试过像这样创建参数:

    Dim param as new SqlParameter()
    param.ParameterName = "ParamName"
    param.Value         = PolicyNo
    Command.Parameters.Add(param)

Have you tried creating the parameter like this:

    Dim param as new SqlParameter()
    param.ParameterName = "ParamName"
    param.Value         = PolicyNo
    Command.Parameters.Add(param)
攒一口袋星星 2024-11-12 01:27:43

不完全确定这是否是问题所在,但是您没有为参数对象指定名称,仅传递将引发异常的值,您还必须为参数指定名称

Command.Parameters.Add(new SqlParameter("PolicyNo",PolicyNo))

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(new SqlParameter("PolicyNo",PolicyNo))
深爱不及久伴 2024-11-12 01:27:43

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

唯憾梦倾城 2024-11-12 01:27:43

尝试:

Command.Parameters.AddWithValue("@PolicyNo", PolicyNo);

Try:

Command.Parameters.AddWithValue("@PolicyNo", PolicyNo);
要走干脆点 2024-11-12 01:27:43

也许您可以尝试显式键入您的参数:

Command.Parameters.Add("@PolicyNumber", SqlDbType.Int)
Command.Parameters("@PolicyNumber").Value = PolicyNo

并确保您的参数名称和数据类型与存储过程中的内容匹配。

Maybe you could try explicitly typing your parameter:

Command.Parameters.Add("@PolicyNumber", SqlDbType.Int)
Command.Parameters("@PolicyNumber").Value = PolicyNo

And make sure your parameter name and datatype match what's in the stored procedure.

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