Think you should be using server not dns
GlobalVariables.ConnectionString = "Server=ms_xyz;Database=xyz;UID="
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
//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.
Think you should be using server not dns
GlobalVariables.ConnectionString = "Server=ms_xyz;Database=xyz;UID="
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.
Connection string syntax varies by provider. There are so many variants that there is a dedicated web site to help you
@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.
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.