Encapsulation of the data access layer in. NET __.net

Source: Internet
Author: User
Tags int size connectionstrings

The data access layer encapsulated in the. NET project of the year, personal sense of use is more convenient for everyone to refer
to the DAO layer access can be called as follows:

public class persondao{Sqldataprovider SQL;
    Public Persondao () {sql = new Sqldataprovider (); //insert public void Addperson (person person) {SQL.
        Addsqlcomm ("INSERT into person (username,age)" + "Select @userName, @age"); Sql.
        AddParameters ("@userName", Person.getusername ()); Sql.
        AddParameters ("@age", Person.getage ()); Sql.
    ExecuteNonQuery (); //update public void Modperson (person person) {SQL.  Addsqlcomm ("UPDATE person" + "SET UserName = @userName and age = @age" + "WHERE Id
        = @id "); Sql.
        AddParameters ("@userName", Person.getusername ()); Sql.
        AddParameters ("@age", Person.getage ()); Sql.
        AddParameters ("@id", Person.getid ()); Sql.
    ExecuteNonQuery (); //delete public void Rmperson (person person) {SQL. Addsqlcomm ("DELETE from person" + "wheRE Id = @id "); Sql.
        AddParameters ("@id", Person.getid ()); Sql.
    ExecuteNonQuery (); }//select public DataTable Getpersonbyid (int id) {SQL. Addsqlcomm (' Select Id, UserName, age ' + ' from person ' with (NOLOCK) ' + ' W
            Here id= @id "); Sql.   
            AddParameters ("@id", id); Return SQL.
    Executedatatable (); The public DataSet getpeople () {SQL.
            Addsqlcomm ("Select Id, UserName, age" + "from person with (NOLOCK)"); Return SQL.
    ExecuteDataset (); //sp public void init (int id) {SQL.
            Addsqlcomm ("Usp_init_person");
            Sql.commandtype = CommandType.StoredProcedure; Sql.
            AddParameters ("@id", id); Sql.
    ExecuteNonQuery (); }
}

It is convenient to call the data access layer as long as you pass in a simple SQL statement or stored procedure. Now, let's take a look at the code implementation for this data access layer: 1. Look at the file schema first

Let's take a visit to the SQL Server database for an example of 2.IDataProvider

Using System;
Using System.Collections.Generic;
Using System.Linq;

Using System.Text;
        Namespace Dataprovider {interface Idataprovider {void AddParameters (string parname, Guid value);
        void AddParameters (String parname, Long value);
        void AddParameters (String parname, String value);
        void AddParameters (String parname, String value, dataprovider.stringfamily datetype);
        void AddParameters (String parname, String value, dataprovider.stringfamily datetype, int size);
        void AddParameters (string parname, float value);
        void AddParameters (String parname, decimal value);
        void AddParameters (String parname, DateTime value, dataprovider.datefamily datetype);
        void AddParameters (string parname, int value);
        void AddParameters (String parname, object value);
        void AddParameters (String parname, byte[] value, dataprovider.bytearrayfamily datetype);
        void AddParameters (string parname, bool value); VoID addparameters (String parname, short value);
        void AddParameters (String parname, byte value);
        System.Data.CommandType CommandType {get; set;}
        String ConnectionString {get;}
        System.Data.DataSet ExecuteDataset ();
        System.Data.DataTable executedatatable ();
        void ExecuteReader (ReadData readdata);
        int ExecuteNonQuery ();
        Object ExecuteScalar ();
    String SQL {get; set;}
public delegate void ReadData (System.Data.IDataReader datareadre); }
3.SqlDataProvider
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Data;
Using System.Data.SqlClient;


Using System.Configuration; namespace Dataprovider.sqldataprovider {///<summary>///SQL data Provider Implementation///</summary> public C

        Lass Sqldataprovider:idataprovider {//Database connection strings string connstr;
        private static System.Data.SqlClient.SqlConnection Conn;


        Private System.Data.SqlClient.SqlCommand cmd; Public Sqldataprovider () {connstr = configurationmanager.connectionstrings["Sqlconn"].
        ToString (); } public Sqldataprovider (string userdefinedconnstr) {connstr = Configurationmanager.connecti ONSTRINGS[USERDEFINEDCONNSTR]. 
        ToString (); Public Sqldataprovider (String connstr, String sql) {conn = new System.Data.SqlClient.SqlCo
            Nnection (CONNSTR); cmd = new System.Data.SqlClient.SqlCommand ();
            Cmd.
            Connection = conn;
            Cmd.commandtext = SQL;
        cmd.commandtimeout = 0; public void Setconn (String conn) {connstr = Configurationmanager.connectionstrings[conn]. 
        ToString (); ///<summary>///When the factory produces a good data access object, use this method to input SQL statements to the object///</summary>///<para M name= "SQL" ></param> public void Addsqlcomm (String sql) {conn = new SYSTEM.DATA.SQL
            Client.sqlconnection (CONNSTR);
            cmd = new System.Data.SqlClient.SqlCommand (); Cmd.
            Connection = conn;
            Cmd.commandtext = SQL;
        cmd.commandtimeout = 0;
            ///<summary>///SQL command to execute///</summary> public string sql {
            set {Cmd.commandtext = value;
            get {return cmd.commandtext;
       } ///<summary>///Current connection string///</summary> public string ConnectionString {get {return conn.
            ConnectionString; }///<summary>///set the type of command///</summary> public System.Data.Comma Ndtype CommandType {set {cmd.
            CommandType = value; get {return cmd.
            CommandType; }///<summary>///Add a Variant type data///</summary>///<param na Me= "Parname" ></param>///<param name= "value" ></param> public void AddParameters (stri Ng Parname, Object value) {cmd. Parameters.Add (Parname, System.Data.SqlDbType.Variant).
        Value = value;
     ///<summary>///Add a bit type data///</summary>   <param name= "Parname" ></param>///<param name= "value" ></param> public void AddParameters (string parname, bool value) {cmd. Parameters.Add (Parname, System.Data.SqlDbType.Bit).
        Value = value; ///<summary>///Add a tinyint type data///</summary>///<param name= "Parnam E "></param>///<param name=" value "></param> public void AddParameters (string parname , byte value) {cmd. Parameters.Add (Parname, System.Data.SqlDbType.TinyInt).
        Value = value; ///<summary>///Add a smallint type data///</summary>///<param name= "Parna Me ></param>///<param name= "value" ></param> public void AddParameters (string Parnam E, short value) {cmd. Parameters.Add (Parname, System.Data.SqlDbType.SmallInt).
        Value = value;

     }   <summary>///Add an int type data///</summary>///<param name= "Parname" ></p 
        aram>///<param name= "value" ></param> public void AddParameters (string parname, int value) {cmd. Parameters.Add (Parname, System.Data.SqlDbType.Int).
        Value = value; ///<summary>///Add an int type data output parameter///</summary>///<param name= "Parnam E "></param>///<param name=" value "></param> public void Addoutputparameters (string p arname) {cmd. Parameters.Add (Parname, System.Data.SqlDbType.Int).
        Direction = ParameterDirection.Output; ///<summary>///Add a bigint type data///</summary>///<param name= "Parname  "></param>///<param name=" value "></param> public void AddParameters (string parname,
            Long value) {Cmd. Parameters.Add (Parname, System.Data.SqlDbType.BigInt).
        Value = value; ///<summary>///Add a byte array family type data///</summary>///<param name= "Parname" ></param>///<param name= "value" ></param>///<param name= "Datetype" ></para m> public void AddParameters (String parname, byte[] value, bytearrayfamily datetype) {cmd. Parameters.Add (Parname, Datatypeadapter.convertsqldbtype (Datetype)).
        Value = value; ///<summary>///Adds a character type data, the default is nvarchar, and the length is value. Length///</summary>///<param name= "Parname" ></param>///<param name= "Val UE ' ></param> public void AddParameters (string parname, String value) {AddParameters ( Parname, value, Stringfamily.nvarchar, value.
        Length);
      }///<summary>//////</summary>  <param name= "Parname" ></param>///<param name= "value" ></param>///<para
            M name= "Length" ></param> public void AddParameters (string parname, string value, int size) {
        AddParameters (Parname, value, Stringfamily.nvarchar, size); ///<summary>///Add a character family type data///</summary>///<param name= "Parname" &G t;</param>///<param name= "value" ></param>///<param name= "Datetype" ></param&
        Gt  <param name= "Length" ></param> public void AddParameters (string parname, String value, stringfamily Datetype) {addparameters (parname, value, Datetype, value).
        Length); ///<summary>///Add a character family type data///</summary>///<param name= "Parname" &G t;</param>///<param name= "value" ></param>///&LT;param name= "Datetype" ></param>///<param name= "size" ></param> public void AddParam Eters (String parname, String value, stringfamily datetype, int size) {cmd. Parameters.Add (Parname, Datatypeadapter.convertsqldbtype (datetype), size).
        Value = value; ///<summary>///Add a date family type data///</summary>///<param name= "Parname" &G t;</param>///<param name= "value" ></param>///<param name= "Datetype" ></param&
        Gt public void AddParameters (string parname, DateTime value, datefamily datetype) {cmd. Parameters.Add (Parname, Datatypeadapter.convertsqldbtype (Datetype)).
        Value = value; ///<summary>///Add a decimal type data///</summary>///<param name= "Parnam E "></param>///<param name=" value "></param> public void AddParameters (string Parname, decimal value) {cmd. Parameters.Add (Parname, System.Data.SqlDbType.Decimal).
        Value = value;

         }

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.