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.
Thank you. I don't work in IT department, not sure where to get InstanceName, not even sure what it is. All the information I can get is from below screenshot (from control panel). If it is impossible to get InstanceName, then I should quit using SqlConnection, just use OdbcConnection.
First line Name is dsn name, third line Server is ServerName? Name and Server are different.
Given that I am able to connect with ODBC connection string, is it possible to write code to pull InstanceName information? Then use InstanceName in Sql connection string.
@VAer , is any update? Please check if my answer works for you.
@Jack J Jun Previous comment is my update. For some reasons (explained in other questions before), I have to use connection string, briefly explain again (for my work PC, Visual Studio is installed in local machine desktop, and database is only accessible in virtual desktop, there is absolutely no way to access database in Visual Studio, I have to build application in local machine desktop, then move exe application to virtual desktop for use, it will be for team members).
I don't work in IT department, no ideas how to get myInstanceName, all the information I can get is from screenshot on above comment. Given that I am able to connect via Odbc connection string, I will give up trying on SqlConnection. This thread is more about learning purpose (learning an alternative way for database connection), and I would like to know how to work with SqlConnection, but it seems there is no solution here.
Sign in to comment