Connection String in web.config fails to connect

╄→尐↘猪︶ㄣ 提交于 2019-12-11 01:59:57

问题


I decided to transfer my project from work to home and I am having some trouble with the connection to the database. This one works on work:

web.config:

  <connectionStrings>
    <add name="ApplicationServices" connectionString="Data Source=XXXXXX\SQLSERVER2008;Persist Security Info=true;Initial Catalog=esResearch;User ID=XXXXXX; Password=XXXXXX"
        providerName="System.Data.SqlClient" />
    <add name="esResearchConnectionString" connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;Persist Security Info=True;User ID=XXXXXX;Password=XXXXXX"
        providerName="System.Data.SqlClient" />
  </connectionStrings>

app.config:

<connectionStrings>
    <add name="esResearchModels.Properties.Settings.esResearchConnectionString"
        connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;Persist Security Info=True;User ID=XXXXXX"
        providerName="System.Data.SqlClient" />
    <add name="esResearchModels.Properties.Settings.esResearchConnectionString1"
        connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;Persist Security Info=True;User ID=XXXXXX;Password=XXXXXX"
        providerName="System.Data.SqlClient" />
    <add name="esResearchModels.Properties.Settings.esResearchConnectionString2"
        connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;User ID=XXXXXX;Password=XXXXXX"
        providerName="System.Data.SqlClient" />
</connectionStrings>

I guess I do not need all those strings but it works atleast. And this line is used in designer.cs

base(global::esResearchModels.Properties.Settings.Default.esResearchConnectionString2, mappingSource)

I have done the movie sample project on asp.net/mvc and used this connectionstring and this one works on my computer at home.

Web.config:

  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />

    <add name="MovieDBContext"
        connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;database=Movies;User ID=sa;password=" 
         providerName="System.Data.SqlClient"/>
  </connectionStrings>

Any ideas?


回答1:


There are so many different connection strings are available:

General(Windows Authentication):

SqlConnection sql=new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=database;Integrated Security="True");

(SqlServer Authentication):

SqlConnection sql=new SqlConnection("Data Source=.\\SQLEXPRESS;Uid=sa;password=sqlserver;database=databasename");

If you want to know more about connection string, go to:

http://www.connectionstrings.com


来源:https://stackoverflow.com/questions/7228020/connection-string-in-web-config-fails-to-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!