zoukankan      html  css  js  c++  java
  • C#-操作Mysql

    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;
                }
            }
    
    
    
    
    
    
    
    
        }
    }
  • 相关阅读:
    基于Furion的.NET5 WebApi开发框架
    由react的todolist想到的
    react第三节-基础概念梳理
    uniapp引入字体图标与uniapp传入事件对象与自定义参数
    (react+tsx)函数式组件传参问题
    关于git正确clone指定分支
    关于‘react-app-rewried 不是内部或外部命令’的深度解析
    flex下的多行对齐与预处理器中使用nth-child选择器
    webpack(2)--webapck自身的配置
    webpack解析(1)
  • 原文地址:https://www.cnblogs.com/JinweiChang/p/11713796.html
Copyright © 2011-2022 走看看