简体   繁体   English

如何在C#中连接到.sdf本地数据库文件

[英]How do I connect to a .sdf local database file in C#

I am using local SQL Server CE database file ( .sdf ) and Entity Framework for my task. 我正在使用本地SQL Server CE数据库文件( .sdf )和实体框架来完成我的任务。

I have created my models and now entity framework should map them to local database. 我创建了我的模型,现在实体框架应该将它们映射到本地数据库。

I have this code in my Main method which inserts some data to database(to check): 我在我的Main方法中有这个代码,它将一些数据插入数据库(检查):

var db = new Context()
var blog = new Blog { Name = 'FirstBlog'};
            db.Blogs.Add(blog);
            db.SaveChanges(); 

and this is my connection string: 这是我的连接字符串:

<connectionStrings>
   <add name="Connection" 
        connectionString="Data source=|DataDirectory|\LocalDB.sdf&quot;" 
        providerName="System.Data.EntityClient" />
</connectionStrings>

when I run this, I get this error: 当我运行它时,我收到此错误:

Format of the initialization string does not conform to specification starting at index 0. 初始化字符串的格式不符合从索引0开始的规范。

I have tried several connection string but none worked. 我已经尝试了几个连接字符串但没有工作。

How can I fix this? 我怎样才能解决这个问题?

UPDATE: 更新:

As suggested I changed my connection string to be: 建议我将连接字符串更改为:

<connectionStrings>
   <add name="Connection" 
        connectionString="provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;Data source=|DataDirectory|\LocalDB.sdf&quot;" 
        providerName="System.Data.EntityClient" />
</connectionStrings>

but this gives me the following error: 但这给了我以下错误:

Some required information is missing from the connection string. 连接字符串中缺少一些必需的信息。 The 'metadata' keyword is always required. 始终需要'metadata'关键字。

First Install this EntityFramework.SqlServerCompact nuget package and then set your connection string to somting like this: 首先安装此EntityFramework.SqlServerCompact nuget包,然后将您的连接字符串设置为somting,如下所示:

<connectionStrings>
    <add name="connection" connectionString="Data Source=|DataDirectory|\TestDb.sdf"
      providerName="System.Data.SqlServerCe.4.0" />
  </connectionStrings>

**Note:**By default entity framework will create TestDb.sdf if it doesn't exsist in you solutions \\bin\\Debug directory and after deployment it will be in C:\\Users\\YourUserName\\AppData\\Roaming this directory **注意:**默认情况下,实体框架将创建TestDb.sdf,如果它在您的解决方案\\bin\\Debug目录中不存在,则在部署之后它将位于C:\\Users\\YourUserName\\AppData\\Roaming此目录中

You are missing Server=(localdb)\\v11.0 in your connection string to indicate that it is a LocalDB connection. 您在连接字符串中缺少Server=(localdb)\\v11.0以指示它是LocalDB连接。 In addition to that, you also have &quot in your connection string that should be removed. 除此之外,你还可以&quot在连接字符串中应删除。

Full details as to how your connection string should look like can be found on this MSDN page. 有关连接字符串应如何显示的详细信息,请参阅此MSDN页面。

Examples for connections strings can be found here (Scroll down to 可以在此处找到连接字符串的示例(向下滚动到

我认为这是一个Sql Server Compact文件,因此您的提供程序需要是System.Data.SqlServerCe提供程序之一。

<connectionStrings>
    <add name="MyCS" connectionString="Data Source=|DataDirectory|\Database1.sdf"
      providerName="System.Data.SqlServerCe.4.0" />
  </connectionStrings>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM