尝试捕获和"继续] - 这可能吗? [英] Try-Catch and "Continue" - is this possible?
问题描述
我在code一节,我查询所有SQL Server数据库在我的网络。我第一次尝试使用SQL登录来访问SQL Server的实例,但如果失败的话我想尝试使用我的Windows凭据连接。之后,如果我仍然无法连接,然后我想要的code失败,然后通知用户。
所以,我想我是问我怎么可以循环从一个try-catch块的行只是try-catch块以上的内页:
字符串conxString = @数据源= SQLInstance1;用户ID = FOO;密码= BAR;;
布尔secondTime = FALSE;
使用(SqlConnection的sqlConx =新的SqlConnection(conxString))
{
尝试{
sqlConx.Open();
数据表tblDatabases = sqlConx.GetSchema(数据库);
sqlConx.Close();
secondTime = FALSE;
Console.WriteLine(SQL Server的发现!);
}
捕捉(System.Data.SqlClient.SqlException E){
如果(!secondTime){
secondTime = TRUE;
conxString = @数据源= SQLInstance1;集成安全性= TRUE;;
//循环回用语句与Windows Creds再试一次
{
其他{
Console.WriteLine(SQL服务器未找到或拒绝证书);
}
//报告无法连接到用户
}
最后{
//重置变
secondTime = FALSE;
}
}
我可能会走这条路:
字符串conxString = @数据源=实例1;用户ID = FOO;密码= BAR;;
//在主函数
如果(!TryConnect(conxString))
{
Console.WriteLine(SQL Creditials失败,Windows凭据尝试......。);
conxString =康涅狄格州的新字符串;
TryConnect(conxString);
}
..............
//你的主要功能之外的新功能
私人布尔TryConnect(字符串CONNSTRING)
{
使用(SqlConnection的sqlConx =新的SqlConnection(conxString))
{
尝试{
sqlConx.Open();
数据表tblDatabases = sqlConx.GetSchema(数据库);
sqlConx.Close();
}
捕捉(System.Data.SqlClient.SqlException E){
返回false;
}
返回true;
}
}
I have a section in my code where I am querying all SQL Server Databases on my network. I am first trying to use a SQL Login to access the SQL Server Instance but if that fails then I want to try connecting using my Windows Credentials. After that if I still can't connect then I want the code to fail and then notify the user.
So I guess what I am asking is how can I loop back from inside of a Try-Catch block to the line just above the Try-Catch block:
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;
}
}
I would probably go this route:
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;
}
}
这篇关于尝试捕获和"继续] - 这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!