-2
I’m having trouble at the time I need to query information in the database through my sql command in my Asp.Net Core application. I’ve done research here at Stackoverflow and I can’t find.
Error: System.Data.SqlClient.SqlException: 'É necessário declarar a variável escalar "@ClienteId".'
Method responsible for carrying out this consultation:
public Cliente ConsultarCliente(long? id)
{
Cliente item;
DAL acessarBanco = new DAL(); // Instanciando objeto acessar banco para realizar a conexão de dados.
string sqlConsulta = string.Format("SELECT * FROM Estudo.Clientes WHERE ClienteId = @ClienteId");
//string sqlConsulta = $"Select ClienteId, Nome, CpfCnpj, Email, Senha From Estudo.Clientes Where ClienteId ='{ClienteId}' order by Nome asc"; // variavel sqlConsulta para trazer todas os registros da tabela Clientes.
DataTable dt = acessarBanco.RetDataTable(sqlConsulta);
item = new Cliente
{
ClienteId = dt.Rows[0]["ClienteId"].ToString(),
Nome = dt.Rows[0]["Nome"].ToString(),
CpfCnpj = dt.Rows[0]["CpfCnpj"].ToString(),
Email = dt.Rows[0]["Email"].ToString(),
Senha = dt.Rows[0]["Senha"].ToString()
};
return item;
}
DAL class with my connection:
public DAL()
{
Conexao = new SqlConnection(StringConexao);
Conexao.Open();
}
public DataTable RetDataTable(string sql)
{
DataTable data = new DataTable();
SqlCommand Executar = new SqlCommand(sql, Conexao);
SqlDataAdapter da = new SqlDataAdapter(Executar);
da.Fill(data);
return data;
}
Antonio, good night to you. I’m trying to call a record by the id parameter, by identifying the id 5 for example it brings me with the fields filled in so I can perform this change..
– Thiago Correa
worked out here thanks.
– Thiago Correa