1

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();
8
  • 1
    what is your error message? Commented Jun 6, 2020 at 21:04
  • System.InvalidOperationException: 'Internal connection fatal error. Error state: 18' This exception was originally thrown at this call stack: [External Code] YeahBoy.Form2.button_login_Click(object, System.EventArgs) in Form2.cs [External Code] YeahBoy.Program.Main() in Program.cs Commented Jun 6, 2020 at 21:05
  • I am unable to find the cause by only seeing this message. Is your SQL server running properly? Commented Jun 6, 2020 at 21:13
  • 1
    Is that port correct? 3306 is usually taken by MySql. In any case that connectionstring seems wrong. It should be xxx.xxx.xxx.xxx\SQLEXPRESS, 1433. If you use the IP address there is no PC-NAME. ConnectionStrings.com Commented Jun 6, 2020 at 21:13
  • Is your SQL server running properly? Are u able to browse through databases and tables by using SQL Management Studio GUI using the same info? Commented Jun 6, 2020 at 21:14

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.