SQL Server database connection problem
I am trying to connect to an instance of SQL Server from C #.
Here is my connection string, I am getting an exception
System.ComponentModel.Win32Exception: The system cannot find the file specified
when i tried to execute this code:
constr = "Data Source=(local);Initial Catalog=bookstall;Integrated Security=True";
My local datasource ELCOT-PC\SQLEXPRESS
, I tried with bottom line too, here I am getting compile time error as
Unrecogonized Esscape Sequence
code:
constr = "Data Source=ELCOT-PC\SQLEXPRESS;Initial Catalog=bookstall;Integrated Security=True";
Please help me with this
Thanks in advance.
Try
constr="Data Source=ELCOT-PC\\SQLEXPRESS;Initial Catalog=bookstall;Integrated Security=True";
OR use Varbatim
constr=@"Data Source=ELCOT-PC\SQLEXPRESS;Initial Catalog=bookstall;Integrated Security=True";
UPDATE
Since the above will fix your connection string, now you need to make sure you are connecting to Named Instance
sql server or Default Instance
.
The rule of thumb is when you try to connect to the default instance, you only use MachineName
connection strings in the data source property.
If you are trying to connect to Named Instance
Sql server on server / machine you will need to use ServerName\InstanceName
sql to connect to server.
You can try using "/" instead of "\" or add @ (verbatim) in front of the line, as shown below:
constr=@"Data Source=ELCOT-PC\SQLEXPRESS;Initial Catalog=bookstall;Integrated Security=True";
Hello,
Change datasource to server if you are trying to connect to a named instance
http://www.connectionstrings.com/sql-server/
For the second case, you need to exit \
. You can use @ to do escaping:
constr=@"Data Source=ELCOT-PC\SQLEXPRESS;Initial Catalog=bookstall;Integrated Security=True";
use "/" instead of "\" or add @ (verbatim)
constr="Data Source=ELCOT-PC\\SQLEXPRESS;Initial Catalog=bookstall;Integrated Security=True";
or
constr=@"Data Source=ELCOT-PC\SQLEXPRESS;Initial Catalog=bookstall;Integrated Security=True";
or
constr=@"Data Source=ELCOT-PC\SQLEXPRESS;Initial Catalog=bookstall;Integrated Security=True";