Use dapper link mysql in C # console app to perform some additions and deletions

Source: Internet
Author: User
Tags bulk insert mysql in

First, start by creating a C # console app

Second, then add the necessary namespaces

Using System;
Using System.Collections.Generic;
Using MySql.Data.MySqlClient;
Using System.Data;
Using Dapper;
Using System.Linq;

Third, first configure the class table that exists in the good one database:

 class   Info { public  int  action_id {get ;        Span style= "COLOR: #0000ff" >set  ;}  public  int  parent_id {get ; set         ;}  public  string  action_code {get ; set         ;}  public  string  Relevance {get ; set     ;} }

Iv. add dapper and MySQL expansion packs

Add Dapper expansion pack

Add MySQL expansion pack

Finally, in the far right management, you can see the extension you just added:

Five. Add Database Link Code

// database connection configuration information;  Public Static string " server = localhost; User Id = Root;password = 123456;database = Ecshop";

Then add the code for the unconditional query:

        /// <summary>        ///Unconditional Inquiry All; /// </summary>        /// <returns></returns>         Public StaticList<info>Query () {using(IDbConnection connection =Newmysqlconnection (CONSTR)) {                returnConnection. Query<info> ("SELECT * from Sdx_admin_action").            ToList (); }        }    

The results of the implementation are as follows:

The following is the source code contains additions and deletions to search:

usingSystem;usingSystem.Collections.Generic;usingMySql.Data.MySqlClient;usingSystem.Data;usingDapper;usingSystem.Linq;namespaceselectall{classInfo { Public intaction_id {Get;Set; }  Public intparent_id{Get;Set; }  Public stringAction_code {Get;Set; }  Public stringrelevance {Get;Set; } }    classProgram {//database connection configuration information;         Public Static stringConstr ="server = localhost; User Id = Root;password = 123456;database = Ecshop"; Static voidMain (string[] args) {            /*Info one = new info ();            one.parent_id = 2;            One.action_code = "small Weak chicken"; One.relevance = "here~";*/            //Insert//Insert (SetInfo (3, "small Weak chicken # third", "Hahahh")); //BULK Insert            /*list<info> infos = new list<info> ();            Infos.add (SetInfo (4, "small weak chicken No. Fourth", "Sdfeff"));            Infos.add (SetInfo (5, "small weak chicken No. Fifth", "sdfsdfdf"));            Infos.add (SetInfo (6, "small weak chicken No. Sixth", "SDFE multidimensional ff")); Insert (infos);*/            //deleted; //Delete (SetInfo (2, "small Weak chicken", "here~")); //Update//Update (SetInfo (2, "small weak chicken No. Second • Change", "hahahh"));List<Info> test =Query (); stringTestInfo ="";  for(inti =0; I < test. Count; i++) {TestInfo= test[i].action_id. ToString () +" , "+ test[i].parent_id +" , "+ Test[i].action_code +" , "+test[i].relevance;            Console.WriteLine (TestInfo);        } console.readline (); }        /// <summary>        ///Unconditional Inquiry All; /// </summary>        /// <returns></returns>         Public StaticList<info>Query () {using(IDbConnection connection =Newmysqlconnection (CONSTR)) {                returnConnection. Query<info> ("SELECT * from Sdx_admin_action").            ToList (); }        }        /// <summary>        ///sets the Info object and returns/// </summary>        /// <param name= "var_parent_id" ></param>        /// <param name= "Var_action_code" ></param>        /// <param name= "var_relevance" ></param>        /// <returns></returns>         Public StaticInfo SetInfo (intVAR_PARENT_ID,stringVar_action_code,stringvar_relevance) {Info tmp=NewInfo (); tmp.parent_id=var_parent_id; Tmp.action_code=Var_action_code; Tmp.relevance=var_relevance; returntmp; }        /// <summary>        ///perform inserts, insert a single message;/// </summary>        /// <returns></returns>         Public Static intInsert (Info varinfo) {using(IDbConnection connection =Newmysqlconnection (CONSTR)) {                returnconnection. Execute ("INSERT INTO sdx_admin_action (parent_id,action_code,relevance) VALUES (@parent_id, @action_code, @relevance)", Varinfo); }        }        /// <summary>        ///bulk execution of inserts; /// </summary>        /// <param name= "Varinfo" ></param>        /// <returns></returns>         Public Static intInsert (list<info>varinfo) {            using(IDbConnection connection =Newmysqlconnection (CONSTR)) {                returnconnection. Execute ("INSERT INTO sdx_admin_action (parent_id,action_code,relevance) VALUES (@parent_id, @action_code, @relevance)", Varinfo); }        }        /// <summary>        ///deletes the line information for the specified condition; /// </summary>        /// <param name= "info" ></param>        /// <returns></returns>         Public Static intDelete (Info info) {using(IDbConnection connection =Newmysqlconnection (CONSTR)) {                returnconnection. Execute ("Delete from sdx_admin_action where [email protected]_code", info); }        }        /// <summary>        ///Update/// </summary>        /// <param name= "info" ></param>        /// <returns></returns>         Public Static intUpdate (Info info) {using(IDbConnection connection =Newmysqlconnection (CONSTR)) {                returnconnection. Execute ("Update sdx_admin_action Set [email protected]_code where [email protected]_id", info); }        }    }}

Reference Document: https://www.jianshu.com/p/c4ca2989d26a

Use dapper link mysql in C # console app to perform some additions and deletions

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.