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

VAer-4038 766 Reputation points
2022-01-30T23:53:47.507+00:00

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

Thanks.

169677-dsn-not-supported.jpg

       //OdbcConnection Cn;  
        SqlConnection Cn;  
  
        public FormLogin()  
        {  
            InitializeComponent();  
        }  
  
                      
        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");  
                return;  
            }  
  
            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;  
  
            try  
            {  
                Cn.Open();  
                SuccessfulConnection = true;                  
            }  
  
            catch  
            {  
                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.

169894-sqlconnection.jpg

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,149 questions
C#
C#
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.
11,114 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Ken Tucker 5,856 Reputation points
    2022-01-31T00:17:52.877+00:00

    Think you should be using server not dns

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

  2. Olaf Helper 45,366 Reputation points
    2022-01-31T06:31:28.707+00:00

    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) 68,236 Reputation points
    2022-02-01T01:56:23.897+00:00

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

    https://www.connectionstrings.com/sql-server/

    0 comments No comments

  4. Jack J Jun 24,611 Reputation points Microsoft Vendor
    2022-02-02T02:49:40.84+00:00

    @VAer-4038 , 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.

    170374-image.png

    170384-image.png

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

    170367-image.png

    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.

    170318-image.png


    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.