Today, I started to learn about connection objects in ADO. Net (I)-connection SQL Server with sqlconnection objects

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.