C# Reconstruction of the computer room charging system-a must-see for getting started! Guiding ideology

How to enter the mobile room to reconstruct


sqlHelper connects to the database

  • What is sqlHelper (database helper class)
    SqlHelper is used to simplify your repeated writing of those database connections (SqlConnection), SqlCommand, SqlDataReader and so on. After SqlHelper is encapsulated, it is usually only necessary to pass in some parameters to the method, such as database connection string, SQL parameters, etc., to access the database
  • How to write sqlHelper
    sqlHelper as the core function code for connecting to the database must be written in the DAL layer
public class sqlHelper
    {
    
            //从配置文件中读取连接字符串
            private static readonly string connStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            //ExecuteNonQuery(); //执行命令的方法: insert update delete
            //返回值类型为int型。多用于执行增加,删除,修改数据,返回受影响的行数。当select操作时,返回-1.
            //ExecuteNonQuery()方法主要用户更新数据,通常它使用Updae,Insert,Delete语句来操作数据库,
            //其方法返回值意义;对于Update,Insert,Delete 语句 执行成功是返回值为该命令所影响的行数,如果影响的行数为0时返回的值为0,
            //params是关键字:是可变参数的意思,目的是省略手动构造数组的过程,直接指定对象编译器会帮助我们构造数组,并将对象加入数组中传递过来
            public static int ExcuteNonQuery(string sql, params SqlParameter[] pms)
            {
    
    
                using (SqlConnection conn = new SqlConnection(connStr))//创建连接对象
                {
    
    
                    SqlCommand cmd = new SqlCommand(sql, conn);//创建command连接对象
                    cmd.Parameters.AddRange(pms);
                    conn.Open();//打开链接
                    int n = cmd.ExecuteNonQuery();//执行命令并返回受影响的行数
                    cmd.Parameters.Clear();
                    return n;
                }
            }
            //Executescaler(); 获取首行首列的方法
            //它的返回值类型多为int类型。它返回的多为执行select查询。得到的返回结果为一个值的情况,比如使用count函数求表中记录个数或者使用sum函数求和等。
            //ExecuteScalar()方法也用来执行SQL语句,但是ExecuteScalar()执行SQL语句后的返回值与ExecuteNonQuery()并不相同,
            //ExecuteScalar()方法的返回值的数据类型是Object类型。
            //如果执行的SQL语句是一个查询语句(SELECT),则返回结果是查询后的第一行的第一列,
            //如果执行的SQL语句不是一个查询语句,则会返回一个未实例化的对象,必须通过类型转换来显示,
            public static object ExecuteScalar(string sql, params SqlParameter[] pms)
            {
    
    
                using (SqlConnection conn = new SqlConnection(connStr))//创建链接对象
                {
    
    
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.Parameters.AddRange(pms); //添加参数
                    conn.Open();//打开链接
                    object o = cmd.ExecuteScalar();//执行命令,获取查询结构中的首行首列的值
                    cmd.Parameters.Clear();
                    return o;
                }
            }
            //获取结果结果集
            public static DataTable GetDataTable(string sql, params SqlParameter[] pms)
            {
    
    
                using (SqlConnection conn = new SqlConnection(connStr))//创建链接对象
                {
    
    
                    SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);//创建适配器对象
                    DataTable dt = new DataTable();//构造数据表用于接受查询结果
                    adapter.SelectCommand.Parameters.AddRange(pms);//添加参数
                    adapter.Fill(dt);//执行结果,fill方法内部自动打开链接,不需要Conn。open();
                    adapter.SelectCommand.Parameters.Clear();//清空集合
                    return dt;//返回结果集
                }
            }
        }
  • How to use sqlHelper
    This code focuses on comments! The comment indicates the applicable type of the method, the return type, and the meaning of the return value. For example,
    Insert picture description here
    it clearly explains the use of ExecutiveNonQuery. This is an important basis for us in selecting statements.

Seven-layer concept

  • The extension of UBD three-tier The
    seventh-tier is based on the original three-tier in order to facilitate code reading, decoupling, more in line with object-oriented thinking, and more in line with the requirements of code design.
    For example:
    1. Project name and description: ( The implementation steps are: 4-1-3-5-2-7-5-6-1)
    1. UI = presentation layer
    2, BLL = business logic layer
    3, IBLL = business logic interface layer definition
    4, Model = business entity
    5. Factory = abstract factory (create reflection)
    6. DAL = data layer
    7. IDAL = data interface layer definition

2. Project reference relationship
1. UI references IBLL, Model
2. BLL references IDAL, Model, use DALFactory to create instances and implement interface methods.
3. IDAL/IBLL refers to Model.
4. Model has no reference.
5. DALFactory references IDAL and returns to BLL for use.
6. DAL references Model and IDAL to implement methods in the interface.
3. Implementation steps
1. Create a Model and implement business entities.
2. Create an IBLL according to the required return value type and incoming parameters.
3. Use abstract factory plus reflection + configuration file BLL to realize the interface
4. D layer the same process
5. U layer passes in parameters, receives the value returned by D layer to B layer, and returns the result to UI layer after logical processing

Note:
1. Be sure to add a reference, otherwise the layers cannot be connected.
2. If you encounter an error, you must breakpoint debugging to see the direction of the incoming value and the correctness of the code writing.
3. The logic must be connected, the best first Draw the flowchart
4. Pay attention to modify the project dependencies in the solution.
5. Pay attention to adding references to each project in the solution.

Decoupling thinking

  • What is coupling?
    A good software project must have high cohesion and low coupling.
    But what is coupling?
    Generally speaking, class module one depends on class module two. When we need to change class module two, class module one also needs to be changed.
  • How to decoupling
    The introduction of the interface is a good decoupling method. Of course, there is also the encapsulation of design pattern method classes, but they are all based on the application of the interface

Use of interface

  • What is an interface?
    The interface defines the grammatical contract that all classes should follow when inheriting the interface. The interface defines the "what" part of the grammar contract, and the derived class defines the "how" part of the grammar contract.
    The interface defines properties, methods, and events, which are all members of the interface. The interface only contains member declarations. The definition of members is the responsibility of the derived class. The interface provides a standard structure that derived classes should follow.
    The interface makes the class or structure that implements the interface consistent in form.
    Generally speaking, the interface is divided into two parts: what is there and what is missing. For example:
 public interface IDeleteonlinestu
    {
    
    /// <summary>
    /// 删除上机表中的学生
    /// </summary>
    /// <param name="cardID">传入的值</param>
    /// <returns></returns>
        int deleteonlinestu(string cardID);
    }

This is how to interpret a simple interface code?
Insert picture description here
The brackets are what we have, that is, the value that needs to be passed in,
and "int" is the return value type that we don’t have to receive from the implementation class

  • Implementation class implements interface
    Insert picture description here

Welcome everyone to give guidance and make progress together

Guess you like

Origin blog.csdn.net/weixin_44693109/article/details/108702615