创建数据库SQLCE编程 [英] Create SQLCE database programatically

查看:93
本文介绍了创建数据库SQLCE编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢所有的输入/答案。以下是我的code以编程方式创建SQL CE数据库:

  / *获取路径* /
VAR目录名= System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly()位置。);
变种文件名= System.IO.Path.Combine(目录名,Foo2Database.sdf);/ *检查是否存在* /
如果(File.Exists(文件名))
    File.Delete(文件名);字符串connStr = @数据源=+文件名;/ *创建数据库* /
SqlCeEngine发动机=新SqlCeEngine(connStr);
engine.CreateDatabase();/ *创建表和列* /
使用(康涅狄格州的SqlCeConnection =新的SqlCeConnection(connStr))
{
    使用(SqlCeCommand CMD =新SqlCeCommand(@CREATE TABLE FooTable(Foo_ID INT,FooData NVARCHAR(200)),康涅狄格州))
    {
        尝试
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        赶上(异常前)
        {
            MessageBox.Show(ex.ToString());
        }
        最后
        {
            conn.Close();
        }    }
}


解决方案

我曾与SQLCE 3.1和SharpDevelop的,试试这个code,看看这是你想要什么工作:

 字符串connStr =数据源= FooDatabase.sdf;密码= SomePassword;如果(File.Exists(FooDatabase.sdf))
    File.Delete(FooDatabase.sdf);SqlCeEngine发动机=新SqlCeEngine(connStr);
engine.CreateDatabase();康涅狄格州的SqlCeConnection = NULL;
尝试
{
    康恩=新的SqlCeConnection(connStr);
    conn.Open();    SqlCeCommand CMD = conn.CreateCommand();
    cmd.CommandText =CREATE TABLE FooTable(COL1 INT,COL2 NTEXT);
    cmd.ExecuteNonQuery();
}
抓住
{}
最后
{
    conn.Close();
}

请注意,数据库只是一个文件,所以你可以检查是否通过查找存在的数据库,如果该文件存在,你也可以通过删除文件删除数据库。希望这有助于。

[EDITED - with answer]

Thanks for all the input/answer. Following is my code to create SQL CE database programmatically:

/* get the Path */
var directoryName = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var fileName = System.IO.Path.Combine(directoryName, "Foo2Database.sdf");

/* check if exists */
if (File.Exists(fileName))
    File.Delete(fileName);

string connStr = @"Data Source = " + fileName;

/* create Database */
SqlCeEngine engine = new SqlCeEngine(connStr);
engine.CreateDatabase();

/* create table and columns */
using (SqlCeConnection conn = new SqlCeConnection(connStr))
{
    using (SqlCeCommand cmd = new SqlCeCommand(@"CREATE TABLE FooTable (Foo_ID int, FooData NVARCHAR(200))", conn))
    {
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            conn.Close();
        }

    }
}

解决方案

I have worked with SQLCE 3.1 and SharpDevelop, Try this code and see if this is what you want:

string connStr = "Data Source = FooDatabase.sdf; Password = SomePassword";

if (File.Exists("FooDatabase.sdf")) 
    File.Delete("FooDatabase.sdf");  

SqlCeEngine engine = new SqlCeEngine(connStr); 
engine.CreateDatabase();

SqlCeConnection conn = null;


try 
{
    conn = new SqlCeConnection(connStr);
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "CREATE TABLE FooTable(col1 int, col2 ntext)";
    cmd.ExecuteNonQuery();
}
catch 
{

}
finally 
{
    conn.Close();
}

Note that the database is just a file, so you can check if the database exists by looking if the file exists, also you can delete the database by deleting the file. Hope this helps.

这篇关于创建数据库SQLCE编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆