1. Determine the database connection status:
(1) Using Method:
Public partial class _ default: system. web. UI. page
{< br> protected void page_load (Object sender, eventargs e)
{< br> using (sqlconnection connection = new sqlconnection (""))
{< br> connection. open ();
If (connection. state = connectionstate. open)
{< br> // already opened
}< BR >}
(2) "try {} catch {}" Method
Try
{
Sqlconnection conn = new sqlconnection ("Data Source = myserveraddress; initial catalog = mydatabase; user id = myusername; Password = mypassword ;");
Conn. open ();
}
Catch
{
// Connection failed
}
(3) always return the correct data connection
Private sqlconnection connection;
Public sqlconnection connection
{
Get
{
String connectionstring = "Server = ..; database = dB; uid = sa; Pwd = 123456 ";
If (connection = NULL)
{
Connection = new sqlconnection (connectionstring );
Connection. open (); // This indicates that connection is enabled.
}
Else if (connection. State = system. Data. connectionstate. Closed)
{
Connection. open (); // This indicates that connection is enabled.
}
Else if (connection. State = system. Data. connectionstate. Broken)
{
Connection. Close ();
Connection. open (); // This indicates that connection is enabled.
}
Return connection; // in any case, an open connection is always returned.
}
}
2. Return the names of all databases on the local computer.
Use master
Select dbid, db_name (dbid) as db_name
From sysdatabases
Order by dbid
Go
3. Return the names of all tables in the specified database.
Select name from SYS. sysobjects where type = 'U'
4. Return all fields in the specified table
select * From syscolumns where ID in (select ID from sysobjects where (name = '"+ tbname + "'))