简体   繁体   中英

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# 代码

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;

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

This should get you started:

LAPTOP-LUC355KE\\MSSQLSERVER01

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM