1

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

2
  • 2
    its c# you need to escape the \, put in 2 of them, or put an @ before the beginning " Commented Nov 22, 2019 at 7:38
  • 1
    Please read about escape codes in C# strings. Commented Nov 22, 2019 at 7:44

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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..

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.