-4

I am trying to connect to a database for my Login wpf.

SqlConnectionStringBuilder mConnectionBuilder = new SqlConnectionStringBuilder
{
    DataSource = @"(LocalDB)\MSSQLLocalDB",
    InitialCatalog = "UserData"               
};

using (SqlConnection mConnection = new SqlConnection(mConnectionBuilder.ConnectionString))
{
    SqlCommand mCommand = new SqlCommand
    {
        Connection = mConnection,
        CommandText = "Select Id, Username, E-Mail, Password from Users"
    };

    if (mConnection.State == ConnectionState.Closed)
    {
        mConnection.Open();
    }

    using (SqlDataReader mReader = mCommand.ExecuteReader())
    {
        while (mReader.Read())
        {
            string mUsername = Convert.ToString(mReader.GetValue(1));
            string mPassword = Convert.ToString(mReader.GetValue(2));

            if (txtLoginUsername.Text == mUsername && txtLoginPassword.Text == mPassword)
            {
                loginSuccessful = true;
            }
            else
            {
                MessageBox.Show("Wrong username or password");
            }
        }
    }
}

I created the database in Visual Studio and named it UserData. But at mConnection.Open() i get an SqlException "Cannot open database "UserData" requested by the login. The login failed. Login failed for user (myUserName)". I know there are different ways to use SQL with C# but I want it to work that way really hard because I waisted way too much time to find a solution for this problem. I hope i can get some helpful information here :)

  • https://stackoverflow.com/questions/1054022/best-way-to-store-password-in-database – mjwills Dec 26 '20 at 13:58
  • What is the value of `mConnectionBuilder.ConnectionString`? – mjwills Dec 26 '20 at 13:59
  • User passwords shouldn't be stored in plain text. Once the connection issue is fixed, ensure that the passwords are encrypted. – user9938 Dec 26 '20 at 14:09
  • A SQL Database is an mdf file. You need to add the location of the mdf file to the connection string which is the database (the extension mdf is optional) property. – jdweng Dec 26 '20 at 14:13
  • 2
    @user9938 That's incorrect. Passwords should never be encrypted, they need to be hashed. If you meant the latter, please do not confuse the terms, those operations are different (encryption is two-way, hashing is one-way) – Camilo Terevinto Dec 26 '20 at 14:42
  • @jdweng i did that but now mCommand.ExecuteReader throws the Exception "Invalid object name". Idk that looks like my CommandText is wrong – BusinessMan13 Dec 26 '20 at 15:50
  • Now there is an error in the server-explorer, it says my database 'PATH' does not exist, i checked, the path is correct and the database does exist... im confused – BusinessMan13 Dec 26 '20 at 16:01
  • Nvmd @jdweng your solution worked after fixing some new errors thank you so much :) – BusinessMan13 Dec 26 '20 at 16:10

1 Answers1

-1
String connetionString = null;
SqlConnection cnn;

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
cnn = new SqlConnection(connetionString);
try
{
    cnn.Open();
    MessageBox.Show ("Connection Open ! ");
    cnn.Close();
}
catch (Exception ex)
{
    MessageBox.Show("Can not open connection ! ");
}
greybeard
  • 2,102
  • 7
  • 24
  • 58