zoukankan      html  css  js  c++  java
  • C#操作MySQL类

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

    namespace WindowsFormsApp1
    {
        public class MySqlHelper
        {
            
            MySqlConnection mysqlConnection;
            DataSet dataSet;
            string IP = "";       //连接名
            string UserName = ""; //用户名
            string Password = ""; //密码
            string Database = "";//数据库
            /// <summary>
            /// 建立mysql连接
            /// </summary>
            public MySqlHelper()
            {
                try
                {
                    mysqlConnection = new MySqlConnection("datasource=" + IP + ";username=" + UserName + ";password=" + Password + ";database=" + Database + ";charset=utf8");
                }
                catch (MySqlException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            public MySqlHelper(string IP, string UserName, string Password, string Database)
            {
                try
                {
                    string connectionString = "datasource=" + IP + ";username=" + UserName + ";password=" + Password + ";database=" + Database + ";charset=gb2312";
                    mysqlConnection = new MySqlConnection(connectionString);
                }
                catch (MySqlException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            public string MysqlInfo()
            {
                string mysqlInfo = null;
                try
                {
                    mysqlConnection.Open();
                    mysqlInfo += "Connection Opened." + Environment.NewLine;
                    mysqlInfo += "Connection String:" + mysqlConnection.ConnectionString.ToString() + Environment.NewLine;
                    mysqlInfo += "Database:" + mysqlConnection.Database.ToString() + Environment.NewLine;
                    mysqlInfo += "Connection ServerVersion:" + mysqlConnection.ServerVersion.ToString() + Environment.NewLine;
                    mysqlInfo += "Connection State:" + mysqlConnection.State.ToString() + Environment.NewLine;
                }
                catch (MySqlException ex)
                {
                    Console.WriteLine("MySqlException Error:" + ex.ToString());
                }
                finally
                {
                    mysqlConnection.Close();
                }
                return mysqlInfo;
            }
            /// <summary>
            /// 执行sql语句无返回结果
            /// </summary>
            public int MysqlCommand(string MysqlCommand)
            {
                try
                {
                    mysqlConnection.Open();
                    Console.WriteLine("MysqlConnection Opened.");
                    MySqlCommand mysqlCommand = new MySqlCommand(MysqlCommand, mysqlConnection);
                    return mysqlCommand.ExecuteNonQuery();
                }
                catch (MySqlException ex)
                {
                    Console.WriteLine("MySqlException Error:" + ex.ToString());
                    if (Regex.IsMatch(ex.ToString(), ""))
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                finally
                {
                    mysqlConnection.Close();
                }
                return -1;
            }

            /// <summary>
            /// 执行select 语句返回执行结果
            /// </summary>
            public DataView MysqlDataAdapter(string table)
            {
                DataView dataView = new DataView();
                try
                {
                    mysqlConnection.Open();
                    MySqlDataAdapter mysqlDataAdapter = new MySqlDataAdapter("Select * from " + table, mysqlConnection);
                    dataSet = new DataSet();
                    mysqlDataAdapter.Fill(dataSet, table);
                    dataView = dataSet.Tables[table].DefaultView;
                }
                catch (MySqlException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    mysqlConnection.Close();
                }
                return dataView;
            }
            /// <summary>
            /// 统计记录个数 参数:select count(*) from isns_users
            /// </summary>
            public long MysqlCountRow(string sql)
            {
                DataView dataView = new DataView();
                try
                {
                    mysqlConnection.Open();

                    MySqlCommand mycm = new MySqlCommand(sql, mysqlConnection);
                    // MySqlDataReader msdr = mycm.ExecuteReader();
                    long recordCount = (long)mycm.ExecuteScalar();
                    return recordCount;
                }
                catch (MySqlException)
                {
                    return -1;
                    // Console.WriteLine(ex.Message);
                }
                finally
                {
                    mysqlConnection.Close();
                }
                // return 0;
            }
        }
    }

  • 相关阅读:
    《哈佛大学公开课:公正该如何做是好?》学习笔记3
    iPhone客户端开发笔记(三)
    iPhone客户端开发笔记(一)
    今天讨论了下本地化服务信息应用
    云游第一天感受
    昨晚调试一段PHP程序时遇到的三个问题
    iPhone客户端开发笔记(四)
    《哈佛大学公开课:公正该如何做是好?》学习笔记2
    Oracle10g数据库归档与非归档模式下的备份与恢复
    javascript 实现页面间传值
  • 原文地址:https://www.cnblogs.com/ganzhihui/p/10129731.html
Copyright © 2011-2022 走看看