欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net连接Mysql封装类代码 可直接调用

程序员文章站 2024-03-01 17:49:46
微软的visual studio没有自带连接mysql的驱动,要去网上下载一个mysql-connector-net-6.4.3驱动,然后安装就可以使用。下面是我封装好的连...
微软的visual studio没有自带连接mysql的驱动,要去网上下载一个mysql-connector-net-6.4.3驱动,然后安装就可以使用。
下面是我封装好的连接数据库的类,直接调用即可。
复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.data;
using system.data.sqlclient;
using system.configuration;
using mysql.data.mysqlclient;
namespace dal
{
  public class getconnection
    {
        private static mysqlconnection  _connection;
      /// <summary>
      /// 获取数据库连接桥
      /// </summary>
        private static mysqlconnection connection
        {
            get
            {
               //string connectionstring = configurationmanager.appsettings["connectionstring"];
                string connectionstring = "server=localhost;user id=root; password=root; database=system; pooling=false";
                //server=222.222.222.222;port=3306;uid=user;pwd=;database=basename;远程连接的
                //string connectionstring = "data source=202.192.72.22;initial catalog=wwj;persist security info=true;user id=wwj;password=wwj123";
                if (_connection == null)
                {
                    _connection = new mysqlconnection(connectionstring);
                    _connection.open();
                }
                if (_connection.state == connectionstate.closed)
                {
                    _connection.open();
                }
                if (_connection.state == connectionstate.broken)
                {
                    _connection.close();
                    _connection.open();
                }
                return getconnection._connection;
            }
        }
      /// <summary>
      /// 获取表数据
      /// </summary>
      /// <param name="sql"></param>
      /// <returns></returns>
        public static mysqldatareader getdataread(string sql)
        {
            mysqlcommand command = new mysqlcommand(sql, connection);
            mysqldatareader read = command.executereader();
            return read;
        }
        public static int noselect(string sql)
        {
            mysqlcommand command = new mysqlcommand(sql, connection);
            int row = command.executenonquery();
            return row;
        }
        public static datatable getdatatable(string sql)
        {
            mysqlcommand command = new mysqlcommand(sql, connection);
            datatable dt = new datatable();
            mysqldataadapter sda = new mysqldataadapter(command);
            sda.fill(dt);
            return dt;
        }
        /// <summary>
        /// 执行sql语句,返回一行一列。。
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns></returns>
        public static string getscalar(string sql)
        {
            mysqlcommand command = new mysqlcommand(sql, connection);
            return command.executescalar().tostring();
        }
    }
}

比如说你想执行删除的,你可以调用getconnection.noselect("delete from userinfo where id=1");读数据库的某一张表,可以调用getconnection.getdatatable("select * from userinfo");调用都很方便。