C# 试着抓住并“抓住”;继续";-这可能吗?
我的代码中有一个部分,我在其中查询网络上的所有SQL Server数据库。我首先尝试使用SQL登录访问SQL Server实例,但如果失败,则我希望尝试使用Windows凭据连接。之后,如果我仍然无法连接,那么我希望代码失败,然后通知用户 因此,我想我要问的是,如何从Try-Catch块内部循环回Try-Catch块正上方的线:C# 试着抓住并“抓住”;继续";-这可能吗?,c#,.net,try-catch,C#,.net,Try Catch,我的代码中有一个部分,我在其中查询网络上的所有SQL Server数据库。我首先尝试使用SQL登录访问SQL Server实例,但如果失败,则我希望尝试使用Windows凭据连接。之后,如果我仍然无法连接,那么我希望代码失败,然后通知用户 因此,我想我要问的是,如何从Try-Catch块内部循环回Try-Catch块正上方的线: String conxString = @"Data Source=SQLInstance1;User ID=FOO;Password=BAR;"; bool seco
String conxString = @"Data Source=SQLInstance1;User ID=FOO;Password=BAR;";
bool secondTime = false;
using (SqlConnection sqlConx = new SqlConnection(conxString))
{
Try{
sqlConx.Open();
DataTable tblDatabases = sqlConx.GetSchema("Databases");
sqlConx.Close();
secondTime = false;
Console.WriteLine("SQL Server found!");
}
Catch(System.Data.SqlClient.SqlException e){
if (!secondTime){
secondTime = true;
conxString = @"Data Source=SQLInstance1; Integrated Security=True;";
//Loop back to the using statement to try again with Windows Creds
{
else{
Console.WriteLine("SQL Server not found or credentials refused");
}
//Report Failure to connect to user
}
finally{
//Reset Variable
secondTime = false;
}
}
我可能会走这条路:
String conxString = @"Data Source=Instance1;User ID=FOO;Password=BAR;";
//in your main function
if(!TryConnect(conxString))
{
Console.WriteLine("SQL Creditials failed. Trying with windows credentials...");
conxString = "new conn string";
TryConnect(conxString);
}
..............
//new function outside of your main function
private bool TryConnect(string connString)
{
using (SqlConnection sqlConx = new SqlConnection(conxString))
{
Try{
sqlConx.Open();
DataTable tblDatabases = sqlConx.GetSchema("Databases");
sqlConx.Close();
}
Catch(System.Data.SqlClient.SqlException e){
return false;
}
return true;
}
}
成功后,您可以将
for
循环与break
组合使用:
for (int attempt = 1; attempt <= 2; attempt++)
{
try
{
/* perform attempt */
var success = TryToConnect();
if (success)
break;
}
catch (Exception e)
{
/* report error */
}
}
for(int-trunt=1;trunt这(尽管是从2005年开始)显示了您的问题的可能解决方案:
使用Goto
写一个包装
@Abe-这就是为什么我添加了secondTime标志,这样它只会循环一次。我所认识的大多数开发人员都会反对使用异常进行代码分叉。异常应该显示或记录错误消息,然后留下错误代码。将生产性代码放在catch块中实际上是混淆了关注的领域rn.我从来没有想到过,好主意!我想说这是这里最好的答案。它简单、有效,解决了当前的问题。看了这个解决方案后,我发现这是一个比使用GoTo更好的解决方案。谢谢大家的建议和评论。不是“GoTo”只是带回有趣的回忆?有趣的是,你这么说,在我的VB6项目中,我会再做一次Resume TryBlock:或类似的事情。很抱歉,每当我在现代代码中看到goto时,我仍然痛苦地尖叫。goto的使用是如何被标记为最佳答案的???goto是对我所有编程敏感性的侮辱。说真的,Stack-O需要设置一个24小时的最短时间,作者才能将答案标记为正确。@Miketeeve-这是一个快速而肮脏的应用程序,我需要编写它,以便在250多个不同的SQL Server实例中搜索特定的数据库。虽然它可能不漂亮,但它完成了设计的任务,我能够找到我要查找的服务器使用.1作为答案。始终从零开始索引(int-trunt=0;trunt<2;trunt++)
“@Xaqron:我选择使用基于一的变量的原因是,在代码的后面,我们可以说if(trunt==2)
在英语中的意思是这是第二次尝试吗?但我同意基于零的选择也是一个明智的选择。但是,我认为,目标是在尝试之间更改连接字符串,是吗?@EoRaptor013:对不起,我的伪代码没有明确说明变量尝试
可以用来更改连接尝试。我的previous注释描述了当代码粘贴到伪代码上时,如何使用trunt==2
代替secondTime
。
TryLabel:
try
{
downloadMgr.DownLoadFile("file:///server/file", "c:\\file");
Console.WriteLine("File successfully downloaded");
}
catch (NetworkException ex)
{
if (ex.OkToRetry)
goto TryLabel;
}
public static bool Wrapper(DownloadManager downloadMgr)
{
try
{
downloadMgr.DownLoadFile("file:///server/file", "c:\\file");
return true;
}
catch (NetworkException ex)
{
Console.WriteLine("Failed to download file: {0}", ex.Message);
return (!ex.OkToRetry);
}
}
static void Main(string[] args)
{
while (!Wrapper(downloadMgr)) ;
}