简体   繁体   English

如何使用C#代码在SQL Server CE数据库文件中创建表

[英]How to Create Tables in a SQL Server CE database file using C# code

I am creating a SQL Server Compact Edition database using C# code in a console application. 我正在控制台应用程序中使用C#代码创建SQL Server Compact Edition数据库。 Here is my code for creating database: 这是我用于创建数据库的代码:

string con;

string fileName = "MiniProfilerData.sdf";
string password = "arcanecode";

if (File.Exists(fileName))
{
    File.Delete(fileName);
}

con = string.Format("DataSource=\"{0}\"; Password='{1}'", fileName, password);
SqlCeEngine en = new SqlCeEngine(con);
en.CreateDatabase();

This code generates the MiniProfilerData.sdf file 此代码生成MiniProfilerData.sdf文件

Now I want to create tables in that .sdf file. 现在,我想在该.sdf文件中创建表。 I have a table creation script which will generates tables, here is the script Click Here 我有一个表创建脚本,它将生成表,这是脚本单击此处

How can I generate tables using that TableCreationScript in my MiniProfilerData.sdf file? 怎样才能使用该表TableCreationScriptMiniProfilerData.sdf文件?

Any idea? 任何想法?

You need to split the script into individual CREATE statements, the execute each (pseudo-code): 您需要将脚本拆分为单独的CREATE语句,然后执行每个(伪代码):

var scripts = TableCreationScript
    .Trim()
    .Replace("create", "#create")
    .Split(new []{'#'}, StringSplitOptions.RemoveEmptyEntries);

for each (var script in scripts)
{
    SqlCeCommand cmd = new SqlCeCommand(script);
    cmd.Connection = connection;
    cmd.ExecuteNonQuery();
}

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

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