Connecting to a SQL Server 2012 database with C # (Visual Studio 2012)

Evening all

I am trying to connect to a SQL Server 2012 database with C #. My connection settings when using SQL Server Management Studio are as follows: -

Server Type: Database Engine Server Name: Paul-PC\SQLEXPRESS Authentication: Windows Authentication Username: Greyed out Password: Greyed out 

The name of the database I'm trying to connect to is "testDB".

Here is my code:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace DatabaseConnection { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnConnect_Click(object sender, EventArgs e) { SqlConnection myConnection = new SqlConnection("server=localhost;" + "Trusted_Connection=yes;" + "database=testDB; " + "connection timeout=30"); try { myConnection.Open(); MessageBox.Show("Well done!"); } catch(SqlException ex) { MessageBox.Show("You failed!" + ex.Message); } } } } 

Unfortunately, my code cannot connect with the following error: -

"You failed! A network-related or specific instance error occurred while establishing a connection to SQL Server. The server could not be found or is unavailable. Verify the instance name is correct and configure SQL Server to allow remote connections."

Any suggestions? SQL Server is running locally.

+7
source share
6 answers

In the connection string, replace server=localhost with " server = Paul-PC\\SQLEXPRESS; "

+6
source

I tested all the answers here, but no one worked for me. So I studied the problem a bit, and finally I found the right connection string. To get this line, follow these steps:
1. in the name of the project:
but. right click on the project name,
b. Click Add
from. select the SQL Server database (obviously, you can rename it as you wish).
Now a new new database will be added to your project.
2. The database is visible in the "Server Explorer" window.
3. Left-click the database name in the "Server Explorer" window; now check the Solution Explorer window and you will find the "Connection String" along with the provider, state, type, version.
4. Copy the attached connection string and place it in the Page_Load method:

 string source = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\x\x\documents\visual studio 2013\Projects\WebApplication3\WebApplication3\App_Data\Product.mdf;Integrated Security=True"; SqlConnection conn = new SqlConnection(source); conn.Open(); //your code here; conn.Close(); 

I renamed my database as Product. In addition, in "AttachDbFilename" you must replace "c: \ x \ x \ documents \" with your path to the physical address of the .mdf file.

This worked for me, but I have to mention that this method works for VS2012 and VS2013. I do not know about other versions.

+3
source

Replacing server=localhost with server=.\SQLEXPRESS can do the job.

0
source

Try:

 SqlConnection myConnection = new SqlConnection("Database=testDB;Server=Paul-PC\\SQLEXPRESS;Integrated Security=True;connect timeout = 30"); 
0
source

Note to

 connetionString =@ "server=XXX;Trusted_Connection=yes;database=yourDB;"; 

Note: XXX =. OR. \ SQLEXPRESS OR. \ MSSQLSERVER OR (local) \ SQLEXPRESS OR (localdb) \ v11.0 & ...

you can replace the server with a data source

you can also replace the ' database ' with the Start Directory

Example:

  connetionString =@ "server=.\SQLEXPRESS;Trusted_Connection=yes;Initial Catalog=books;"; 
0
source

use this style

 @"server=.\sqlexpress;" 
-one
source

All Articles