安装 SQL CE 4.0 包:http ://www.microsoft.com/en-us/download/details.aspx?id=17876
尝试将 IDE 中的引用添加到“C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Desktop\System.Data.SqlServerCe.dll”
添加 using 子句:
using System.Data.SqlServerCe;
using System.IO;
然后尝试这样的事情:
string connectionString;
string fileName = "test.sdf";
string password = "test";
if (File.Exists(fileName))
File.Delete(fileName);
connectionString = string.Format("DataSource=\"{0}\"; Password='{1}'", fileName, password);
SqlCeConnection conn = null;
try {
SqlCeEngine engine = new SqlCeEngine(connectionString);
engine.CreateDatabase();
conn = new SqlCeConnection(connectionString);
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "CREATE TABLE myTable (col1 int, col2 ntext)";
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally {
conn.Close();
}