C # operations MySQL Database

Source: Internet
Author: User

This article explains C # to the MySQL database for the increase and deletion check operation.

1. Install the database, and note that you want to install Connector NET.

2. Introduce MySql.Data.dll, add the project reference, the file general location is: C:\Program files (x86) \mysql\connector NET xx\assemblies\v xx (determined by the MYSQL installation location).

3. Add a test table to MySQL Workbench, this example creates a database test and creates a user table in the database with username and password two table entries, as follows:

  

  4. For the database to be increased and censored, C # code is as follows:

1 usingSystem;2 usingMySql.Data.MySqlClient;3 4 namespaceMySQL Database operations5 {6     class Program7     {8         Static voidMain (string[] args)9         {Ten             //Database Configuration One             stringConnStr ="Database=test;datasource=127.0.0.1;port=3306;user=root;pwd=root;"; AMysqlconnection conn =Newmysqlconnection (CONNSTR); -  - Conn. Open (); the  -             #regionInquire - //            //querying all entries in the user table - //mysqlcommand cmd = new Mysqlcommand ("SELECT * from User", conn); + // - //mysqldatareader reader = cmd. ExecuteReader (); + // A //            //reading data row by line at //While (reader. Read ()) - //            { - //string username = reader. GetString ("username"); - //string password = reader. GetString ("password"); - //Console.WriteLine (username + ":" + password); - //            } in // - //Reader. Close (); to             #endregion +  -             #regionInsert the //            //Insert a piece of data normally * //string username = "LJ"; string password = "6666"; $ //mysqlcommand cmd = new Mysqlcommand ("INSERT into user set username = '" + Username + "'" + ", password= '" + Password + "'", conn);Panax Notoginseng //cmd. ExecuteNonQuery (); -              the //            //SQL injection, the database is deleted + //string username = "LJ"; string password = "6666"; Delete from user; "; A //mysqlcommand cmd = new Mysqlcommand ("INSERT into user set username = '" + Username + "'" + ", password= '" + Password + "'", conn); the //cmd. ExecuteNonQuery (); +  - //            //prevent injection, do not execute Delete database statement $ //string username = "LJ"; string password = "6666"; Delete from user; "; $ //mysqlcommand cmd = new Mysqlcommand ("INSERT into user set [email protected], password = @pwd", conn);  - //cmd. Parameters.addwithvalue ("UID", username); - //cmd. Parameters.addwithvalue ("pwd", password); the //cmd. ExecuteNonQuery (); -             #endregionWuyi  the             #regionDelete - //            //Delete an entry with an ID of 6 Wu //mysqlcommand cmd = new Mysqlcommand ("Delete from user where id = @id", conn); - //cmd. Parameters.addwithvalue ("id", 6); About // $ //cmd. ExecuteNonQuery (); -             #endregion -  -             #regionUpdate A             //change the entry with ID 7 pwd to lll +Mysqlcommand cmd =NewMysqlcommand ("Update user Set password = @pwd where id = 7", conn); theCmd. Parameters.addwithvalue ("pwd","lll"); -  $ cmd. ExecuteNonQuery (); the             #endregion the  the Conn. Close (); the  - Console.readkey (); in         } the     } the}

C # operations MySQL 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.