check SQL db exist...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon

    #1

    check SQL db exist...

    hi,

    Could anyone pls tell me what is the best way to check if a database exist
    on the SQL server or not?

    any example code would be appreciated!

    many thanks in advance.
    Jon
  • Michael C#

    #2
    Re: check SQL db exist...

    Something like this should do the trick:

    Dim SqlCon As SqlConnection
    Dim DatabaseExists As Boolean = True
    Try
    SqlCon = New SqlConnection(" SERVER=(local); " & _
    "INITIAL CATALOG=databas e_name;" & _
    "USER ID=mcoles;PASSW ORD=1qaz1qaz;")
    SqlCon.Open()
    Catch ex As SqlException
    If ex.Number = 4060 Then
    DatabaseExists = False
    Else
    Throw (ex)
    End If
    Finally
    If Not (SqlCon Is Nothing) Then
    SqlCon.Dispose( )
    End If
    End Try



    "Jon" <Jon@discussion s.microsoft.com > wrote in message
    news:CFA6DE7F-F231-46CF-A855-358F95E9BB79@mi crosoft.com...[color=blue]
    > hi,
    >
    > Could anyone pls tell me what is the best way to check if a database exist
    > on the SQL server or not?
    >
    > any example code would be appreciated!
    >
    > many thanks in advance.
    > Jon[/color]


    Comment

    • Michael C#

      #3
      Re: check SQL db exist...

      Ooops. I set up a test account to do this. That username and password need
      to be modified to yours to run it. Sorry about that.

      "Michael C#" <xyz@abcdef.com > wrote in message
      news:7Bbie.2261 0$NZ1.14392@fe0 9.lga...[color=blue]
      > Something like this should do the trick:
      >
      > Dim SqlCon As SqlConnection
      > Dim DatabaseExists As Boolean = True
      > Try
      > SqlCon = New SqlConnection(" SERVER=(local); " & _
      > "INITIAL CATALOG=databas e_name;" & _
      > "USER ID=mcoles;PASSW ORD=1qaz1qaz;")
      > SqlCon.Open()
      > Catch ex As SqlException
      > If ex.Number = 4060 Then
      > DatabaseExists = False
      > Else
      > Throw (ex)
      > End If
      > Finally
      > If Not (SqlCon Is Nothing) Then
      > SqlCon.Dispose( )
      > End If
      > End Try
      >
      >
      >
      > "Jon" <Jon@discussion s.microsoft.com > wrote in message
      > news:CFA6DE7F-F231-46CF-A855-358F95E9BB79@mi crosoft.com...[color=green]
      >> hi,
      >>
      >> Could anyone pls tell me what is the best way to check if a database
      >> exist
      >> on the SQL server or not?
      >>
      >> any example code would be appreciated!
      >>
      >> many thanks in advance.
      >> Jon[/color]
      >
      >[/color]


      Comment

      • Jon

        #4
        Re: check SQL db exist...

        thanks, i will give it a try.


        "Michael C#" wrote:
        [color=blue]
        > Ooops. I set up a test account to do this. That username and password need
        > to be modified to yours to run it. Sorry about that.
        >
        > "Michael C#" <xyz@abcdef.com > wrote in message
        > news:7Bbie.2261 0$NZ1.14392@fe0 9.lga...[color=green]
        > > Something like this should do the trick:
        > >
        > > Dim SqlCon As SqlConnection
        > > Dim DatabaseExists As Boolean = True
        > > Try
        > > SqlCon = New SqlConnection(" SERVER=(local); " & _
        > > "INITIAL CATALOG=databas e_name;" & _
        > > "USER ID=mcoles;PASSW ORD=1qaz1qaz;")
        > > SqlCon.Open()
        > > Catch ex As SqlException
        > > If ex.Number = 4060 Then
        > > DatabaseExists = False
        > > Else
        > > Throw (ex)
        > > End If
        > > Finally
        > > If Not (SqlCon Is Nothing) Then
        > > SqlCon.Dispose( )
        > > End If
        > > End Try
        > >
        > >
        > >
        > > "Jon" <Jon@discussion s.microsoft.com > wrote in message
        > > news:CFA6DE7F-F231-46CF-A855-358F95E9BB79@mi crosoft.com...[color=darkred]
        > >> hi,
        > >>
        > >> Could anyone pls tell me what is the best way to check if a database
        > >> exist
        > >> on the SQL server or not?
        > >>
        > >> any example code would be appreciated!
        > >>
        > >> many thanks in advance.
        > >> Jon[/color]
        > >
        > >[/color]
        >
        >
        >[/color]

        Comment

        Working...