khalid4775 Ответов: 2

Не вызываемый член 'sqlcommand.parameters' не может использоваться как метод.


привет...

Я получаю эту ошибку :

CS1955	Non-invocable member 'SqlCommand.Parameters' cannot be used like a method.


И

CS0103	The name 'Conversions' does not exist in the current context


Это мой класс :

<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Project5.NewFolder1
{
    class DatabaseManager
    {
        private SqlConnection _connection;
        private readonly string _connectionString;
        public SqlConnection Connection
        {
            get
            {
                if (_connection is null)
                    _connection = new SqlConnection(_connectionString);
                return _connection;
            }
        }

        public DatabaseManager()
        {
            _connectionString = ConfigurationManager.ConnectionStrings["connSQLServer"].ConnectionString;
        }

        public DatabaseManager(string ConnectionString)
        {
            this._connectionString = _connectionString;
        }

        public void Close()
        {
            if (_connection is object && _connection.State == ConnectionState.Open)

                _connection.Close();
        }

        public int FillTable(ref SqlCommand cmd, ref DataTable dt)
        {
            int retval = -1;
            dt = new DataTable();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Connection;
            var da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            if (dt is object)
                retval = dt.Rows.Count;
            return retval;
        }

        public int ExecuteReader(ref SqlCommand cmd, ref SqlDataReader dr)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            return ExeReader(ref cmd, ref dr);
        }

        public int ExecuteReader(string sql, ref SqlDataReader dr)
        {
            var cmd = new SqlCommand(sql)
            {
                CommandType = CommandType.Text,
                Connection = Connection
            };
            int retval = ExecuteReader(ref cmd, ref dr);
            return retval;
        }

        private int ExeReader(ref SqlCommand cmd, ref SqlDataReader dr)
        {
            int retval = -1;
            cmd.Connection = Connection;
            try
            {
                if (cmd.CommandType == CommandType.StoredProcedure)
                {
                    var pr = new SqlParameter("@retval", SqlDbType.Int) { Direction = ParameterDirection.ReturnValue };
                    cmd.Parameters.Add(pr);
                }

                if (cmd.Connection.State == ConnectionState.Closed)
                    cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                if (cmd.CommandType == CommandType.StoredProcedure)
                    retval = cmd.Parameters("@retval").Value;   
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                Close();
            }

            return retval;
        }
        public int ExecuteNonQuery(ref SqlCommand cmd)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            return ExeNonQuery(ref cmd);
        }

        public int ExecuteNonQuery(ref string sql)
        {
            var cmd = new SqlCommand(sql)
            {
                CommandType = CommandType.Text,
                Connection = Connection
            };
            int retval = ExeNonQuery(ref cmd);
            return retval;
        }

        private int ExeNonQuery(ref SqlCommand cmd)
        {
            int retval = -1;
            cmd.Connection = Connection;
            try
            {
                if (cmd.CommandType == CommandType.StoredProcedure)
                {
                    var pr = new SqlParameter("@retval", SqlDbType.Int) { Direction = ParameterDirection.ReturnValue };
                    cmd.Parameters.Add(pr);
                }

                if (cmd.Connection.State == ConnectionState.Closed)
                    cmd.Connection.Open();
                retval = cmd.ExecuteNonQuery();
                if (cmd.CommandType == CommandType.StoredProcedure)

                    retval = cmd.Parameters("@retval").Value;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                Close();
            }

            return retval;
        }
        public object ExecuteScalar(ref SqlCommand cmd)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            return ExeScalar(ref cmd);
        }

        public object ExecuteScalar(string sql)
        {
            var cmd = new SqlCommand(sql)
            {
                CommandType = CommandType.Text,
                Connection = Connection
            };
            int retval = Conversions.ToInteger(ExecuteScalar(ref cmd));
            return retval;
        }

        private object ExeScalar(ref SqlCommand cmd)
        {
            object retval;
            cmd.Connection = Connection;
            try
            {
                if (cmd.Connection.State == ConnectionState.Closed)
                    cmd.Connection.Open();
                retval = cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                Close();
            }

            return retval;
        }
    }

}



Спасибо....

Что я уже пробовал:

Что вызывает эти ошибки ?

<pre>CS1955	Non-invocable member 'SqlCommand.Parameters' cannot be used like a method.


И

CS0103	The name 'Conversions' does not exist in the current context

2 Ответов

Рейтинг:
2

khalid4775

Спасибо, друг, анализировать

да, я пытался поменять тормоза на ["@retval"]

но все же ошибка есть

о другой ошибке можно узнать, изменив код на

int retval = Convert.ToInt32(ExecuteScalar(ref cmd));


Рейтинг:
1

OriginalGriff

Посмотрите на свой код:

if (cmd.CommandType == CommandType.StoredProcedure)
   retval = cmd.Parameters("@retval").Value;  

Параметры-это коллекция, а не метод:
if (cmd.CommandType == CommandType.StoredProcedure)
   retval = cmd.Parameters["@retval"].Value;  


[редактировать]
И это "конвертировать", а не "конверсии"
[/редактировать]