How to connect How to check SQL connection in C#.Net?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jeyavel
    New Member
    • Aug 2010
    • 3

    How to connect How to check SQL connection in C#.Net?

    Hi,

    i want check my SQL server connection before to connect with DB, and i need to update the status of SQL Server connection in my GUI.

    can any one give some idea to get output?
  • George N
    New Member
    • Aug 2010
    • 7

    #2
    I f you are interested getting the solution in VB.NET I think I have the solution for you

    Comment

    • Jeyavel
      New Member
      • Aug 2010
      • 3

      #3
      Just post code here(only connection checking code) from that i can able to convert C#.net

      Comment

      • George N
        New Member
        • Aug 2010
        • 7

        #4
        Following is how to know if youare connected to the database and it continues help you to back p the DB as well. if you want more , you can check my entire article on msdn.com

        The home for technical questions and answers at Microsoft. Get started asking, answering, and browsing questions about products like .Net, Azure, or Teams.




        GEORGESNAFFAH is the sqlconnection string that targets to the database



        Dim GEORGESNAFFAH As New SqlConnection(" Data Source=.\SQLEXP RESS;Integrated Security=True")





        ' Defining the SQL Commands.



        Dim cmd As SqlCommand







        ' we will define the sql string containing the sql to be executed , we have 1 so we define 1 string.



        Dim sql As String


        'Open Connection_ & _string and definition of SQL CODE

        GEORGESNAFFAH.O pen()



        ' If the Connection is openned succesfully the state of GEORGESNAFFAH Connectionstrin g will display a message



        If GEORGESNAFFAH.S tate = ConnectionState .Open Then

        MsgBox("Connect ion Successfully Created - Press OK to start the Backup process - This might take a few minutes .")




        'Script to Back up the Database

        sql ="BACKUP DATABASE [data2] TO DISK = N'C:\DB_Backup\ data2.bak' WITH NOFORMAT, NOINIT, NAME = N'LGAS_OFFICE-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10"






        ' command = new sql command (sql + name of the sqlconnection)

        cmd =New SqlCommand(sql, GEORGESNAFFAH)

        cmd.ExecuteNonQ uery()







        'Close Connection

        cmd.Dispose()







        ' After that GEORGESNAFFAH will close and the Form will automatically Close

        GEORGESNAFFAH.C lose()

        MsgBox("Databas e Backup Completed Successfully")





        'Close the form



        Me.Close()







        End If



        End Sub

        Comment

        • George N
          New Member
          • Aug 2010
          • 7

          #5
          let me know if it helped you

          Comment

          • Jeyavel
            New Member
            • Aug 2010
            • 3

            #6
            Thanks George,
            Here is the code i am checking SQL connection but i couldn't able to get status frequently

            Scenario :

            * stop the sql server service from services window
            * Running the project and will show status "Connection Not Available"
            * Start Sql server service and displaying "Connection Live"
            * And Again Stop SQL server service and i am not getting the status as ""Connectio n Not Available". It's returning status as "Connection Live"
            * It's not get in to the catch block

            Code:

            private void timer1_Tick(obj ect sender, EventArgs e)
            {
            bool Flag = false;
            try
            {
            using (SqlConnection con = new SqlConnection(s trcon))
            {
            con.Open();
            }
            }
            catch (SqlException s)
            {
            Flag = true;
            label1.Text = "Connection Not available";
            }
            finally
            {
            if (Flag == false)
            {
            label1.Text = "Connection Live";
            }
            }
            }

            Comment

            Working...