Connecting C # to SQL Server

1

I am trying to connect a program to the database hosted in SQL Server, but I am having problems ..

When the program runs, the login screen is the first one to appear, and it needs to open the second screen with options to add payments and etc in the database ..

My database has 3 "roles" where each one has permissions to change payment, change contract, etc. They are: Owner, Manager and Associate, and 3 users with the necessary passwords to which I have already given the " roles ". They are: mrlee (Owner), mngr (Manager) and asso (Associate).

How can I connect to the database through these users I created in SQL Server and "maintain" this connection when I open the other program window where I will make the changes? I need to keep the connection so that each user only does what is allowed in his role.

It's my first time trying to connect a program to a database and I'm having a lot of trouble ..

The DataBase name is ML074539

The login window looks like this:

public MainWindow()
    {
        InitializeComponent();
    }

    private void loginBtn_Click(object sender, RoutedEventArgs e)
    {
        if (usernameBox.Text == "" || passwordBox.Password == "")
        {
            MessageBox.Show("Please, check if you've entered your username and password.");
        } else
        {
            using (SqlConnection dataConnection = new SqlConnection())
            {
                try
                {
                    string userName = usernameBox.Text;
                    string password = passwordBox.Password;

                    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                    builder.DataSource = ".\MSSQLSERVER";
                    builder.InitialCatalog = "ML074539";
                    builder.UserID = userName;
                    builder.Password = password;
                    dataConnection.ConnectionString = builder.ConnectionString;
                    dataConnection.Open();


                    MultiLeaseWindow mw = new MultiLeaseWindow();
                    mw.Show();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unexpected error:" + ex.Message);
                }
            }
        }

    }

MultiLeaseWindow is where is the form to add the payment and etc.

When I press the "Login" button, regardless of the User Name and Password that I use (as long as it does not leave blank), I get an error saying that the server was not found or was not accessible and so on. >

I've researched a lot, but I still have not found an answer that I can get it to work.

    
asked by anonymous 09.07.2018 / 01:45

0 answers