SqlCompact 只是在您的本地文件系统上创建一个文件 (.sdf)。您可以即时创建它,然后在完成后将其删除..或保留它。这是我从我曾经做过的一个项目中获取的一些代码片段。它应该有助于解决创建/CRUD 问题。
using System.Data.SqlServerCe;
private SqlCeConnection InitializeDatabase()
{
string connectionString = CreateDatabase();
SqlCeConnection conn = new SqlCeConnection(connectionString);
conn.Open();
CreateTable(conn);
return conn;
}
private string CreateDatabase()
{
string dbPath = String.Format("{0}scanner.sdf", _rootPath);
if (File.Exists(dbPath))
File.Delete(dbPath);
string connectionString = String.Format("DataSource=\"{0}\";Max Database Size=3000;", dbPath);
SqlCeEngine en = new SqlCeEngine(connectionString);
en.CreateDatabase();
en.Dispose();
return connectionString;
}
private void CreateTable(SqlCeConnection conn)
{
using (SqlCeCommand comm = new SqlCeCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = "CREATE TABLE gnis ([Id] [int] IDENTITY(1,1) PRIMARY KEY, [Name] [nvarchar](110) NOT NULL, [Geometry] [varbinary](429) NOT NULL)";
comm.ExecuteNonQuery();
}
}
private void CreateTableIndex(SqlCeConnection conn)
{
using (SqlCeCommand comm = new SqlCeCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = "CREATE INDEX IXgnis ON gnis ([Name]);";
comm.ExecuteNonQuery();
}
}
private void WriteFeature(SqlCeConnection conn, string name, MultiPoint multiPoint)
{
byte[] wkb = WkbWriter.WriteWkb(multiPoint);
using (SqlCeCommand comm = new SqlCeCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = "INSERT INTO gnis ([Name], [Geometry]) VALUES (@a, @b)";
comm.Parameters.AddWithValue("@a", name);
comm.Parameters.AddWithValue("@b", wkb);
comm.ExecuteNonQuery();
}
}