Preliminary database operations in ASP. NET (3)

Source: Internet
Author: User

Database Operations in ASP. NET-connect to and open the database

For users who have used beta2, it is very easy to learn the content of this article, because. net official version is not much different from beta2 version, so you can run programs in beta2 normally without making any changes (or only making a few changes.
Here we will talk about how to open the database. We do not agree to write asp.net using a text editor such as wordbook or editplus, so this article is all about VS. NET development tools.
Create a new database connection. First, double-click OleDbConnection from the toolbox or double-click SqlConnection.

 

Note: The difference between OleDbConnection and SqlConnection is that OleDbConnection is suitable for connecting to any type of Database (such as Oracle, SQL Server, ACCESS, etc.), and SqlConnection is dedicated to connecting SQL Server (ms SQL) database, it is said that the efficiency is higher than OleDbConnection. If your database is ms SQL, double-click SqlConnection. For other databases, use OleDbConnection. The same principle SqlDataAdapter, SqlCommand is dedicated for (ms SQL) database, and OleDbDataAdapter, OleDbCommand applies to all databases, if you double-click is SqlConnection, in future database operations, you can only use SqlDataAdapter and SqlCommand. Similarly, if you double-click OleDbConnection, you can only use OleDbDataAdapter and OleDbCommand in future database operations. Here we select OleDbConnection.
After double-clicking OleDbConnection, a database connection source named oleDbConnection1 will appear at the bottom of the window.


In the Properties window, we can change oleDbConnection1 to MyConnection,

To connect to a database, perform the following steps: Select <new connection...> from ConnctionStr in. The following window is displayed:


Select the database type "Next" and select the correct database location, as shown in figure


(After the connection test is successful) Click OK, and then (double-click any blank position in the editing window) Open the corresponding cs file. The corresponding cs file is automatically added to the program.
Protected System. Data. OleDb. OleDbConnection MyConnection; Code
Added the private void InitializeComponent ()
This. MyConnection = new System. Data. OleDb. OleDbConnection ();
//
// MyConnection
//
This. MyConnection. ConnectionString = @ "Provider = SQLOLEDB.1; Persist Security
Info = False; User ID = sa; Initial Catalog = house; Data Source = CYH; Use Procedure
For Prepare = 1; Auto Translate = True; Packet Size = 4096; Workstation ID = CYH; Use
Encryption for Data = False; Tag with column collation when possible = False ";
//
Now it is easy to open the database. You only need to use
MyConnection. Open (); // The database is opened.
MyConnection. Close (); // Close the database
For example, you can open the database in private void Page_Load (object sender, System. EventArgs e ).
Private void Page_Load (object sender, System. EventArgs e)
{
MyConnection. Open ();
// Place the user code here to add, delete, modify, and query the database.
MyConnection. Close ();
}


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.