Asp. NET has no magical--asp. NET and database

Source: Internet
Author: User
Tags how to use sql server postgresql how to use sql

In the previous article, we introduced the use of ASP. NET MVC to develop a blog system, and has been the first prototype, you can view the article List page, you can also click on the article List of one of the articles to view the details, which has completed the first needs analysis of the reader's view list and view the article two requirements, But the biggest problem now is that the article data is still "static".
All data is stored in memory, and the system automatically adds data that is hardcoded in the code file when it is initialized, and the server only retains the data each time it restarts, and does not provide the "author" Management Interface, "the author" cannot manage the data, even if it can be managed is useless, Because the data does not exist after the server restarts, this is the worst. Originally saved by the HTML file is not easy to modify, but at least the data in the form of files are saved on the hard disk, as long as the hard drive is not bad, then the data will never be lost, but now is not the same, how to persist the data?
Database is the best choice (sometimes you will need to use files to manage the data, you need to choose according to the actual application scenario, such as configuration files), of course, you can also use the way the file to manage the data, in fact, the database is a special kind of file, but the database file can be unique access to the data management, such as INSERT, delete, update and quick Find, and so on, these functions do not need to write themselves, they are provided by the database management system (Dbms,database Management systems), there are many types of databases, in general enterprise applications are commonly used in the relational database, The main features are SQL Server, MySQL, Oracle, PostgreSQL, etc., all of which provide a reliable guarantee for the application's data storage.
The most common and supported of course is SQL Server for ASP, but since MySQL and PostgreSQL are open source and can be used free of charge, they are often. NET developers, this series of articles will use SQL Server and MySQL two databases to implement data storage capabilities, as well as see how different databases can affect an application.

using SQL Server to store data

1. Use SQL Server to create a blog database, and Posts tables, table fields corresponding to the post class (How to use SQL Server is not within the scope of this series of articles, so many details are ignored):

  

2. Add data to the table:

  

Accessing SQL Server in ASP.

The article said earlier that the database is actually a special kind of file, and then use it in a unique way to access it, so how do I connect and use SQL Server? ADO. (See more information: http://blog.csdn.net/dreamcatchergo/article/details/9729525)
Now modify the original blogrepository that gets the data through a static array, Using ADO to get data from the database, the ADO-related classes are in the System.Data.dll assembly, and ADO provides different data providers for different data sources, which are used to connect different data sources:
SQL Server:System.Data.SqlClient
OleDb:System.Data.OleDb
Odbc:System.Data.Odbc
Oracle:System.Data.OracleClient

In this case, SQL Server is used, so support for System.Data and System.Data.SqlClient is required, System.Data when the project is created and the default exists.

1. Install the System.Data.Sqlclient library through the NuGet Package Manager:

  

2. Modify the blogrepository to get the data from the database:

  

  

There are three important objects in the code above, namely SqlConnection, SqlCommand, SqlDataReader, which are used to connect the database through a connection string, execute SQL through SQL statements and parameters (or execute stored procedures), Reads the results returned by the execution SQL.

3. Program execution Results:

  

  

Summary:
This chapter describes how to use the database to manage the data, then connect to the database via ADO, get the article data from the database, and then display to the page, now the application has "moved", only need to update the contents of the database, the page content will also change. But one problem is that every time you get the data from the database, you write the SQL statement and then get the data from the data set that is returned to the corresponding field to create the entity object. Is there a better way to solve it?

Reference:

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/

http://blog.csdn.net/dreamcatchergo/article/details/9729525

This article connects: http://www.cnblogs.com/selimsong/p/7651460.html

Asp. NET no magic--Directory

Asp. NET has no magical--asp. NET and database

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.