Database Learning Task Two: Database Connection object SqlConnection

Source: Internet
Author: User

the development process of a database application is generally divided into the following steps:
    1. Create a database
    2. To connect to a database using a Connection object
    3. Execute SQL commands against the data source using the Command object and return the data
    4. Using DataReader and DataSet objects to read and manipulate data from a data source

Now that you've learned how to create a database, here's a look at some of the connection object's content.

The connection object is a "bridge" between the connecting program and the database, and to access the data in the data source, first establish a connection between the program and the data source.

The SqlConnection object is the main way to connect the SQL Server type data source, through the related properties and methods, the connection parameters are set, read and related connection operation.

Create SqlConnection Object

SqlConnection are objects, just like other objects in C #. Many times, you just need to declare and instantiate the SqlConnection as follows:

Mode one: parameterized string

SqlConnection con = new SqlConnection ("Data source= (local); Initial catalog=northwind;integrated Security=sspi");

Mode two: Set connection string properties

SqlConnection con = new SqlConnection ();

Con. connectionstring= @ "Data source= (local); Initial catalog=northwind;integrated Security=sspi";

The first instantiation of the SqlConnection object above uses a constructor with a string type argument. This parameter is called the connection string (connection string). Table 1 describes the general part of the connection string.

Table 1.ado.net The connection string includes some keys / value pairs to indicate how to connect to the database. They include location, name of database, and security authentication.

Integrated security is safe when you are developing on a standalone machine. However, you typically want to indicate the security license for the application that you are using based on the SQL Server user ID. The connection string shown below uses the user ID and the password parameter:

SqlConnection con = new SqlConnection ();

Con. connectionstring= @ "Data source=databaseserver;initial catalog=northwind; User Id=youruserid; Password=yourpassword "";

Note: Data source is set to Databaseserver to indicate that you can indicate a database located on a different machine-across LAN or internet――. Additionally, the User ID and password replace the integrated security parameters.

"Example": Connecting to a SQL Server database using a SqlConnection object

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data;usingSystem.Data.SqlClient; Public Partial classregister:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {                  }    protected voidBtncon_click (Objectsender, EventArgs e) {SqlConnection con=NewSqlConnection (); Con. ConnectionString=@"Data source=.\sqlexpress;database=shopbookdb;integrated Security=sspi; User instance=true"; Con.        Open (); Try{Response.Write ("<script>alert (' Connect database successfully! ') </script>"); Con.        Close (); }        Catch(Exception) {Response.Write ("<script>alert (' Connection database failed! ') </script>"); Con.        Close (); }    }}

The results of the operation are as follows:

Database Learning Task Two: Database Connection object SqlConnection

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.