Error searching local database in Visual Studio 2019?

Asked

Viewed 52 times

-1

When trying to perform a query to a local database in Visual Studio, returns an exception.

inserir a descrição da imagem aqui


inserir a descrição da imagem aqui

Code I am using in FORM1, LOGIN button.

string sql = "(SELECT * FROM Login WHERE CNPJ ='"+textBox1.Text+"'AND senhaCorp ='"+textBox2.Text+"')";

SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\ThiagoSantana\source\repos\ArqX\ArqX\DBPaciente.mdf; Integrated Security = TrueconnectionString");
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = CommandType.Text;
SqlDataReader reader;
con.Open();
reader = cmd.ExecuteReader();
try
{
    if (!reader.Read())
    {
        MessageBox.Show("Login e senha incorreta");
    }
    else
    {
        this.Hide();
        ViewPrincipal vp = new ViewPrincipal();
        vp.Show();
    }
}
catch (Exception ex)
{
    MessageBox.Show("Erro: " + ex.ToString());
}
finally
{
    con.Close();
}
  • Don’t forget to vote and accept the answer if it has solved your problem.

  • Add the exception details to the question, please.

  • You managed to solve your problem?

1 answer

2

There’s a mistake in yours connection string. The line

SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\ThiagoSantana\source\repos\ArqX\ArqX\DBPaciente.mdf; Integrated Security = TrueconnectionString");

Should be (note the difference at the end of the line)

SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\ThiagoSantana\source\repos\ArqX\ArqX\DBPaciente.mdf; Integrated Security = True");

This may be a simple mistake, but there are other problems in the question you presented. Among them, the most serious is that vc is storing the password of users directly in the database without any encryption. Only through this piece of code is also possible to see that there are problems in the structuring and organization of your project. I recommended reading a lot about it and looking for more examples of how to organize your project, as well as how to protect sensitive data such as passwords.

  • Thanks tvdias, I made suggested change but the problem persists. I installed Sql server management 18 and also did not solve my problem.

  • @Thiagosantana Add the exception data to the question, since the ConnectionString is then not the only problem.

  • https://drive.google.com/open?id=1K8_fuAsQp07HPhRtNt7rYB5D1piIsax9

  • The file is not public, but you can edit the question and add the image by the editor itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.