(primary test water) MVC uses the ADO + generic operations database

Source: Internet
Author: User

First, create an MVC project,

Then, create a person class to get the "single-individual" data for the table persons of the database

    public class person    {        [Key] public        string Id {get; set;}        public string Name {get; set;}        public string Sex {get; set;}        public int Age {get; set;}    }

 

Furthermore, create a Interface1 interface, in order to implement the curd operation

    public interface interface1<t> where T:class,new ()    {        ienumerable<t> Get ();        void Create (T Item);        void Update (T Item);        void Delete (T Item);    }

 

So, we can create a Northwind class that uses interface Interface1 to get data for multiple person curd databases (classes are made to use the model's {get; Set;}, we just use get here)

public class Northwind    {        private interface1<person> _personoperation = null;        Public interface1<person> Persons        {            get            {                if (this._personoperation = = null)                {                    this._ Personoperation = new Method ();                }                return this._personoperation;}}    }

Of course, with this interface, we can have a direct unified call operation of the database (of course, the interface must be implemented), so we need to implement its curd operation,

Then, create a method class that inherits this interface and implements it.

Reference

Using system.data;using system.data.sqlclient;using System.Web.Configuration;

Using parameterized queries

public class Method:interface1<person> {private String _path = Environment.currentdirectory; private string _connectionstring = webconfigurationmanager.connectionstrings["curd"]. ToString ();//Get the database configuration of our web. config//@ "Server=.;i Nitial catalog=curd;integrated security=true; ";  You can also use this instead of the value of _connectionstring public void Create (person Item) {IDbConnection connection = new            SqlConnection (this._connectionstring);            IDbCommand cmd = new SqlCommand (@ "Insert into Persons (id,name,sex,age) Values (@Id, @Name, @Sex, @Age)"); Cmd.            Connection = Connection; Cmd. Parameters.Add ((item.id = = null)? New SqlParameter ("@Id", DBNull.Value): New SqlParameter (            "@Id", item.id)); Cmd.            Parameters.Add (New SqlParameter ("@Name", Item.name)); Cmd.            Parameters.Add (New SqlParameter ("@Sex", Item.sex)); Cmd.      Parameters.Add (New SqlParameter ("@Age", Item.age));      Connection.            Open (); Cmd.            ExecuteNonQuery (); Connection.        Close (); } public void Delete (person Item) {IDbConnection connection = new SqlConnection (this._connection            String);            IDbCommand cmd = new SqlCommand (@ "Delete from Persons Where [email protected]"); Cmd.            Connection = Connection; Cmd. Parameters.Add ((item.id = = null)? New SqlParameter ("@Id", DBNull.Value): New SqlParameter (            "@Id", item.id)); Cmd.            Parameters.Add (New SqlParameter ("@Name", Item.name)); Cmd.            Parameters.Add (New SqlParameter ("@Sex", Item.sex)); Cmd.            Parameters.Add (New SqlParameter ("@Age", Item.age)); Connection.            Open (); Cmd.            ExecuteNonQuery (); Connection.        Close (); } public ienumerable<person> Get () {IDbConnection connection = new SqlConnection (This._con            nectionstring); IDbCommand cmd = new SqlcoMmand ("SELECT * from Persons"); Cmd.            Connection = Connection; Connection.            Open (); IDataReader reader = cmd. ExecuteReader (CommandBehavior.CloseConnection |            Commandbehavior.singleresult); while (reader. Read ()) {Person person = new person () {Id = reader. GetValue (reader. GetOrdinal ("Id")). ToString (), Sex = reader. GetValue (reader. GetOrdinal ("Sex")). ToString (), Name = reader. GetValue (reader. GetOrdinal ("Name")). ToString (), age = Convert.ToInt32 (reader. GetValue (reader.                GetOrdinal ("Age"))};            yield return person; } connection.                    Close (); ' public void Update ' (person Item) {IDbConnection connection = new SqlConnection (this._connection            String); IDbCommand cmd = new SqlCommand (@ "Update Persons Set [email protected],[email protecteD],[email protected] Where [email protected]); Cmd.            Connection = Connection; Cmd. Parameters.Add ((item.id = = null)? New SqlParameter ("@Id", DBNull.Value): New SqlParameter (            "@Id", item.id)); Cmd.            Parameters.Add (New SqlParameter ("@Name", Item.name)); Cmd.            Parameters.Add (New SqlParameter ("@Sex", Item.sex)); Cmd.            Parameters.Add (New SqlParameter ("@Age", Item.age)); Connection.            Open (); Cmd.            ExecuteNonQuery (); Connection.        Close (); }    }

Add:

Database configuration for Web. config

  <connectionStrings>    <add name= "curd" connectionstring= "Data source=.; Initial catalog=curd;integrated security=true "providername=" System.Data.SqlClient "/> </  Connectionstrings>

Method uses

Northwind db = new Northwind ();            Ienumerable<person> pList = db. Persons.get ();//Gets the person table's data person            PL = new Person {                id= "5",                name= "ddd", age                =,                sex= "male"            } ;            Db. persons.create (PL);//Insert Data
Db. persons.update (PL);//Update data
            Db. Persons.delete (PL);//Delete data

  

(primary test water) MVC uses the ADO + generic operations database

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.