meyt
meyt

Reputation: 37

Problem with sql connection string syntax

I'm trying to connect to SQL server using SQLCLIENT.

And I think I have a problem with CONNECTION STRING.

The name of the SERVER as it appears: "LAPTOP-LUC355KE \ MSSQLSERVER01"

When I write the name, C# has a problem with the "\", I tried to make "/" but I can't connect to the server and I get an error message that the name may not be correct.

error: The network name cannot be found.

C# code

Upvotes: 1

Views: 1060

Answers (3)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28413

Method 1 : Use @ at beginning of string

@"LAPTOP-LUC355KE \ MSSQLSERVER01"

Method 2 : Use the Web.config and its good practice to read the connection string from there

You need to add a reference to System.Configuration and then use:

System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;

Upvotes: 2

Nits Patel
Nits Patel

Reputation: 390

Connection string:

Bellow System.webserver

  </system.webServer>
  <connectionStrings>
    <add name="YourConection_String_Name"
      connectionString="Data Source= Your_Sql_Port_No; Initial Catalog=Your_Data_Base_Name;User ID=your_Sql_ID;Password=Your_Sql_Password;Max Pool Size=10240;Pooling=true;"
      providerName="System.Data.SqlClient"/>
  </connectionStrings>

May It helps..

Upvotes: 0

Tanveer Badar
Tanveer Badar

Reputation: 5512

You need to escape the \ character in machine name by doubling it.

This should get you started:

LAPTOP-LUC355KE\\MSSQLSERVER01

P.S.: Unless it is throw-away code you are writing, it is extremely bad practice to hard-code connection strings in the code.

Upvotes: 1

Related Questions