using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using MySql.Data.MySqlClient;

namespace mysql
{
    class MysqlConnection
    {
        MySqlConnection mysqlConnection;
        DataSet dataSet;
        string IP = "192.168.0.101";
        string UserName = "root";
        string Password = "123456";
        string Database = "ga";
        /// <summary>
        /// 建立mysql连接
        /// </summary>
        public MysqlConnection()
        {
            try
            {
                mysqlConnection = new MySqlConnection("datasource=" + IP + ";username=" + UserName + ";password=" + Password + ";database=" + Database + ";charset=utf8");
            }
            catch (MySqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

public MysqlConnection(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;
        }
    }//end class
}

C#操作Mysql类的更多相关文章

  1. java分享第十七天-03(封装操作mysql类)

     JAVA操作mysql所需jar包:mysql-connector-java.jar代码: import java.sql.*; import java.util.ArrayList; import ...

  2. PHP操作mysql类

    <?php class Mysql{ //数据库连接句柄 private $link; //返回结果集 private $result; //返回查询数据 private $data; //执行 ...

  3. LightMysql:为方便操作MySQL而封装的Python类

    原文链接:http://www.danfengcao.info/python/2015/12/26/lightweight-python-mysql-class.html mysqldb是Python ...

  4. .NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper

    .NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper 參考演示样例代码,例如以下所看到的: /// <summary> /// MySql ...

  5. C#操作MySQL的类

    C#操作MySQL的类 public class MySqlService { private static log4net.ILog logger = log4net.LogManager.GetL ...

  6. Jave工具——servlet+jsp编程中mysql数据库连接及操作通用工具类

    该工具类是在JavaWeb中连接mysql所用到的通用工具类 该类用于Java+Servlet的编程中,方便数据库的操作,连接,获取其列表值.下面是这个数据库操作类的通用方法,基本上能够用于类里面只含 ...

  7. C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]

    原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using Sy ...

  8. C#---数据库访问通用类、Access数据库操作类、mysql类 .

    //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System. ...

  9. Python3操作MySQL基于PyMySQL封装的类

    Python3操作MySQL基于PyMySQL封装的类   在未使用操作数据库的框架开发项目的时候,我们需要自己处理数据库连接问题,今天在做一个Python的演示项目,写一个操作MySQL数据库的类, ...

随机推荐

  1. dataGridView 设置

    //窗体加载事件 //内容居中 dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCe ...

  2. 怎样批量删除PDF文件中的注释

    日常我们在阅读一些PDF文章时候,我们会发现有些PDF文章带有非常多的注释,显得非常不美观,影响了阅读体验.那么PDF文章里的批注应该怎么进行删除呢?怎样批量删除PDF文件中的注释?   操作教程: ...

  3. mysql 是如何保证在高并发的情况下autoincrement关键字修饰的列不会出现重复

    转载自 https://juejin.im/book/5bffcbc9f265da614b11b731/section/5c42cf94e51d45524861122d#heading-8 mysql ...

  4. Jsp页面中常用的EL表达式

    首先引入标签 <%@ page language="java" contentType="text/html; charset=utf-8"  pageE ...

  5. 死磕itchat源码--config.py

    itchat的配置文件,源码: import os, platform # 版本及微信的url,二维码等 VERSION = '1.3.10' BASE_URL = 'https://login.we ...

  6. PAT_A1030#Travel Plan

    Source: PAT A1030 Travel Plan (30 分) Description: A traveler's map gives the distances between citie ...

  7. spotlight on mysql 监控

    . 安装 下载地址:https://pan.baidu.com/s/1qYi3lec 官网地址——https://www.quest.com/common/registration.aspx?requ ...

  8. MySQL中的注释符号的使用

    前言 在学习MySQL的过程中,因为目前接触的语法简单,所以实在没有想到过加入注释.在写博客使用Markdown时,突然想用注释语句,所以便百度了一下,引用了这一篇转载博客. MySQL中的注释符号有 ...

  9. 自动装箱拆箱(Autoboxing,Unboxing)

    自动装箱和拆箱 https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html 1.5才有(Autoboxing,Unboxing) ...

  10. hdu 4858 容器的简单模拟

    我用临接表模拟容器超时 #include<stdio.h> #include<string.h> #include<vector> using namespace ...