0

I'm very new to coding, and recently got a task to connect my SSMS/SWL Database to Visual Studio 2019.

When I press the button, the DB should connect and a message should pop up saying it's been connected.

I assume I have the incorrect login details, but I'm not sure, and even if I was sure, I wouldn't know how to find the correct login details.

So here's my current button click code:

    private void button1_Click(object sender, EventArgs e)
    {
        string connectionString;
        SqlConnection cnn;
        connectionString = @"Data Source=TRISTAN\SQLEXPRESS ;Initial Catalog=TutorialDB ;User ID=TRISTAN\Tristan; Password=";
        cnn = new SqlConnection(connectionString);
        cnn.Open();
        MessageBox.Show("Connection Open");
        cnn.Close();
    }

So, the login detail's I'm using are those shown when launching SSMS and the "Connect to Server" box pops up.

Am i using the login details from the correct place "Connect to server window" Or are the details I need somewhere else.

The error message I get when clicking the 'Connect' button "System.Data.SqlClient.SqlException: 'Login failed for user 'TRISTAN\Tristan'.'"

I'd appreciate any assistance, please also keep in mind I don't know much Programming language yet, so please keep it simple.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
Tristan
  • 3
  • 3
  • I read all this twice but could not find an actual question. What is it specifically you are asking? Is it how to show a message? How to catch an exception? How to check a connection string? How to pass user data to a connection string? Something else entirely? Please also refer to [ask] – Igor Sep 08 '20 at 13:12
  • Sorry I completely forgot that part. Am i using the login details from the correct place "Connect to server window" Or are the details i need somewhere else. I also forgot the error message i get "System.Data.SqlClient.SqlException: 'Login failed for user 'TRISTAN\Tristan'.'" – Tristan Sep 08 '20 at 13:14
  • Did you add the password to that connectionstring? – Steve Sep 08 '20 at 13:15
  • Try https://learn.microsoft.com/en-us/sql/connect/oledb/help-topics/data-link-pages?view=sql-server-ver15. Create a .txt file, rename to .udl. Open it and try to get a valid connection. Then open that file using notepad and copy/paste the connection string. – Igor Sep 08 '20 at 13:15
  • There is no password when i launch SSMS so i didn't insert any password. – Tristan Sep 08 '20 at 13:20
  • Well, connecting from code requires the password – Steve Sep 08 '20 at 13:26
  • Likely you are connecting using integrated security (windows credentials). Again, use the UDL file to build your connection string and copy/paste it. – Igor Sep 08 '20 at 13:31
  • Wait, in SSMS is the user field disabled? If yes then you are using a trusted connection and you don't need to specify the username but you need to add to the connectionstring _Trusted_Connection=Yes_ – Steve Sep 08 '20 at 13:32
  • Duplicate of [How to set SQL Server connection string?](https://stackoverflow.com/q/15631602/1260204) – Igor Sep 08 '20 at 13:33

2 Answers2

1
  • Open Visual Studio
  • Go to View tab -> Server Explorer
  • Right click to "Data Connections" tab
  • Add connection
  • Select the server name
  • Select the database
  • Ok
  • Server Explorer -> Data Connections -> myServer\myDB.dbo -> right click and then Properties
  • Copy the connection string from Properties View

Write this code;

            string connectionString;
            SqlConnection cnn;
            connectionString = @"***YOUR_CONNECTION_STRING***";
            cnn = new SqlConnection(connectionString);
            cnn.Open();
            if (cnn.State == System.Data.ConnectionState.Open)
            {
                MessageBox.Show("Connection Open");
                cnn.Close();
            }
            if (cnn.State == System.Data.ConnectionState.Closed)
            {
                MessageBox.Show("Connection Closed");
            }
Tolga Cakir
  • 725
  • 1
  • 7
  • 13
  • It opens connection and checks your connection state (open or not) If your connection is open. Message box will show "Connection open" messages And connection is closed. And it checks your connection for closed state Also i explained how can you find the correct connection string (with connection details) – Tolga Cakir Sep 08 '20 at 13:42
0

You should catch error while opening a connection and display it to the user, something like this:

private void button1_Click(object sender, EventArgs e)
{
  string connectionString = @"Data Source=TRISTAN\SQLEXPRESS;Initial Catalog=TutorialDB;Trusted_Connection=True;";
  using (var cnn = new SqlConnection(connectionString))
  {
    try
    {
      cnn.Open();
      MessageBox.Show("Connection Open");
    }
    catch (SqlException ex)
    {
      MessageBox.Show("Error while connecting to database: " + ex.Message);
    }
    finally
    {
      cnn.Close();
    }
  } 
}
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
  • I have no idea what that means/does or where to insert it ? – Tristan Sep 08 '20 at 13:25
  • And I changed connection string to use windows authentication – Antonio Bakula Sep 08 '20 at 13:31
  • I'm getting 2 errors with that code Catch (SqlException e): "A local parameter named e cannot be declared in that scope (error is on the 'e') MessageBox.Show(("Error while connecting to database: " + e.Message); CS1026: ) Expected (Error on the semi colon) – Tristan Sep 08 '20 at 13:42
  • fixed, I overlooked that e is a parameter in winforms event – Antonio Bakula Sep 08 '20 at 13:49
  • It worked! Thank you so much. How do i go about understanding what you've done now. – Tristan Sep 08 '20 at 13:56
  • read about try/catch block https://learn.microsoft.com/en-us/dotnet/standard/exceptions/how-to-use-the-try-catch-block-to-catch-exceptions or watch https://www.youtube.com/watch?v=MYmRJDBnKWw – Antonio Bakula Sep 08 '20 at 14:59