码迷,mamicode.com
首页 > 数据库 > 详细

C#(.net) MySql数据库链接工具类

时间:2015-02-09 23:08:10      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:c#.net mysql数据库链接工具类

C#(.net) MySql数据库链接工具类


先下载和安装MySQLDriverCS 
http://sourceforge.net/projects/mysqldrivercs/

在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中

应用程序配置文件:

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内容:

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 = ConfigurationManager.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();

              
                //防止乱码
                MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
                commn.ExecuteNonQuery();
                //连接语句和SQL
                MySQLCommand cmd = new MySQLCommand(sql, conn);
                //添加参数
                cmd.Parameters.AddRange( parameters);
                //返回执行结果
                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();
                //防止乱码
                MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
                commn.ExecuteNonQuery();

                MySQLCommand cmd = new MySQLCommand(sql, conn);
                //添加参数
                cmd.Parameters.AddRange(parameters);
                
                return cmd.ExecuteNonQuery();
            }
        
        }

        //较少的时候
        public static DataTable ExecuteReaderEx(String sql, MySQLParameter[] parameters)
        {

            using (MySQLConnection conn = new MySQLConnection(new MySQLConnectionString(server, database, login, password).AsString))
            {

                conn.Open();
                //防止乱码
                MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
                commn.ExecuteNonQuery();

                MySQLCommand cmd = new MySQLCommand(sql, conn);
                //添加参数
                cmd.Parameters.AddRange(parameters);

                MySQLDataAdapter mda = new MySQLDataAdapter(cmd);

                //查询出的数据是存在DataTable中的,DataTable可以理解成为一个虚拟的表,DataTable中的一行为一条记录,一列为一个数据库字段  


                DataTable dt = new DataTable();
                mda.Fill(dt);  

                return dt;
            }

        }
        public static DataSet ExecuteReaderEx2(String sql, MySQLParameter[] parameters)
        {


            using (MySQLConnection conn = new MySQLConnection(new MySQLConnectionString(server, database, login, password).AsString))
            {

                conn.Open();
                //防止乱码
                MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
                commn.ExecuteNonQuery();

                MySQLCommand cmd = new MySQLCommand(sql, conn);
                //添加参数
                cmd.Parameters.AddRange(parameters);

                MySQLDataAdapter mda = new MySQLDataAdapter(cmd);

                //查询出的数据是存在DataTable中的,DataTable可以理解成为一个虚拟的表,DataTable中的一行为一条记录,一列为一个数据库字段  


                DataSet ds = new DataSet();
                mda.Fill(ds);
                return ds;
            }

        }




    }
}


使用示例:


  //sql语句
                string sql = "update tbl_sysuser set isActived=@isActived where id=@id";
          

                int number = SqlHelper.ExecuteNoQuery(sql, new MySQLParameter[] 
                {
                    new MySQLParameter("@isActived", "YES"),
                    new MySQLParameter("@id", 2)
                });
               

                Console.WriteLine("受影响的行数:" + number);


C#(.net) MySql数据库链接工具类

标签:c#.net mysql数据库链接工具类

原文地址:http://blog.csdn.net/w605283073/article/details/43678149

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!