c# 数据库 使用

本文介绍了如何在C#中使用NUGET的MySql.Data.MySqlClient插件,实现数据库连接池管理,包括连接的创建、获取、关闭以及定时任务的调度。通过实例演示了如何处理线程安全和异常处理,确保高效数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c# MYSQL数据库 基本使用

1、NUGET MYSQL插件,要不用不了

2、using MySql.Data.MySqlClient;

写连接池(抄的)

  public class ConnectionPool
        {
            private static ConnectionPool cpool = null;//池管理对象
            private static Object objlock = typeof(ConnectionPool);//池管理对象实例
            private int size = 1;//池中连接数
            private int useCount = 0;//已经使用的连接数
            private ArrayList pool = null;//连接保存的集合
            private String ConnectionStr = "";//连接字符串

            public ConnectionPool()
            {
                //数据库连接字符串
                ConnectionStr = DataServerConnString;    //这是连接语句
                //创建可用连接的集合
                pool = new ArrayList();
            }

            #region 创建获取连接池对象
            public static ConnectionPool getPool()
            {
                lock (objlock)
                {
                    if (cpool == null)
                    {
                        cpool = new ConnectionPool();
                    }
                    return cpool;
                }
            }
            #endregion

            #region 获取池中的连接
            public MySqlConnection getConnection()
            {
                lock (pool)
                {
                    MySqlConnection tmp = null;
                    //可用连接数量大于0
                    if (pool.Count > 0)
                    {
                        //取第一个可用连接
                        tmp = (MySqlConnection)pool[0];
                        //在可用连接中移除此链接
                        pool.RemoveAt(0);
                        //不成功
                        if (!isUserful(tmp))
                        {
                            //可用的连接数据已去掉一个
                            useCount--;
                            tmp = getConnection();
                        }
                    }
                    else
                    {
                        //可使用的连接小于连接数量
                        if (useCount <= size)
                        {
                            try
                            {
                                //创建连接
                                tmp = CreateConnection(tmp);
                            }
                            catch (Exception e)
                            {
                            }
                        }
                    }
                    //连接为null
                    if (tmp == null)
                    {
                        //达到最大连接数递归调用获取连接否则创建新连接
                        if (useCount <= size)
                        {
                            tmp = getConnection();
                        }
                        else
                        {
                            tmp = CreateConnection(tmp);
                        }
                    }
                    return tmp;
                }
            }
            #endregion

            #region 创建连接
            private MySqlConnection CreateConnection(MySqlConnection tmp)
            {
                //创建连接
                MySqlConnection conn = new MySqlConnection(ConnectionStr);
                conn.Open();
                //可用的连接数加上一个
                useCount++;
                tmp = conn;
                return tmp;
            }
            #endregion

            #region 关闭连接,加连接回到池中
            public void closeConnection(MySqlConnection con)
            {
                lock (pool)
                {
                    if (con != null)
                    {
                        //将连接添加在连接池中
                        pool.Add(con);
                    }
                }
            }
            #endregion

            #region 目的保证所创连接成功,测试池中连接
            private bool isUserful(MySqlConnection con)
            {
                //主要用于不同用户
                bool result = true;
                if (con != null)
                {
                    string sql = "select 1";//随便执行对数据库操作
                    MySqlCommand cmd = new MySqlCommand(sql, con);
                    try
                    {
                        cmd.ExecuteScalar().ToString();
                    }
                    catch
                    {
                        result = false;
                    }

                }
                return result;
            }
            #endregion

        }

开线程

ThreadStart ReaddataFS1 = new ThreadStart(ReaddataFS);
Thread threadFS = new Thread(ReaddataFS1);
threadFS.Start();


//死了怎么办
catch  里面  NEW 自己

加一个定时器,按时间读取 

System.Timers.Timer timer = new System.Timers.Timer();//开一个内部计时器线程
            timer.Interval = 60000; //60s一次
            timer.Elapsed += new System.Timers.ElapsedEventHandler(ReaddataFSIn);
            timer.Enabled = true;
            void ReaddataFSIn(object sender, System.Timers.ElapsedEventArgs e)
            {
               ReadFsdata();  

            }

再写

public static void ReadFsdata()
        {

            MySqlConnection connFS = null;
            connFS = ConnectionPool.getPool().getConnection();
            try
            {
                //数据操作
                string sqlstring = "";
                MySqlCommand cmd = new MySqlCommand(sqlstring, connFS);
                MySqlDataReader drFS1 = cmd.ExecuteReader();
                if (drFS1.HasRows)
                {
                    dtFS.Clear();
                    dtFS.Load(drFS1);
                }
                else
                {

                }
                drFS1.Close();
                }

            catch (Exception ex)
            {

                Console.WriteLine(ex);
            }

            ConnectionPool.getPool().closeConnection(connFS);
           

        }

使用

  this.label7.Text = dtFS.Rows[0][0].ToString() ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值