【发布时间】:2018-04-08 09:00:52
【问题描述】:
private async void Button_Click(object sender, RoutedEventArgs e)
{
var sampleMessageDialog = new SampleMessageDialog
{
Message = { Text = "Failed to connect" }
};
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionS"].ConnectionString);
try
{
con.Open();
// await DialogHost.Show(sampleMessageDialog, "RootDialog");
con.Close();
}
catch (Exception ex)
{
await DialogHost.Show(sampleMessageDialog, "RootDialog");
}
}
当凭据正确时,一切似乎都运行顺利。但是,当我为 SQL Server 连接字符串输入错误的凭据时,它似乎在我单击按钮的第二次冻结,并且对话框仅在冻结后显示。
【问题讨论】:
-
当您在提供错误凭据的情况下尝试打开连接时,会出现更长的线程块。尝试使用
await con.OpenAsync();instaed。 -
您的用户要为数据库连接输入凭据?通常不是一个好主意。我建议让连接超时时间更长而不是更短会提高安全性。
标签: c# sql-server wpf