user12644056
user12644056

Reputation:

Trying to connect to SQL Server using C#

I am trying to connect to SQL Server and get data. This is what I did, but it's not working:

string connectionString;
SqlConnection cnn;

connectionString = @"Data Source=(IP)\PC-NAME\SQLEXPRESS,3306;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password";

cnn = new SqlConnection(connectionString);
cnn.Open();

MessageBox.Show("Connection Open  !");
cnn.Close();

Upvotes: 1

Views: 1656

Answers (2)

Hesam Akbari
Hesam Akbari

Reputation: 1141

Your Code is Correct, except your connection string i think

So, first, connect to your database via server Explorer in VisualStudio\View menu Then right-click on your database and select properties and check the connection string and copy that for test

enter image description here

enter image description here

enter image description here

enter image description here

Upvotes: 2

user366312
user366312

Reputation: 16896

I think you have a problem with your connection string.

Check your connection string using this given example:

Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial  
Catalog=myDataBase;User ID=myUsername;Password=myPassword;

Upvotes: 1

Related Questions