简体   繁体   English

以编程方式创建SQL Server CE 4.0数据库并在C#中添加架构?

[英]Programmatically create SQL Server CE 4.0 Database and add a schema in C#?

I'm having a go at learning to use SQL Server CE databases as a storage medium for my applications. 我正在学习使用SQL Server CE数据库作为我的应用程序的存储介质。 I've currently been storing data in text and xml files, but thought I should move onto something more appropriate for my project as the text files are getting abit messy. 我目前一直在将数据存储在文本和xml文件中,但是我认为我应该继续进行更适合我的项目的工作,因为文本文件有些混乱。

I have now learnt how to programmatically create an SQL Server CE 4.0 database file in C#, but is there any way to programmatically add an already built Schema to that newly created database file in C#? 我现在已经学习了如何以C#方式以编程方式创建SQL Server CE 4.0数据库文件,但是有什么方法可以以编程方式将已构建的架构添加到C#中的新创建的数据库文件中吗?

I guess, it depends on what approach you use to work with a database. 我猜,这取决于您使用哪种方法来处理数据库。 If Linq to Sql is available (say, you're not using .Net Compact Framework), you can create database by the following code: 如果可用Linq to Sql(例如,您没有使用.Net Compact Framework),则可以通过以下代码创建数据库:

using (MyDataContext dc = new MyDataContext("Data Source=\"test.sdf\""))
{
    dc.CreateDatabase();
}

instead of 代替

SqlCeEngine en = new SqlCeEngine("Data Source=\"test.sdf\"");
en.CreateDatabase();

Linq DataContext creates db schema automatically. Linq DataContext自动创建数据库模式。

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

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