C # (. net) MySQL database link tool class

Source: Internet
Author: User

C # (. net) MySQL database link tool class


Download and install Mysqldrivercs First
http://sourceforge.net/projects/mysqldrivercs/

found under the installation folder MySQLDriver.dll , and then the MySQLDriver.dll Adding references to the project

Application configuration file:

App. Config:

<?xml version= "1.0" encoding= "Utf-8"?><configuration>  <connectionStrings>    <add name= "Server" connectionstring= "localhost" ></add>    <add name= "database" connectionstring= "housing" > </add>    <add name= "Login" connectionstring= "root" ></add>    <add name= "Password" connectionstring= "root" ></add>  </connectionStrings></configuration>



SqlHelper.cs content:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Configuration;using Mysqldrivercs;using System.data;namespace demo{class SqlHelper {private static string server = Configuratio nmanager.connectionstrings["Server"].        ConnectionString; private static string database = configurationmanager.connectionstrings["Database"].        ConnectionString; private static string login = configurationmanager.connectionstrings["Login"].        ConnectionString; private static string password = configurationmanager.connectionstrings["Password"].        ConnectionString;  public static int Executenoquery (String sql,mysqlparameter[] parameters) {using (Mysqlconnection conn = New Mysqlconnection (New mysqlconnectionstring (server, database, login, password). asstring)) {Conn.                              Open ();         Prevent garbled Mysqlcommand commn = new Mysqlcommand ("Set names gb2312", conn);       Commn.                ExecuteNonQuery ();                Connection statements and SQL Mysqlcommand cmd = new Mysqlcommand (SQL, conn); Add the parameter cmd.                Parameters.addrange (Parameters); Returns the execution result of the return CMD.            ExecuteNonQuery ();            }} public static object ExecuteScalar (String sql, mysqlparameter[] parameters) { using (mysqlconnection conn = new Mysqlconnection (new mysqlconnectionstring (server, database, login, password). asstring)) {Conn.                Open ();                Prevent garbled Mysqlcommand commn = new Mysqlcommand ("Set names gb2312", conn); Commn.                ExecuteNonQuery ();                Mysqlcommand cmd = new Mysqlcommand (SQL, conn); Add the parameter cmd.                                Parameters.addrange (Parameters); return CMD.            ExecuteNonQuery (); }}//Less public static DataTable Executereaderex (StringSQL, mysqlparameter[] parameters) {using (mysqlconnection conn = new Mysqlconnection (New Mysqlconnectio Nstring (server, database, login, password). asstring)) {Conn.                Open ();                Prevent garbled Mysqlcommand commn = new Mysqlcommand ("Set names gb2312", conn); Commn.                ExecuteNonQuery ();                Mysqlcommand cmd = new Mysqlcommand (SQL, conn); Add the parameter cmd.                Parameters.addrange (Parameters);                Mysqldataadapter MDA = new Mysqldataadapter (cmd); The data that is queried is present in a DataTable, and a DataTable can be understood as a virtual table, a behavior in a DataTable, a record, a database field datatable dt = new DataTable ()                ; Mda.                  Fill (DT);            return DT; }} public static DataSet ExecuteReaderEx2 (String sql, mysqlparameter[] parameters) {Usin G (Mysqlconnection conn = new Mysqlconnection (new mysqlconnectionstring (server, database, login, password). TheTring)) {Conn.                Open ();                Prevent garbled Mysqlcommand commn = new Mysqlcommand ("Set names gb2312", conn); Commn.                ExecuteNonQuery ();                Mysqlcommand cmd = new Mysqlcommand (SQL, conn); Add the parameter cmd.                Parameters.addrange (Parameters);                Mysqldataadapter MDA = new Mysqldataadapter (cmd);                The data that is queried is present in a DataTable, and a DataTable can be understood as a virtual table, a behavior in a DataTable, a record, a database field DataSet ds = new DataSet (); Mda.                Fill (DS);            return DS; }        }    }}


Examples of Use:


  SQL statement                String sql = "Update tbl_sysuser set [email protected] where [email protected]";                          int number = Sqlhelper.executenoquery (sql, new mysqlparameter[]                 {                    new Mysqlparameter ("@isActived", "yes"),                    new Mysqlparameter ("@id", 2)                });                               Console.WriteLine ("Rows Affected:" + number);


C # (. net) MySQL database link tool class

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.