sql local database connection

1.1k views Asked by At

I am trying to open my local database in c# with this code :

SqlConnection c = new SqlConnection("Server = (LocalDB)\\v11.0 ;Integrated Security=True;Database =Informati;");
c.Open();

But i receive an error when he is trying to open it :

Cannot open database "Informati" requested by the login. The login failed.
Login failed for user 'AURELIAN121\Aurelian'.

I've tried to connect using

new SqlConnection("Server = (LocalDB)\v11.0 ;User id=AURELIAN121\Aurelian;Integrated Security=True;Database =Informati;");

but the error persist.

2

There are 2 answers

0
Anupam Sharma On BEST ANSWER

Your connection string is not correct : For local database u can use dot also, like

SqlConnection c = new SqlConnection("Data Source=.;
Integrated Security=True;Initial Catalog=Informati;");

From error it seams that your local user does not have access permission on Sql server. U have to add create login first :http://www.reliasoft.com/support/rs40024.htm

0
genichm On

This is works for me

1. Data Source=(LocalDb)\v11.0;Initial Catalog=CSN;Integrated Security=SSPI;

The full version

2. <add name="DefaultConnection" 
   connectionString="Data Source=(LocalDb)\v11.0;
   Initial Catalog={catalog name};
   Integrated Security=SSPI;
   AttachDBFilename=|DataDirectory|\{database name}.mdf"        
   providerName="System.Data.SqlClient" />;

where {catalog name} is your database name and {database name} also your database name

Here you can find full list of connection strings

http://www.connectionstrings.com/sql-server-2012/