Interaction between servers and clients based on asp.net MVC (1), asp. netmvc

Source: Internet
Author: User
Tags connectionstrings

Interaction between servers and clients based on asp.net MVC (1), asp. netmvc
Architecture

 

L3 Architecture

A three-tier Architecture Based on ASP. NET is proposed to construct a Web application system. The basic content is: Object-oriented UML modeling and Web application system development.

The entire system is divided into four layers: Application presentation layer, business logic layer, data access layer, and data storage layer suitable for ASP. NET development. Web applications constructed in this way

It not only achieves the goal of clear and clear code organization structure, high reusability, applicability, easy maintenance and transplantation, but also improves the development speed of web application systems. Solved the current large

In Web application development, code applicability and reusability are poor, and it is difficult to maintain and transplant.

 

Mvc Framework

MVC is one of three ASP. NET Programming modes.

MVC is a pattern used to design and create a Web application using MVC (Model View Controller Model-View-Controller:

The MVC mode also provides full control over HTML, CSS, and JavaScript.

A Model is a part of the application's data logic.

Typically, model objects are used to access data in databases.

View is a part of the application that processes data display.

Generally, a view is created based on model data.

A Controller is a part of an application that processes user interaction.

Generally, the Controller reads data from the view, controls user input, and sends data to the model.

MVC layering helps you manage complex applications, because you can focus on one aspect within a period of time. For example, you can focus on view settings without relying on business logic.

. It also makes application testing easier.

MVC layering also simplifies group development. Different developers can develop views, controller logic, and business logic at the same time.

Interaction between the client and the server

 

On the server side, how does one obtain and process data in the database? How to establish a connection to the database? How can I Package Multiple Data to realize interaction between server and client information?

 

If mobile phone development focuses on the client, the data and programs in the background are basically not changed.

Take asp.net server connection SQL Server2012 as an example.
 <connectionStrings>    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-CP_campus_WebApp-20150822102435;Integrated Security=SSPI" />    <add name="connStr" connectionString="server=.;uid=sa;pwd=123456;database=dataSet"/>  </connectionStrings>

"ConnStr": the string that we connect to the database.

When SQL authentication is used in the SQL Server database,

"Uid": account, "pwd": Password

// Database connection string (configured by web. config). You can use DbHelperSQLP for multiple databases to implement. public static string connectionString = PubConstant. ConnectionString;
    
For example, you can query a field to determine whether the field exists.
/// <Summary> /// determine whether a field exists in a table. /// </summary> /// <param name = "tableName"> table name </param >/// <param name = "columnName"> column name </param> /// <returns> whether the column name exists </returns> public static bool ColumnExists (string tableName, string columnName) {string SQL = "select count (1) from syscolumns where [id] = object_id ('" + tableName + "') and [name] = '"+ columnName +"' "; object res = GetSingle (SQL); if (res = null) {return false;} return Convert. toInt32 (res)> 0 ;}

You can also encapsulate the data-related operation code in a static class and then call

Using System; using System. collections. generic; using System. linq; using System. text; using System. data. oleDb; using System. data; using System. data. sqlClient; namespace CETP {class SqlHelper {// <summary> // obtain the database connection string information in the configuration file /// </summary> private static string connStr = @ "Data Source = CSIM; initial Catalog = CETP; Integrated Security = True "; // <summary> // customize the ExecuteNonQuery operation // </summary> // <param Name = "SQL"> the SQL statement to be executed (Insert, Update, Delete) </param> // <param name = "parameters"> variable-length array, store the variable value in the SQL statement </param> // <returns> returns the number of rows affected by the database after the SQL statement is executed </returns> public static int ExecuteNonQuery (string SQL, params SqlParameter [] parameters) {using (SqlConnection conn = new SqlConnection (connStr) {conn. open (); using (SqlCommand cmd = conn. createCommand () {cmd. commandText = SQL; cmd. parameters. addRang E (parameters); return cmd. executeNonQuery ();} // end of using} // end of ExecuteNonQuery // <summary> // customize the ExecuteScalar operation // </summary> /// <param name = "SQL"> SQL statement to be executed </param> // <param name = "parameters"> variable-length array, store the variable values in SQL statements </param> // <returns> returns the first column of the dataset obtained from SQL statement execution in the first row </returns> public static object ExecuteScalar (string SQL, params SqlParameter [] parameters) {using (SqlConnect Ion conn = new SqlConnection (connStr) {conn. open (); using (SqlCommand cmd = conn. createCommand () {cmd. commandText = SQL; cmd. parameters. addRange (parameters); return cmd. executeScalar ();} // end of using} // end of ExecuteScalar // <summary> // customize the ExecuteReader operation // </summary> /// <param name = "SQL"> SQL query statement to be executed </param> // <param name = "parameters"> variable-length array, store the variable values in SQL statements </param> // /<Returns> return query result set </returns> public static SqlDataReader ExecuteReader (string SQL, params SqlParameter [] parameters) {SqlConnection conn = new SqlConnection (connStr); conn. open (); using (SqlCommand cmd = conn. createCommand () {cmd. commandText = SQL; cmd. parameters. addRange (parameters); return cmd. executeReader ();} // end of using} // end of OleDbDataReader // <summary> // customize Execu TeDataTable operation, run the SQL query statement </param> only when the query result is small /// <param name = "parameters"> variable-length array, store the variable values in the SQL statement </param> // <returns> returns the dataset after the SQL statement is executed </returns> public static DataTable ExecuteDataTable (string SQL, params SqlParameter [] parameters) {using (SqlConnection conn = new SqlConnection (connStr) {conn. open (); using (SqlCommand cmd = conn. createCommand () {cmd. comma NdText = SQL; cmd. parameters. addRange (parameters); SqlDataAdapter adapter = new SqlDataAdapter (cmd); DataSet dataset = new DataSet (); adapter. fill (dataset); return dataset. tables [0];} // end of using} // end of ExecuteDataTable // <summary> // determines whether the value retrieved from the database is null, and perform conversion /// </summary> /// <param name = "value"> value retrieved from the database </param> /// <returns> If the database this value is DBNull, returns if it is converted to null. Otherwise, this value is directly returned. </returns> Public static object FromDbValue (object value) {if (value = DBNull. value) {return null;} else {return value ;} // end of if} // end of FromDbValue // <summary> // determines whether the value to be written to the database is null, and perform conversion // </summary> /// <param name = "value"> value to be written to the database </param> /// <returns> if you want to write the value is null, then it is converted to DBNull and returned; otherwise, the system returns </returns> public static object ToDbValue (object value) {if (value = null) {return DBNull. value ;} Else {return value ;} // end of if} // end of ToDbValue // <summary> // return all table names in the database /// </summary> /// <returns> </returns> public static List <string> GetShemaTableName () {using (SqlConnection conn = new SqlConnection (connStr) {conn. open (); DataTable dt = conn. getSchema ("Tables"); DataView dv = new DataView (dt); dv. rowFilter = "table_type = 'table'"; List <string> names = new List <string> (); foreac H (DataRow item in dv. toTable (). rows) {names. add (item ["Table_Name"]. toString (); Console. writeLine (item ["Table_Name"]. toString () ;}; return names ;}} /// <summary> /// obtain the number of records of a table in the database /// </summary> /// <param name = "selstr"> </param>/ // <returns> </returns> public static int GetCount (string selstr) {using (SqlConnection conn = new SqlConnection (connStr) {conn. open (); SqlDataAdapter oleda = new SqlDataAdapter (selstr, conn); DataSet ds = new DataSet (); DataTable dt = new DataTable (); oleda. fill (ds, "11"); dt = ds. tables ["11"]; int MaxValue = dt. rows. count; return MaxValue ;}} public static bool ExecuteScalarS (string SQL, out SqlDataReader sdr, params SqlParameter [] parameters) {using (SqlConnection conn = new SqlConnection (connStr) {conn. open (); using (SqlCommand cmd = conn. createComm And () {cmd. commandText = SQL; cmd. parameters. addRange (parameters); sdr = SqlHelper. executeReader (SQL); return sdr. read ();} // end of using} // end of ExecuteScalar} This section briefly introduces asp.net's connection to Data, how can I Package Multiple Data to realize interaction between server and client information? Next Decomposition

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.