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

Source: Internet
Author: User

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.