如何创建 SQL Compact Edition 数据库?我试图弄清楚它,在互联网上搜索但找不到它(但话又说回来,你如何搜索这个?!)。这是到目前为止我的代码:
checkPasswordsMatch();
SqlCEConnection myconn = new SqlCEConnection();
SqlCeCommand myCommand = new SqlCeCommand(
@"CREATE DATABASE ""pwdb.sdf"" DATABASEPASSWORD '" + textBox1.Text + "'");
myCommand.ExecuteNonQuery();
连接部分要放什么?我不明白。
更新:我决定创建一个数据库来与应用程序合并,并简单地检查是否有一个专用于记录 Master 的值(管理员密码)。
这是我的新代码:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SqlCeConnection myconn = new SqlCeConnection("Data Source = pwdb.sdf;");
SqlCeCommand checkpass = new SqlCeCommand("SELECT * from PW WHERE Name = Master;",myconn);
try
{
myconn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
if (checkpass.ExecuteNonQuery() != null)
{
Application.Run(new enterMasterPassword());
}
else
Application.Run(new mainPassSet());
问题是,当我现在执行此操作时,我得到:
An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
大家有什么想法吗?
最佳答案
尝试使用SqlCeEngine
类。使用该类,您可以创建一个新数据库:
using (SqlCeEngine engine = new SqlCeEngine())
{
engine.LocalConnectionString = yourConnectionString;
engine.CreateDatabase();
}
关于c# - 连接到尚未创建的数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19230171/