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

C#-操作Mysql

时间:2019-10-21 16:03:17      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:ima   mysq   nbsp   mit   技术   catalog   als   span   try   

 

Nuget添加库

技术图片

 

 

 

 

公共类

 

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mysql_nf
{
    class Mysql_Helper
    {



        private MySqlConnection myConnection;
        private MySqlCommand myCommand;
        private MySqlDataAdapter myAdapter;
        private MySqlTransaction myTransaction;
        string str_Con = "data source=172.20.168.210;user id=root;pwd=QSMC+12345;initial catalog=jinwei;allow zero datetime=true";
        //建立DB连接
        public  Mysql_Helper()
        {

            string contString = str_Con;
            try
            {
                myConnection = new MySqlConnection();
                myConnection.ConnectionString = contString;
                myConnection.Open();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                MessageBox.Show("连接失败!");
            }
            finally
            {
                myConnection.Close();
            }
        }
        //数据查询操作
        public DataTable executeQuery(String sql)
        {
            DataTable myTable;
            try
            {
                myCommand = myConnection.CreateCommand();
                myCommand.CommandText = sql;
                myAdapter = new MySqlDataAdapter(myCommand);
                DataSet mySet = new DataSet();
                myAdapter.Fill(mySet, "selectDa");
                myTable = mySet.Tables["selectDa"];
                return myTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                myConnection.Close();
            }
        }

        //数据插入,删除,更新操作
        public Boolean executeUpdate(String sql)
        {
            try
            {
                myCommand = myConnection.CreateCommand();
                myCommand.CommandText = sql;
                myCommand.ExecuteNonQuery();
                if (myTransaction == null)
                {
                    myConnection.Close();
                    myConnection = null;
                }
                return true;
            }
            catch (Exception ex)
            {
                if (myTransaction != null)
                {
                    myTransaction.Rollback();
                    myTransaction = null;
                    MessageBox.Show("数据发生错误,正在启用事务回滚!");
                }
                else if (myConnection == null)
                {
                    MessageBox.Show("请启用事务!");
                }
                else
                {
                    MessageBox.Show("发生错误!");
                }
                Console.WriteLine(ex);
                return false;
            }
            finally
            {
                myConnection.Close();
            }
        }
        //创建事务
        public void createTransaction()
        {
            try
            {
                myTransaction = myConnection.BeginTransaction();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                myConnection.Close();
            }
        }
        //提交事务
        public void commitTransaction()
        {
            try
            {
                if (myTransaction != null) myTransaction.Commit();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                myConnection.Close();
                myConnection = null;
            }
        }








    }
}

 

C#-操作Mysql

标签:ima   mysq   nbsp   mit   技术   catalog   als   span   try   

原文地址:https://www.cnblogs.com/JinweiChang/p/11713796.html

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