Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5955997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:11:37+00:00 2026-05-22T18:11:37+00:00

I have a section in my code where I am querying all SQL Server

  • 0

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;
         }

      }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:11:38+00:00Added an answer on May 22, 2026 at 6:11 pm

    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;    
          }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code section below : <asp:SqlDataSource ID=SqlDataSource1 runat=server ConnectionString=<%$ ConnectionStrings:MainConnStr %> SelectCommand=SELECT
I am working on my first Adobe Flex application, and have a code section
I have the following section of code. Is there anyway that I can use
I have a section of code that sends email from SMTP server. The code
I have a section of code that is being used to determine if a
I have a section of code that can be summarised as follows; void MyFunc()
I have the following section of code in an app that I am writing:
I have following Code Block Which I tried to optimize in the Optimized section
I have some VERY simple code to return the title for a section header:
I have a section of code where the user enters input from the keyboard.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.