ASP.NET development actual combat - (VII) and ASP.NET database

  Describes the use of ASP.NET MVC to develop a blog system, and has begun to take shape, you can view the list of articles page, you can click on the list of articles in which the article view details in the previous article, which has completed the beginning View a list of articles two and see the needs of readers needs analysis, but the biggest problem now is the article data is still "static".
  All data is stored in memory, the system will automatically add the data is hard-coded in the code file, the server every time you restart will only retain this data at initialization, and does not provide the "author" of the management interface, "author" unable to manage these data, even if it can be managed useless, because the server restarts data does not exist, this is the worst. Originally used to save an HTML file, although difficult to modify article, but at least in the form of a data file is saved on the hard disk, as long as the hard disk is not bad, then the data is never lost, but now is not the same, how the data persistence it ?
  Database is the best choice (sometimes also need to manage data files, you need to choose the actual application scenarios, such as configuration files), and of course you can use way to manage file data, in fact, is a special database files, database files can be unique only way to access the data management, such as insert, delete, update, and quickly find and so on, these functions do not need to write your own, which is a database management system provided by (DBMS, database management System), database and there are many types of enterprise applications in general are commonly used in relational databases, there are SQL Server, MySQL, Oracle, PostgreSQL, etc., they are able to provide a reliable guarantee for data storage applications.
  For ASP.NET is the most common and the most support, of course, SQL Server, but due to the open source MySQL and PostgreSQL are, to some extent, can use it for free, so it is often used .NET developers, this series of articles SQL Server will use both databases and MySQL to implement data storage capabilities, but also can be seen on a different database application will be affected.

  This article describes how to use the following two points to a SQL Server database in ASP.NET:

  ● Use SQL Server to store data
  ● access SQL Server in ASP.NET

Use SQL Server to store data

  1. Create a Blog using SQL Server database, and the Posts table, table fields corresponding to the Post class (how to use SQL Server is not within the scope of this series, it will ignore many of the details):

  

 

  2. Add the data in the table:

  

Access SQL Server in ASP.NET

  The article said before the database is actually a special kind of file, then use it to access through the unique way, how to connect to it ASP.NET and SQL Server to use it? ADO.NET. (ADO.NET More information: http://blog.csdn.net/dreamcatchergo/article/details/9729525 )
  now modify the original data BlogRepository get through static arrays, using ADO.NET to retrieve data from the database, ADO.NET System.Data.dll related classes in the assembly, and provides a different ADO.NET data provider for different data sources, which are used to connect different data sources:
  the SQL Server: the System.Data.SqlClient
  the OleDb: the System.Data.OleDb
  Odbc: System.Data.Odbc
  the Oracle: System.Data.OracleClient

  Used in this example is SQL Server, so it is necessary to support the System.Data and System.Data.SqlClient, System.Data and exists by default when you create a project.

  1. Installation System.Data.Sqlclient Nuget Package Manager library by:

  

  2. Modify BlogRepository, allowed to get data from a database:

  

  

  The above code has three important objects are SqlConnection, SqlCommand, SqlDataReader, their effects are connected by connecting string database, SQL statements and execute SQL parameters (stored procedure can also be performed), a reading result of the implementation of SQL return .

  3. The program execution results:

  

  

 

summary

  This chapter describes how to use the database to manage the data, and then connect via ADO.NET database for articles from the database and then displayed on the page, this application has now "move", and only need to update the contents of the database, page content will also change accordingly. But there is a problem is that every time to get data from the database and then have to write SQL statements to obtain the appropriate data fields used to create the entity objects returned from the data set. Is there a better way to solve?

   

reference:

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

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

 

Welcome to add a personal Micro Signal: Like if thoughts.

I welcome the attention of the public numbers, not only recommend the latest blog for you, there are more surprises waiting for you and resources! Learn together and common progress!

 

Guess you like

Origin www.cnblogs.com/cool2feel/p/11544507.html