使用来自 ADO.Net 的输入参数调用存储过程时出错
我正在使用 VSTS 2008 + C# + .Net 3.5 + ADO.Net。这是我的代码和相关的错误消息。错误消息显示,未提供@Param1,但实际上我的代码中提供了它。任何想法有什么问题吗?
System.Data.SqlClient.SqlException: 过程或函数“Pr_Foo”需要 参数“@Param1”,这不是 提供。
class Program
{
private static SqlCommand _command;
private static SqlConnection connection;
private static readonly string _storedProcedureName = "Pr_Foo";
private static readonly string connectionString = "server=.;integrated Security=sspi;initial catalog=FooDB";
public static void Prepare()
{
connection = new SqlConnection(connectionString);
connection.Open();
_command = connection.CreateCommand();
_command.CommandText = _storedProcedureName;
_command.CommandType = CommandType.StoredProcedure;
}
public static void Dispose()
{
connection.Close();
}
public static void Run()
{
try
{
SqlParameter Param1 = _command.Parameters.Add("@Param1", SqlDbType.Int, 300101);
Param1.Direction = ParameterDirection.Input;
SqlParameter Param2 = _command.Parameters.Add("@Param2", SqlDbType.Int, 100);
portal_SiteInfoID.Direction = ParameterDirection.Input;
SqlParameter Param3 = _command.Parameters.Add("@Param3", SqlDbType.Int, 200);
portal_RoleInfoID.Direction = ParameterDirection.Input;
_command.ExecuteScalar();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
static void Main(string[] args)
{
try
{
Prepare();
Thread t1 = new Thread(Program.Run);
t1.Start();
t1.Join();
Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\t" + ex.StackTrace);
}
}
}
提前致谢,
乔治
I am using VSTS 2008 + C# + .Net 3.5 + ADO.Net. Here is my code and related error message. The error message says, @Param1 is not supplied, but actually it is supplied in my code. Any ideas what is wrong?
System.Data.SqlClient.SqlException:
Procedure or function 'Pr_Foo' expects
parameter '@Param1', which was not
supplied.
class Program
{
private static SqlCommand _command;
private static SqlConnection connection;
private static readonly string _storedProcedureName = "Pr_Foo";
private static readonly string connectionString = "server=.;integrated Security=sspi;initial catalog=FooDB";
public static void Prepare()
{
connection = new SqlConnection(connectionString);
connection.Open();
_command = connection.CreateCommand();
_command.CommandText = _storedProcedureName;
_command.CommandType = CommandType.StoredProcedure;
}
public static void Dispose()
{
connection.Close();
}
public static void Run()
{
try
{
SqlParameter Param1 = _command.Parameters.Add("@Param1", SqlDbType.Int, 300101);
Param1.Direction = ParameterDirection.Input;
SqlParameter Param2 = _command.Parameters.Add("@Param2", SqlDbType.Int, 100);
portal_SiteInfoID.Direction = ParameterDirection.Input;
SqlParameter Param3 = _command.Parameters.Add("@Param3", SqlDbType.Int, 200);
portal_RoleInfoID.Direction = ParameterDirection.Input;
_command.ExecuteScalar();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
static void Main(string[] args)
{
try
{
Prepare();
Thread t1 = new Thread(Program.Run);
t1.Start();
t1.Join();
Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\t" + ex.StackTrace);
}
}
}
Thanks in advance,
George
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试用下面的代码替换您的函数并检查:
Try to replace your function by below code and check :
您没有为参数添加值。 Add 的签名是 Add(stringparameterName, SqlDbType type, int size)...最后一个参数是大小,而不是值。您可以使用方法 AddWithValue。
MSDN文章
You did not add value to parameter. Signature of Add is Add(string parameterName, SqlDbType type, int size)... last parameter is size, not value. you can use method AddWithValue.
MSDN Article
尝试从 .Add 语句中删除“@”符号。添加参数时我从不在前面添加@。
例如:
Try taking the "@" symbol out of your .Add statements. I never prepend the @ when adding parameters.
For example: