C# 以编程方式创建 SQL Server CE 数据库文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1487845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create SQL Server CE database file programmatically
提问by Andrew Arnott
How can I create a new SQL Server Compact database file (.sdf) programmatically, without having an existing template file to copy from?
如何以编程方式创建新的 SQL Server Compact 数据库文件 (.sdf),而无需复制现有模板文件?
采纳答案by RedFilter
There is some good info here: Create a SQL Server Compact Edition Database with C#
这里有一些很好的信息:Create a SQL Server Compact Edition Database with C#
string connectionString = "DataSource=\"test.sdf\"; Password=\"mypassword\"";
SqlCeEngine en = new SqlCeEngine(connectionString);
en.CreateDatabase();
回答by aminescm
First, go click on the Browse tab. Now navigate to C:\Program Files\Microsoft SQL Server Compact Edition\v3.1
. Now pick the System.Dadta.SqlServerCe.dll
, click on it, then click on OK to pull in the reference.
首先,单击“浏览”选项卡。现在导航到C:\Program Files\Microsoft SQL Server Compact Edition\v3.1
. 现在选择System.Dadta.SqlServerCe.dll
,单击它,然后单击确定以拉入引用。
Now let's write some code. First, go to the head of the class, whoops I mean header of your class and let's create a using reference.
现在让我们写一些代码。首先,转到班级的头部,哎呀,我的意思是您的班级的头部,让我们创建一个 using 引用。
using System.Data.SqlServerCe;
using System.IO;
string connectionString;
string fileName = "aminescm.sdf";
string password = “aminescm”;
if (File.Exists(fileName))
{
File.Delete(fileName);
}
connectionString = string.Format(
"DataSource=\"{0}\"; Password='{1}'", fileName, password);
SqlCeEngine en = new SqlCeEngine(connectionString);
en.CreateDatabase();
You can find more and more about this, it's really an amazing web site:
你可以找到更多关于这个的信息,这真是一个了不起的网站: