将连接字符串从SQEXPRESS服务器更改为LocalDb文件

时间:2016-07-15 13:26:15

标签: c# sql-server

我首先使用数据库开发应用程序,应用程序已连接到我的。\ SQLEXPRESS,现在我想将其更改为本地mdf文件。

这是原始字符串:

<add name="caloriedatabaseEntities1" connectionString="metadata=res://*/calorie.csdl|res://*/calorie.ssdl|res://*/calorie.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=VULE-PC\SQLEXPRESS;initial catalog=caloriedatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

这是第一次尝试(手动将文件同步到项目文件夹)

<add name="caloriedatabaseEntities1" connectionString="metadata=res://*/calorie.csdl|res://*/calorie.ssdl|res://*/calorie.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\caloriedatabase.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.EntityClient" /></connectionStrings>

并尝试第二次

<add name="caloriedatabaseEntities1" connectionString="metadata=res://*/calorie.csdl|res://*/calorie.ssdl|res://*/calorie.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;initial catalog=caloriedatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

错误:

  System.ArgumentException was unhandled
  HResult=-2147024809
  Message=Format of the initialization string does not conform to specification starting at index 102.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, String& keyname, String& keyvalue)
       at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.ParseInternal(IDictionary`2 parsetable, String connectionString, IList`1 validKeywords)
       at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions..ctor(String connectionString, IList`1 validKeywords)
       at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
       at System.Data.Entity.Core.EntityClient.EntityConnection..ctor(String connectionString)
       at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)
       at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config)
       at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
       at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
       at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       at System.Data.Entity.Internal.InternalContext.Initialize()
       at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
       at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
       at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
       at System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)
       at Calorie1.Form1.updateListViewItem(caloriedatabaseEntities1 context) in c:\Users\Vule\Documents\Visual Studio 2013\Projects\Calorie1\Calorie1\Form1.cs:line 117
       at Calorie1.Form1.startCalorie() in c:\Users\Vule\Documents\Visual Studio 2013\Projects\Calorie1\Calorie1\Form1.cs:line 79
       at Calorie1.Form1..ctor() in c:\Users\Vule\Documents\Visual Studio 2013\Projects\Calorie1\Calorie1\Form1.cs:line 18
       at Calorie1.Program.Main() in c:\Users\Vule\Documents\Visual Studio 2013\Projects\Calorie1\Calorie1\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

1 个答案:

答案 0 :(得分:1)

尝试其中之一:

<add name="caloriedatabaseEntities1" connectionString="metadata=res://*/calorie.csdl|res://*/calorie.ssdl|res://*/calorie.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\caloriedatabase.mdf;Initial Catalog=caloriedatabase;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient&quot;" providerName="System.Data.EntityClient" /></connectionStrings>


<add name="caloriedatabaseEntities1" connectionString="metadata=res://*/calorie.csdl|res://*/calorie.ssdl|res://*/calorie.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=c:\mypath\caloriedatabase.mdf;Initial Catalog=caloriedatabase;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

别忘了调整第二个路径,AttachDbFilename = ...