本文介绍了使用任务测试数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 美好的一天!我有一个表单,我输入连接详细信息以连接到MSSql数据库。这就是我所做的...... private void sbTest_Click( object sender,EventArgs e) { if (!dxvpConnectionWizard.Validate()) return ; TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext(); CancellationTokenSource tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; this .Cursor = Cursors.AppStarting; 任务connectionTestTask = Task.Factory.StartNew(()= > { bsiConnWizardStatus.Glyph = ClaimCatch_RIS .Properties.Resources.loading; bsiConnWizardStatus.Caption = 试图连接数据库...; 使用(SqlConnection connection = new SqlConnection(GetDataBaseConnectionString( ))) { try { connection.Open(); connection.Close( ); } catch // (例外情况) { tokenSource.Cancel( true ); } } },令牌).ContinueWith(结果= > { this .Cursor = Cursors.Default; if (token.IsCancellationRequested) { bsiConnWizardStatus.Glyph = ClaimCatch_RIS.Properties.Resources.warning_16x16; bsiConnWizardStatus.Caption = ServerModeStrings.failedDatabaseConnection; } else { bsiConnWizardStatus.Glyph = ClaimCatch_RIS.Properties.Resources.apply_16x16; bsiConnWizardStatus.Caption = Ready ...; } },CancellationToken.None,TaskContinua tionOptions.None,TaskScheduler.FromCurrentSynchronizationContext()); } 但这不是我想要的。我实际上想要的是能够从数据库中捕获错误并获得任务状态。 我该如何去做... 提前付款... 解决方案 你好Azinyama, 你可以用 System.Data.SqlClient.SqlException 用于从数据库中捕获异常的类。 访问此链接 http://msdn.microsoft.com/en-us/library/system.data.sqlclient .sqlexception.aspx [ ^ ] - 谢谢 Good day all!!! I have a form where I'm entering the connection details to connect to an MSSql database. This is what I have done...private void sbTest_Click(object sender, EventArgs e) { if (!dxvpConnectionWizard.Validate()) return; TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext(); CancellationTokenSource tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; this.Cursor = Cursors.AppStarting; Task connectionTestTask = Task.Factory.StartNew(() => { bsiConnWizardStatus.Glyph = ClaimCatch_RIS.Properties.Resources.loading; bsiConnWizardStatus.Caption = "Attempting to connect to database..."; using (SqlConnection connection = new SqlConnection(GetDataBaseConnectionString())) { try { connection.Open(); connection.Close(); } catch// (Exception ex) { tokenSource.Cancel(true); } } }, token).ContinueWith(result => { this.Cursor = Cursors.Default; if (token.IsCancellationRequested) { bsiConnWizardStatus.Glyph = ClaimCatch_RIS.Properties.Resources.warning_16x16; bsiConnWizardStatus.Caption = ServerModeStrings.failedDatabaseConnection; } else { bsiConnWizardStatus.Glyph = ClaimCatch_RIS.Properties.Resources.apply_16x16; bsiConnWizardStatus.Caption = "Ready..."; } }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); }But it's not exactly what I want. I actually want is to be able catch the error from the database and also get the task status.How would I go about doing that...Thanx in advance... 解决方案 Hi Azinyama, You can use System.Data.SqlClient.SqlException class to catch exception from Database. Visit this link http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception.aspx[^]--Thanks 这篇关于使用任务测试数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 阿里云证书,YYDS! 05-23 01:01