禁用SQLITE自动数据库创建-C#
我成功使用一种尝试 - 捕获方法来测试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;
}
。
I use successfully a try - catch method to test connection of C# with common databases.
However, in case of SQLite, when the database is not detected, it creates a new one, which is unwanted in my case. "When you connect to an SQLite database file that does not exist, SQLite automatically creates the new database for you"
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;
}
What is needed is to disable some way the automatic database creating and trigger the "catch" routine
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在@rafael的建议之后,我检查了文件的存在。解决这个问题。
after the suggestion of @Rafael i check the file presence. Doing this problem solved.