Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, is there anyone could help??
I am doing my project about system . I am using software visual basic .net 2005 and sql 2005. In this system i must create interface which contain button use to check database connection if it online or offline. Example if my database is not connect it will show message database offline and if my database if connect it will show message your database if online and it will direct to another interface ..My Probleam is once i click the button it not show anything.. below is my coding.. need your advice

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class DatabaseStatus
    
    Public Shared Sub SqlConnectionExample()

        Using con As New SqlConnection
            con.ConnectionString = "Data Source=.\sqlexpress;Database=AdventureWorks;Integrated Security=SSPI;"
            con.Open()

            If con.State = ConnectionState.Open Then
                Console.WriteLine("SqlConnection Information:")
                Console.WriteLine("  Connection State = " & con.State)
                Console.WriteLine("  Connection String = " & con.ConnectionString)
                Console.WriteLine("  Database Source = " & con.DataSource)
                Console.WriteLine("  Database = " & con.Database)
                Console.WriteLine("  Server Version = " & con.ServerVersion)
                Console.WriteLine("  Workstation Id = " & con.WorkstationId)
                Console.WriteLine("  Timeout = " & con.ConnectionTimeout)
                Console.WriteLine("  Packet Size = " & con.PacketSize)
            Else
                Console.WriteLine("SqlConenction failed to open.")
                Console.WriteLine("  Connection State = " & con.State)
            End If
            con.Close()
        End Using
    End Sub

    Public Shared Sub OleDbConnectionExample()
        Using con As New OleDbConnection
            con.ConnectionString = "Provider=SQLOLEDB;Data Source=.\sqlexpress;Initial Catalog=AdventureWorks;Integrated Security=SSPI;"
            con.Open()
            If con.State = ConnectionState.Open Then
                Console.WriteLine("OleDbConnection Information:")
                Console.WriteLine("  Connection State = " & con.State)
                Console.WriteLine("  Connection String = " & con.ConnectionString)
                Console.WriteLine("  Database Source = " & con.DataSource)
                Console.WriteLine("  Database = " & con.Database)
                Console.WriteLine("  Server Version = " & con.ServerVersion)
                Console.WriteLine("  Timeout = " & con.ConnectionTimeout)
            Else
                Console.WriteLine("OleDbConnection failed to open.")
                Console.WriteLine("  Connection State = " & con.State)
            End If
            con.Close()
        End Using
    End Sub
    Public Shared Sub Main()
        SqlConnectionExample()
        OleDbConnectionExample()
    End Sub
End Class


[Edited Always wrap your code in "pre" tags[/Edited]
Posted
Updated 6-Aug-11 10:50am
v2

1 solution

Everything seems correct. I tested console and Windows Forms with output to TextBox. Check your connection string.
Update: Just add exception handling

Try
        ...
        Catch SqlExceptionErr As SqlException
            'Write the exception
            Console.WriteLine(SqlExceptionErr.Message)
        Catch InvalidOperationExceptionErr As InvalidOperationException
            'Write the exception
            Console.WriteLine(InvalidOperationExceptionErr.Message)
    End Try
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900