The connection object is used to establish a connection channel with the data source.
First, let's look at an instance:
Using system;
Using system. Data;
Using system. Data. sqlclient;// SQL Server Data SourceProgram
Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
String connectionstring = getconnectionstring ();
String querystring = "select * From DBO. t_student ;";
// Create a connection object
Sqlconnection connection = new sqlconnection (connectionstring );
{
Try
{
Connection. open (); // Open the database connection
Console. writeline ("successfully connected to SQL Server 2005 Database ");
Console. Readline ();
Connection. Close (); // Close the database connection
Console. writeline ("successfully shut down the database to SQL Server 2005 ");
}
Catch (sqlexception ex)
{
// Tostring () is used to convert exception information to string display.
Console. writeline (ex. tostring ());
Console. Readline ();
}
}
}
Static private string getconnectionstring () // Return the function of the connection string
{
Return "Integrated Security = true; initial catalog = database name; server = server name ";
}
}
}
Complete ~~!
Attribute |
Description |
Connectionstring |
String used to open or connect to the database |
Connectiontimeout |
All waiting time before the connection is terminated and an error is generated |
Datebase |
Name of the database to be used after the left connection of the front database is opened |
Stat |
Current connection status |
Method |
Description |
Open |
Open a connection to the database |
Close |
Close the current connection to the database |
Createcommand |
Create and return a command object related to the connection |
Begintransaction |
Start database transactions |
Changedatabase |
Change the database of the currently opened connection object |