App.config connection string and use hierarchical thinking project

Copyright Notice: Copyright: @@ individual all https://blog.csdn.net/y20_20/article/details/90760760

C # development of some common hierarchical thinking, and database connection string

Content Directory

First, use a connection string in Appconfig in use and some precautions
extraction Second, the entity classes and data access classes and some simple instructions
III project stratified and hierarchical thinking

Text content

First, use a connection string in Appconfig in use and some precautions

1, the use of steps:
First: Right Add New Item, add an application configuration file App.config
second: added to the root directory, do not need to modify the file name, contents are written within connectionStrings node, the node name must be written on
sample Code:
third: introducing System.Configuration namespace
IV: connecting string: string connString = ConfigurationManager.ConnectionStrings [ "connString ( App.config values in the name)"] ToString ();.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="connString" connectionString ="Server=CHINA-20181227Q\server;DataBase=SMDB;Uid=sa;Pwd=123456"/>
  </connectionStrings>
</configuration>

2, connecting some of the possible problems of failure:

  • App.config file is modified, not placed in the root directory of the program
  • <Add name = "wrong here (pay more spaces, wrong letters, etc.)" connectionString = "tantamount sample code and structure"
  • Not on <connectionStrings inside the node, or node name is wrong, etc.

Second, the entity class and extracting data access classes and some simple instructions

1, the concept of entity classes: some of the only property category to add in the project, and consistent attribute names and data types and database table field names and data types in the class, the class name and show the same property names and field names the same, data types and data type field in the same table. Further in the database char, text, nvarchar so string corresponding to the string type in C #

Database data tables students:
Here Insert Picture Description
the corresponding entity class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace text3
{
    [Serializable]//可序列化标记,以后需要序列化存储图片信息
    public class Students
    {
        public int StudentId { get; set; }
        public string  StudentName { get; set; }
        public string Gender { get; set; }
        public DateTime Birthday { get; set; }
        public string StudentIdNo { get; set; }
        public string StuImage { get; set; }
        public int Age { get; set; }
        public string PhoneNumber { get; set; }
        public string StudentAddress { get; set; }
        public int ClassId { get; set; }
    }
}

2, the entity class how many questions should be added:

  • Mapping entity class is essentially the structure of a table
  • Generally the number of tables on the number of entity classes should be added to achieve the object table and entity-one mapping relationship, but in order to facilitate the processing of data in the program, you also need to add some tables and not a mapping between an entity class called extended entity class

3. Why use entity classes?
One reason:

  • Development of database management software, essentially processing the data.
  • From the perspective of C #, C # is a fully object-oriented programming language, any data processing should have the appropriate entity (object)
  • From the perspective of the database, the database table is a set of entities, each record in the table corresponds to a target
  • A C # data may also be understood as an object, so that the database can be generated and the contact C #

Cause two:

  • The six principles of object-oriented programming, data display and user needs operations, business logic, data separation operation, the entity type in these layers will function as data transfer and packaging
  • If you do not pass the data entity class in these layers, it will lead to too many parameters, the interface is cumbersome and unstable, and deal with them more trouble

4, the benefits of using entity classes:

  1. OOP better reflect the characteristics of the package: the data is not fragmented
  2. Calling easy: do not care about the internal structure
  3. The role of passing data between different objects, play isolation: the caller does not have to be concerned at the data, the data transfer does not have to worry about how to use (the caller: Data where to come; messengers: the data to you, whatever you how to use )

5, data access classes:

  • Why use the data access classes: data access classes for a particular entity class, the operation of the corresponding data package, and entities like segregation of duties (that is, data entity classes are how to use)
  • Data Access and entity classes is actually a class broken up into properties and behavior, it is the entity class attributes, data access classes behavior, do so early to avoid the unnecessary waste of resources when transferring data, when necessary, can also be combined into One
  • Entity classes: a package and transfer data; data access classes: a package corresponding to the operation of the data entity class
  • General: entity class and data access classes one to one, but for convenience of data processing may be extended
  • Writing proposed data access class: the entity class name + Service

Third, the project hierarchical thinking

1, three basic items thought : six principles of object-oriented programming
4.2 Layer:
UI interface layer: a package display data and
business logic: a task: a package certain algorithms and logic processing (not including data manipulation and UI operation); two tasks: the transfer tasks, and the data can not directly interact with the UI (recommended naming: the entity class name suffix + manager)
data manipulation layer: including data access entity classes and classes, specialized data processing
** 3, all other layered based on these three combined add: ** For example: layers UI layer or the like is data access layer

Guess you like

Origin blog.csdn.net/y20_20/article/details/90760760