禁用 SQLite 自动创建数据库 - C#
Disable SQLite automatic Database creation - C#
我成功地使用了 try - catch 方法来测试 C# 与常见数据库的连接。
但是,对于 SQLite,当未检测到数据库时,它会创建一个新的数据库,这在我的情况下是不需要的。 “当您连接到一个不存在的 SQLite 数据库文件时,SQLite 会自动为您创建新的数据库”
SQLiteDataAdapter ad;
DataTable dt = new DataTable();
try
{
sqlite = new SQLiteConnection("Data Source=C:/Users/SKUL/Desktop/db/PatHist.db");
SQLiteCommand cmd;
sqlite.Open(); //Initiate connection to the db
cmd = sqlite.CreateCommand();
//-----------------
picConnectionStatus.Image = Properties.Resources.icons8_database_view_64;
lblConnectionEstablished.Text = "Connection Established";
sqlite.Close();
}
catch (Exception ex)
{
picConnectionStatus.Image = Properties.Resources.icons8_delete_database_64;
lblConnectionEstablished.Text = "Connection Error";
lblConnectionEstablished.ForeColor = System.Drawing.Color.Red;
}
需要的是禁用自动数据库创建和触发“捕获”例程的某种方式
在@Rafael 的建议下,我检查了文件是否存在。这样做问题就解决了。
if (System.IO.File.Exists(@"C:\Users\SKUL\Desktop\db\nPatHist.db"))
{
sqlite = new SQLiteConnection("Data Source=C:\Users\SKUL\Desktop\db\PatHist.db");
SQLiteCommand cmd;
sqlite.Open(); //Initiate connection to the db
cmd = sqlite.CreateCommand();
//-----------------
picConnectionStatus.Image = Properties.Resources.icons8_database_view_64;
lblConnectionEstablished.Text = "Connection Established";
sqlite.Close();
}
我成功地使用了 try - catch 方法来测试 C# 与常见数据库的连接。 但是,对于 SQLite,当未检测到数据库时,它会创建一个新的数据库,这在我的情况下是不需要的。 “当您连接到一个不存在的 SQLite 数据库文件时,SQLite 会自动为您创建新的数据库”
SQLiteDataAdapter ad;
DataTable dt = new DataTable();
try
{
sqlite = new SQLiteConnection("Data Source=C:/Users/SKUL/Desktop/db/PatHist.db");
SQLiteCommand cmd;
sqlite.Open(); //Initiate connection to the db
cmd = sqlite.CreateCommand();
//-----------------
picConnectionStatus.Image = Properties.Resources.icons8_database_view_64;
lblConnectionEstablished.Text = "Connection Established";
sqlite.Close();
}
catch (Exception ex)
{
picConnectionStatus.Image = Properties.Resources.icons8_delete_database_64;
lblConnectionEstablished.Text = "Connection Error";
lblConnectionEstablished.ForeColor = System.Drawing.Color.Red;
}
需要的是禁用自动数据库创建和触发“捕获”例程的某种方式
在@Rafael 的建议下,我检查了文件是否存在。这样做问题就解决了。
if (System.IO.File.Exists(@"C:\Users\SKUL\Desktop\db\nPatHist.db"))
{
sqlite = new SQLiteConnection("Data Source=C:\Users\SKUL\Desktop\db\PatHist.db");
SQLiteCommand cmd;
sqlite.Open(); //Initiate connection to the db
cmd = sqlite.CreateCommand();
//-----------------
picConnectionStatus.Image = Properties.Resources.icons8_database_view_64;
lblConnectionEstablished.Text = "Connection Established";
sqlite.Close();
}