Database Connection in ASP. NET

Source: Internet
Author: User

In fact, it cannot be a summary. I just need to repeat what I learned today, hoping to deepen my memory.
Recently, a project is a laboratory information management system based on WEB. ASP was used in the past. However, ASP cannot reflect the concept of program design, and when I see. After NET, I learned that this is a good thing, and it is also very popular nowadays. So I read it and learned how to be a website. After all, the hardest effort is to perform database operations, and the artist can change it slowly, database operations are the core, SQL, stored procedures, classes, and three-layer structures. I have tried these operations in the ASP era, but at last, I just made a DLL and registered it as a component. After using it for a while, I had a great sense of accomplishment. But how can I register components for the applied host, so I can only be empty and useless. Now it's okay. . NET, I can do a lot of things for program design, instead of thinking about how to insert the <%... %> statement into it!
Since the database is so important, let me start with it. Naturally, it is still a good way to think of ADO:
I. Conection
SqlConnection conn = new SqlConnection (ConfigurationSettings. etetgins ["ConnectionString"]);
2. Execute Command
SqlCommand myCommand = new SqlCommand ("Select...", conn );

Okay. After the above two steps, we can say that we are ready. This is often necessary. We have to choose the method to operate the database: there are two ways:
1. Use DataReader to get data from the data source row by row and put it into the buffer zone for processing. However, the disadvantage of this method is that it can only be read in sequence and cannot be returned to the previous one.

Method: try {
SqlDataReader myDataReader = new SqlDataReader ();
MyDataReader = myCommand. ExecuteReader (); // returns the SqlDataReader set
While (myDataReader. Read ())
{
... // Read the statement
}
MyDataReader. Close ()
}
Catch (...){....}

2. Use a DataSet object to store data in the memory for processing. The advantage of this method is that the virtual table corresponding to the real table in the database can be created in the memory, making data operations convenient and flexible.

Method:
Public DataSet CreateDataSource ()
{
SqlConnection conn = new SqlConnection (ConfigurationSettings. etettings ["ConnectionString"]);
SqlCommand myCommand = new SqlCommand ("Select * from User_Info", conn );

DataSet ds = new DataSet ();
SqlDataAdapter myAdapter = new SqlDataAdapter ();
MyAdapter. SelectCommand = myCommand;
MyAdapter. Fill (ds, "UserInfo ");

Return ds;
}
4. After you get the dataset, you can see it all through the sea. However, when I found a small error, how can I reference a data table, I mixed the VB syntax with the C # syntax, for example:
In C #, ds. Tables ["UserInfo"]. Rows [0] ["UserID"]. ToString ();
In VB, [] is represented by (). in C #, [] is an attribute and () is a method. Therefore, we hope you will not be fooled by this detail in the future.

There are naturally a lot of database operations. Let me write this article first and add it later. We are learning about the database principle and hope we can have a deeper understanding in the future.

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.