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

时间:2024-01-14 15:16:16 

微软的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");调用都很方便。

标签:.net,Mysql,封装
0
投稿

猜你喜欢

  • php以post形式发送xml的方法

    2023-11-22 12:40:47
  • 对python读取zip压缩文件里面的csv数据实例详解

    2022-04-14 10:48:57
  • Python全面解读高级特性切片

    2021-06-05 14:23:13
  • Python实现读取文件最后n行的方法

    2023-08-02 10:33:32
  • Python Serial串口基本操作(收发数据)

    2022-04-17 09:54:07
  • springMVC + easyui + $.ajaxFileUpload实现文件上传注意事项

    2023-09-04 09:17:26
  • js数组的基本用法及数组根据下标(数值或字符)移除元素

    2024-04-10 10:40:26
  • 通过字符串导入 Python 模块的方法详解

    2023-10-15 03:00:56
  • 利用box-sizing实现div仿框架

    2009-12-08 15:45:00
  • SQL语句练习实例之六 人事系统中的缺勤(休假)统计

    2011-11-03 17:15:55
  • MySQL创建数据表时设定引擎MyISAM/InnoDB操作

    2024-01-20 18:28:58
  • python打包exe开机自动启动的实例(windows)

    2023-11-08 06:34:00
  • 分享13款非常有用的jQuery插件

    2011-05-16 19:07:00
  • 在pandas中一次性删除dataframe的多个列方法

    2022-08-16 02:50:02
  • 浅谈JavaScript窗体Window.ShowModalDialog使用

    2024-04-23 09:05:39
  • MySQL关于字符串中数字排序的问题分析

    2024-01-21 23:30:18
  • python/golang 删除链表中的元素

    2021-05-05 14:08:07
  • mysql数据库sql优化原则(经验总结)

    2024-01-22 17:55:12
  • 一文读懂python Scrapy爬虫框架

    2021-01-31 10:08:31
  • 修改并编译golang源码的操作步骤

    2024-05-02 16:26:23
  • asp之家 网络编程 m.aspxhome.com