the development process of a database application is generally divided into the following steps:
- Create a database
- To connect to a database using a Connection object
- Execute SQL commands against the data source using the Command object and return the data
- 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