Ошибка вызова хранимой процедуры с входным параметром из ADO.Net - PullRequest
1 голос
/ 07 июня 2010

Я использую 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);
            }
        }
    }

Заранее спасибо,

Джордж

Ответы [ 3 ]

2 голосов
/ 07 июня 2010

Вы не добавили значение к параметру.Сигнатура Add is Add (строковое имя_параметра, тип SqlDbType, int size) ... последний параметр - это размер, а не значение.Вы можете использовать метод AddWithValue.

MSDN Article

2 голосов
/ 07 июня 2010

Попробуйте заменить свою функцию приведенным ниже кодом и проверьте:

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);
            }
        }
1 голос
/ 07 июня 2010

Попробуйте убрать символ "@" из своих заявлений .Add. Я никогда не добавляю @ при добавлении параметров.

Например:

SqlParameter Param1 = _command.Parameters.Add("Param1", SqlDbType.Int, 300101);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...