简体   繁体   English

从ADO.Net输入参数调用存储过程时出错

[英]Error invoking stored procedure with input parameter from ADO.Net

I am using VSTS 2008 + C# + .Net 3.5 + ADO.Net. 我正在使用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. 错误消息说,@ Param1没有提供,但实际上它在我的代码中提供。 Any ideas what is wrong? 任何想法有什么问题吗?

System.Data.SqlClient.SqlException: Procedure or function 'Pr_Foo' expects parameter '@Param1', which was not supplied. 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);
            }
        }
    }

Thanks in advance, 提前致谢,

George 乔治

You did not add value to parameter. 您没有为参数添加值。 Signature of Add is Add(string parameterName, SqlDbType type, int size)... last parameter is size, not value. Add的签名是Add(字符串parameterName,SqlDbType类型,int大小)...最后一个参数是size,而不是值。 you can use method AddWithValue. 您可以使用方法AddWithValue。

MSDN Article MSDN文章

Try to replace your function by below code and check : 尝试用下面的代码替换您的函数并检查:

public static void Run()
        {
            try
            {
                _command.Parameters.AddWithValue("@Param1", 300101);
                _command.Parameters.AddWithValue("@Param2", 100);
                _command.Parameters.AddWithValue("@Param3", 200);

                _command.ExecuteScalar();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

Try taking the "@" symbol out of your .Add statements. 尝试从您的.Add语句中删除“ @”符号。 I never prepend the @ when adding parameters. 添加参数时,我永远不会在@前面加上@。

For example: 例如:

SqlParameter Param1 = _command.Parameters.Add("Param1", SqlDbType.Int, 300101);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM