Test to see if a DB is attached using ADO.NET/C#/SQL 2000

  • Thread starter Thread starter Derek
  • Start date Start date
D

Derek

I am trying to test to see if a DB is attached using ADO.NET/C#/SQL 2000.

How can I do this. I am new to C#
 
Bustieboy said:
Open a connection in a try ... catch block.

Please consult the .NET Framework SDK docs as it has much better
information than I could provide:
ms-help://MS.NETFrameworkSDKv1.1/cpguidenf/html/cpconaccessingdatawithadonet.htm

But to quickly answer your question, for an MS SQL Server, for example:

string conStr = "Data Source=server;Initial Catalog=Northwind;User
ID=sa;Password=foo";

try
{
using( SqlConnection con = new SqlConnection(conStr) )
{
con.Open();
// If you get here, it's successful
}
}
catch( SqlException se )
{
// If you get here, it wasn't
}

-Chad
 
Back
Top