Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
829 views
in Technique[技术] by (71.8m points)

c# - A network-related or instance-specific error occurred while establishing a connection to SQL Server. Error Locating Server/Instance Specified)

Moving a web project from test enviroment to the web server made this error occur whenever tehre's an attempt to open a connection to the sql server.

We can insert and read data from sql server management studio.

We're suspecting that the error is created from a bad connection string or from the sql server using integrated security, but haven't been able to confirm that.

the connection string.

<connectionStrings>
<add name="DSVUShort" connectionString="data source=localhostSQLEXPRESS;database=NS_Survey; integrated security=true;multipleactiveresultsets=True;App=EntityFramework" />
</connectionStrings>

where it is used

SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DSVUShort"].ToString());
Connection.Open() // pops the error
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

causes this error is connectionString in webConfig
Set connectionString in webConfig.
name of connectionStrin in webConfig and code behind must be same.

in web.config:

<configuration>
<connectionStrings>
    <add name="DSVUShort" connectionString="Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
    <compilation debug="true" targetFramework="4.0"/>
</system.web>

in code:

SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DSVUShort"].ToString());  
Connection.Open();

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...