Daniel Koh

Daniel Koh

  • NA
  • 60
  • 15.1k

System.Data.SqlClient.SqlException

Oct 10 2017 12:03 AM
 Hi guys i'm learning C# now and am trying to create a login application and one of the lines for sql gave me this error
 
System.Data.SqlClient.SqlException: 'An attempt to attach an auto-named database for file C: \*****\**********\Documents\Data.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.'
 
this is the code i was learning:
 
SqlConnection sqlConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \*****\**********\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30;");
 
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("Select Count(*) From Login where Username='" + textBox1.Text + "' and Password = '" + textBox2.Text + "'",sqlConnection);
 
DataTable dataTable = new DataTable();
 
sqlDataAdapter.Fill(dataTable); 
if (dataTable.Rows[0][0].ToString() == "1")
{
this.Hide();
Form2 MainMenu = new Form2();
MainMenu.Show();
}
else
{
MessageBox.Show("Please check your username and password");
}
 
HELP

Answers (2)