iHunter1895
*.Open() works with Windows Auth, but not SQL Auth. Credentials correct.
private void loginButton_Click(object sender, RoutedEventArgs e) { //1.Address of SQL server and databaseSystem.Data.SqlClient.SqlException: 'Cannot open database "VisualUI_DB.mdf" requested by the login. The login failed. Login failed for user '[THE LOGIN]'.'
//////////string connString = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=VisualUI_DB.mdf;User ID=[Redacted];Password=[Redacted];Connect Timeout=30"; << DOESNT WORK string connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users[Redacted]\ [Redacted]\ [Redacted]\Database_VisualUI_Practice\Database_VisualUI_Practice\VisualUI_DB.mdf;Integrated Security=True;Connect Timeout=30"; // DOES WORK // 2. Establish connection using (SqlConnection conn = new SqlConnection(connString)) { // 3. Open connection conn.Open(); // 4. Prepare query string Email = emailText.Text, Pw = passwordText.Text; string query = "SELECT * FROM Users WHERE Email = '" + Email.Trim() + "' AND Password = '" + Pw.Trim() + "'"; // 5. Execute query SqlCommand cmd = new SqlCommand(query, conn); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read() == true) { UserDashboard page = new UserDashboard(); WPFUtils.SwitchToNewWindow(this, page); } else { MessageBox.Show("Your email and/or password are incorrect. Try again."); } // 6. Close Connection conn.Close(); } }
17 replies