Home All Groups Group Topic Archive Search About

How can I determinate if the Database exist before detach it?

Author
31 Mar 2006 8:32 PM
ad
I use the code below to detach a Database, but if the database does not
exist, it will thow exception.
How can I determinate if the Database exist before detach it?



***********************************************************************
            string sConnection = "Data Source=......";
            string sCmdDetach="sp_detach_db 'Health'";
            SqlConnection cnn = new SqlConnection(sConnection);
            cnn.Open();
            SqlCommand cmd= new SqlCommand(sCmdDetach, cnn);
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cnn.Close();
            }

Author
30 Mar 2006 9:11 PM
Marina Levit [MVP]
Connect to the 'master' database, and look in the 'sysdatabases' table.

Show quote
"ad" <fly***@wfes.tcc.edu.tw> wrote in message
news:ut47VkDVGHA.5580@TK2MSFTNGP11.phx.gbl...
>I use the code below to detach a Database, but if the database does not
>exist, it will thow exception.
> How can I determinate if the Database exist before detach it?
>
>
>
> ***********************************************************************
>            string sConnection = "Data Source=......";
>            string sCmdDetach="sp_detach_db 'Health'";
>            SqlConnection cnn = new SqlConnection(sConnection);
>            cnn.Open();
>            SqlCommand cmd= new SqlCommand(sCmdDetach, cnn);
>            try
>            {
>                cmd.ExecuteNonQuery();
>            }
>            catch (Exception ex)
>            {
>                MessageBox.Show(ex.Message);
>            }
>            finally
>            {
>                cnn.Close();
>            }
>
>
>
Author
31 Mar 2006 6:32 AM
Jim Hughes
sp_helpdb
Reports information about a specified database or all databases.

Syntax
sp_helpdb [ [ @dbname= ] 'name' ]

Arguments
[@dbname=] 'name'

Is the name of the database for which to provide information. name is
sysname, with no default. If name is not specified, sp_helpdb reports on all
databases in master.dbo.sysdatabases.

Return Code Values
0 (success) or 1 (failure)

Show quote
"ad" <fly***@wfes.tcc.edu.tw> wrote in message
news:ut47VkDVGHA.5580@TK2MSFTNGP11.phx.gbl...
>I use the code below to detach a Database, but if the database does not
>exist, it will thow exception.
> How can I determinate if the Database exist before detach it?
>
>
>
> ***********************************************************************
>            string sConnection = "Data Source=......";
>            string sCmdDetach="sp_detach_db 'Health'";
>            SqlConnection cnn = new SqlConnection(sConnection);
>            cnn.Open();
>            SqlCommand cmd= new SqlCommand(sCmdDetach, cnn);
>            try
>            {
>                cmd.ExecuteNonQuery();
>            }
>            catch (Exception ex)
>            {
>                MessageBox.Show(ex.Message);
>            }
>            finally
>            {
>                cnn.Close();
>            }
>
>
>
Author
31 Mar 2006 7:22 PM
William (Bill) Vaughn
Use the Connection.GetSchema method.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Show quote
"ad" <fly***@wfes.tcc.edu.tw> wrote in message
news:ut47VkDVGHA.5580@TK2MSFTNGP11.phx.gbl...
>I use the code below to detach a Database, but if the database does not
>exist, it will thow exception.
> How can I determinate if the Database exist before detach it?
>
>
>
> ***********************************************************************
>            string sConnection = "Data Source=......";
>            string sCmdDetach="sp_detach_db 'Health'";
>            SqlConnection cnn = new SqlConnection(sConnection);
>            cnn.Open();
>            SqlCommand cmd= new SqlCommand(sCmdDetach, cnn);
>            try
>            {
>                cmd.ExecuteNonQuery();
>            }
>            catch (Exception ex)
>            {
>                MessageBox.Show(ex.Message);
>            }
>            finally
>            {
>                cnn.Close();
>            }
>
>
>
Author
31 Mar 2006 8:30 PM
Jim Hughes
Won't that get the the default database for the current connection?

If so, he won't be able to detach the database because he is currently using
it.

If the database doesn't exist, then the connection with that
connectionstring will fail anyway!

Show quote
"William (Bill) Vaughn" <billvaRemoveT***@nwlink.com> wrote in message
news:u7hh0hPVGHA.328@TK2MSFTNGP11.phx.gbl...
> Use the Connection.GetSchema method.
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant
> Microsoft MVP
> INETA Speaker
> www.betav.com/blog/billva
> www.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> __________________________________
>
> "ad" <fly***@wfes.tcc.edu.tw> wrote in message
> news:ut47VkDVGHA.5580@TK2MSFTNGP11.phx.gbl...
>>I use the code below to detach a Database, but if the database does not
>>exist, it will thow exception.
>> How can I determinate if the Database exist before detach it?
>>
>>
>>
>> ***********************************************************************
>>            string sConnection = "Data Source=......";
>>            string sCmdDetach="sp_detach_db 'Health'";
>>            SqlConnection cnn = new SqlConnection(sConnection);
>>            cnn.Open();
>>            SqlCommand cmd= new SqlCommand(sCmdDetach, cnn);
>>            try
>>            {
>>                cmd.ExecuteNonQuery();
>>            }
>>            catch (Exception ex)
>>            {
>>                MessageBox.Show(ex.Message);
>>            }
>>            finally
>>            {
>>                cnn.Close();
>>            }
>>
>>
>>
>
>

AddThis Social Bookmark Button