C#: Connection string for SqlConnection (Error: dsn not supported)

VAer 756 Reputation points

For below sample code, if I use OdbcConnection, it works fine. But if I change to SqlConnection, there is error --- dsn not supported (see screenshot).

Server: MS SQL

Not an IT professional, absolutely don't know what the error mean and how to fix it? Should I modify the connection string?

Last year, I asked a different question, someone says I should use SqlClient. Now I am trying different code for learning purpose. Here is previous thread: c-for-ms-sql-connection.html



       //OdbcConnection Cn;  
        SqlConnection Cn;  
        public FormLogin()  
        private void btnConnect_Click(object sender, EventArgs e)  
            if (string.IsNullOrEmpty(txtUsername.Text) || string.IsNullOrEmpty(txtPassword.Text))  
                MessageBox.Show("Username and/or Password cannot be empty.", "Error Message");  
            GlobalVariables.Username = txtUsername.Text;  
            GlobalVariables.Password = txtPassword.Text;  
            GlobalVariables.ConnectionString = "DSN=ms_xyz;Database=xyz;UID=" + txtUsername.Text + ";PWD=" + txtPassword.Text + ";";  
     //Cn = new OdbcConnection(GlobalVariables.ConnectionString);  
            Cn = new SqlConnection(GlobalVariables.ConnectionString);  
            bool SuccessfulConnection = false;  
                SuccessfulConnection = true;                  
                MessageBox.Show("Connection failed. Invalid Username/Password, or you don't have the permission to access the database.", "Error Message");  

Edit: All the information I can got is from this screenshot, for some reasons, I have to use connection string.

Edit2: It seems that the connection string for SqlConnection does not work, and I will quit trying. Instead I just use OdbcConnection.


SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,644 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,199 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Ken Tucker 5,846 Reputation points

    Think you should be using server not dns

      GlobalVariables.ConnectionString = "Server=ms_xyz;Database=xyz;UID="

  2. Olaf Helper 40,656 Reputation points

    dsn not supported

    Because DSN is ODBC specific, no other data provider uses it.
    See https://www.connectionstrings.com/sqlconnection/ for valid .NET connection strings.

    0 comments No comments

  3. Bruce (SqlWork.com) 54,866 Reputation points

    Connection string syntax varies by provider. There are so many variants that there is a dedicated web site to help you


    0 comments No comments

  4. Jack J Jun 24,281 Reputation points Microsoft Vendor

    @VAer , based on my test, as your metioned, the Server Name is the name of Server in the ODBC Driver.

    You could try the following steps to find your connection string for SqlConnection.

    First, Please open visual studio 2022 or vs 2019, choose Tools->Connect to DataBase->Choose Microsoft SQL Server->Click Continue.



    Second, Please Copy Server Name from the ODBC Driver and paste it into your ServerName.


    Then, Please input your UserName and Password, choose the database and click the Test Connection Button.

    As usual, If it shows a message box called Test Connection succeeded, it indicates that your connection is successful.

    Finally, you could get your connection string from the Advanced Properties.

    Note: You can set DataSource string as the connection string.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments