Connecting C# to SQL Server

Asked

Viewed 1,274 times

1

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

When the program runs, the login screen is the first to appear, and from it I need 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 proper passwords to which I have already given the proper "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 "keep" that connection when I open the other program window which is where I will make the changes? I need to keep the connection so that each user only does what is allowed in their "role".

It’s my first time trying to connect a program to a database and I’m having a hard time..

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 payment and etc..

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

I’ve done a lot of research, but I still haven’t found an answer I can make work.

  • The InitialCatalog is the name of the database in SQL Server and DataSource should be "ML074539\\MSSQLSERVER", if your instance of SQL Server is the default (MSSQLSERVER). If it is Express instance, the default name of the instance is SQLEXPRESS.

  • If I understand correctly, the name of the database in SQL is ML074539. That’s where the tables are and etc. I use the name of the database in Datasource as well? Before the SQL instance? (The instance is the default yes.. MSSQLSERVER, from what I saw on Windows services)

  • And what is the name of the computer? You have to replace the . in ".\\MSSQLSERVER" by the name of the computer-server SQL Server, otherwise the app will try to connect to the own computer where the app runs.

  • No, use ML074539 in the InitialCatalog, then

  • The app is running on the same computer as the database (and I need this to happen because I will deliver this project in the course, and there will already be another computer..). Even so, need to change? Computer name is DESKTOP-066MJ19

  • Is Windows Firewall turned on? If you are, try turning it off.

  • I hung up and continued with the same mistake.. Just a question, if I can connect to the database, this connection will be maintained when the login window is closed and Multileasewindow is opened?

  • And then, managed to progress a little after the chat?

  • 1

    I could think of another way to do it that I thought was easier, hahaha! I made the table "Users" in the database and used it to check the credentials and assign an access level to the user who logged in.. With this I created a User in the app with these properties and passed it to the Multilease screen, then I can check what his "access" to each task that will be executed to see if he has the level of access necessary to perform this task. Thanks for the help!!!

  • Good guy, go deep, whatever thing just give a shout. Abs!

Show 6 more comments
No answers

Browser other questions tagged

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