Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Knowing if connection failed to sql server in C#

Knowing if connection failed to sql server in C#

Scheduled Pinned Locked Moved Database
databasequestioncsharpsql-serversysadmin
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pluss4
    wrote on last edited by
    #1

    I'm not good at SQL indulgence. Am not sure if it's a theme for database or C #. I want to be able to distinguish when logging into my SQL database if the person has used the correct user name. As usual connection to SQL Server looks like this in C #. Using SQL connection and not the Windows connection. SqlConnection con = new SqlConnection ("connection string with the user name here"); con.Open (); Question: How can I know if the connection with the user name for the SQL server has failed using C # code.

    J 1 Reply Last reply
    0
    • P pluss4

      I'm not good at SQL indulgence. Am not sure if it's a theme for database or C #. I want to be able to distinguish when logging into my SQL database if the person has used the correct user name. As usual connection to SQL Server looks like this in C #. Using SQL connection and not the Windows connection. SqlConnection con = new SqlConnection ("connection string with the user name here"); con.Open (); Question: How can I know if the connection with the user name for the SQL server has failed using C # code.

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2
              SqlConnection con = new SqlConnection("connection string with the user name here");
              try
              {
                  con.Open();
              }
              catch (SqlException ex)
              {
                  //Handle exception here
              }
      

      Wrong is evil and must be defeated. - Jeff Ello

      Richard DeemingR P 2 Replies Last reply
      0
      • J Jorgen Andersson
                SqlConnection con = new SqlConnection("connection string with the user name here");
                try
                {
                    con.Open();
                }
                catch (SqlException ex)
                {
                    //Handle exception here
                }
        

        Wrong is evil and must be defeated. - Jeff Ello

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Jörgen Andersson wrote:

        catch (Exception ex)

        You should only be catching exception types which you can handle. In this case, it should be sufficient to catch SqlException.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        J 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Jörgen Andersson wrote:

          catch (Exception ex)

          You should only be catching exception types which you can handle. In this case, it should be sufficient to catch SqlException.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          Yes, you're right, I've updated the answer.

          Wrong is evil and must be defeated. - Jeff Ello

          1 Reply Last reply
          0
          • J Jorgen Andersson
                    SqlConnection con = new SqlConnection("connection string with the user name here");
                    try
                    {
                        con.Open();
                    }
                    catch (SqlException ex)
                    {
                        //Handle exception here
                    }
            

            Wrong is evil and must be defeated. - Jeff Ello

            P Offline
            P Offline
            pluss4
            wrote on last edited by
            #5

            Thank you.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups