本地SQL 2012 Express Server实例的连接字符串

时间:2014-04-09 17:50:40

标签: c# asp.net sql sql-server

所以我一直想学习更多关于SQL的知识,但是在我正在研究的ASP.NET项目中连接到本地SQL服务器数据库时遇到了一些麻烦。虽然我能够使用Visual Studio和SQL Server Management Studio进行连接,但我无法正确获取连接字符串。我一直在关注connectionstrings.com,但在使用它建议的很多关键字(服务器,初始目录,数据库等)时我会遇到异常。

*编辑 - 添加了Web.config

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>

  <connectionStrings>
    <add name="StarterSite" connectionString="Data Source=|DataDirectory|\StarterSite.sdf" providerName="System.Data.SqlServerCe.4.0" />
    <add name="AdventureWorks" connectionString="Data Source=ROBERT-PC;Initial Catalog=AdventureWorks2012;Integrated Security=True" />
    <add name="DefaultConnnection" connectionString="Data Source=ROBERT-PC\MSSQLSERVER;Initial Catalog=AdventureWorks2012;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD"
  providerName="System.Data.SqlClient" />
    </connectionStrings>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="1.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="1.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234.0.0" newVersion="1.5.2.14234.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
<system.data>        
        <DbProviderFactories>
            <remove invariant="System.Data.SqlServerCe.4.0" />
            <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        </DbProviderFactories>
    </system.data></configuration>

1 个答案:

答案 0 :(得分:0)

一般情况下,visual studio默认使用&#34; defaultConnectionString&#34;你可以试试这个

<add name="DefaultConnnection"
  connectionString="Data Source=YOURMACHINE\YOUR_SQLEXPRESS_INSTANCE;Initial Catalog=DATABASE_NAME;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD"
  providerName="System.Data.SqlClient" />

显然,如果你使用EF它会改变它可能会有点不同