admin

web.config中的连接字符串无法连接

sql

我决定将我的项目从工作转移到家庭,并且在连接数据库时遇到了一些麻烦。此作品适用于工作:

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>

我想我不需要所有这些字符串,但它至少可以工作。这行用于designer.cs

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

我已经在asp.net/mvc上完成了电影示例项目,并使用了这个连接字符串,并且这个可以在我家里的计算机上使用。

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>

有任何想法吗?


阅读 165

收藏
2021-07-01

共1个答案

admin

有很多不同的连接字符串可用:

常规(Windows身份验证):

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

(SqlServer身份验证):

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

如果您想了解有关连接字符串的更多信息,请转到:

http://www.connectionstrings.com
2021-07-01