【发布时间】:2016-01-04 17:27:07
【问题描述】:
我正在使用 Entity Framework 6 作为我的 C# Winforms 应用程序的一部分。我的底层数据库是 SQL Server Express 2014。
我正在尝试通过这样做来测试数据库是否存在:
MyEntity me = new MyEntity()
// Try and open the connection, if unable to, show an error message and exit
try
{
me.Database.Connection.Open();
}
catch (SqlException ex)
{
MessageBox.Show("Unable to connect to database!\n\nError message:\n" + ex.Message, "Unable to connect to Database", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
我目前在表单的“加载”事件中有这个,但我知道这不起作用,因为消息泵尚未启动。
我的问题是,如果数据库没有运行,我最早可以退出(优雅地)多长时间?
【问题讨论】:
-
将其放入
Shown()事件中也不起作用。 -
你的意思是你没有看到 MessageBox 因为你退出了你的应用程序?
-
抱歉,不,我的意思是我看到了消息框,但我的程序继续执行而不是退出。
标签: c# sql-server winforms entity-framework-6