[編輯 - 與答案]創建SQLCE數據庫編程
以下是我的代碼編程方式創建SQL CE數據庫:
/* 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();
}
}
}
你確定這段代碼引發了Exception嗎? – 2011-06-01 06:20:58
如果你遇到這樣一個奇怪的錯誤,你會在你的痕跡中得到確切的代碼行號。請提供更多詳細信息,如完整的異常消息和代碼位置。 – ibram 2011-06-01 06:51:15
因爲您正在使用'using',所以'Close'不是必需的。 – mafu 2011-08-31 13:33:04