C#操作Mysql类
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类的更多相关文章
- java分享第十七天-03(封装操作mysql类)
JAVA操作mysql所需jar包:mysql-connector-java.jar代码: import java.sql.*; import java.util.ArrayList; import ...
- PHP操作mysql类
<?php class Mysql{ //数据库连接句柄 private $link; //返回结果集 private $result; //返回查询数据 private $data; //执行 ...
- LightMysql:为方便操作MySQL而封装的Python类
原文链接:http://www.danfengcao.info/python/2015/12/26/lightweight-python-mysql-class.html mysqldb是Python ...
- .NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper
.NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper 參考演示样例代码,例如以下所看到的: /// <summary> /// MySql ...
- C#操作MySQL的类
C#操作MySQL的类 public class MySqlService { private static log4net.ILog logger = log4net.LogManager.GetL ...
- Jave工具——servlet+jsp编程中mysql数据库连接及操作通用工具类
该工具类是在JavaWeb中连接mysql所用到的通用工具类 该类用于Java+Servlet的编程中,方便数据库的操作,连接,获取其列表值.下面是这个数据库操作类的通用方法,基本上能够用于类里面只含 ...
- C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]
原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using Sy ...
- C#---数据库访问通用类、Access数据库操作类、mysql类 .
//C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System. ...
- Python3操作MySQL基于PyMySQL封装的类
Python3操作MySQL基于PyMySQL封装的类 在未使用操作数据库的框架开发项目的时候,我们需要自己处理数据库连接问题,今天在做一个Python的演示项目,写一个操作MySQL数据库的类, ...
随机推荐
- jQuery顺序加载图片(初版)
浏览器加载图片区别: IE:同时加载与渲染 其他:加载完之后再渲染 根据这个差异用jQuery做个实例:按顺序加载一组图片,加载完成后提示. <!DOCTYPE html> <htm ...
- Centos6.6 编译安装nginx
一.基本环境 nginx 1.9版以后增加了一些新的特性,支持tcp负载均衡,不过这次还是用1.8.0,这里面有个memcached的代理模块,有时间再测试下 1.centos6.6 2.nginx1 ...
- (转)OpenLayers3基础教程——OL3 介绍interaction
http://blog.csdn.net/gisshixisheng/article/details/46808647 概述: 本节主要讲述OL3的交互操作interaction,重点介绍draw,s ...
- Java_Reflect反射
类是对象,类是java.lang.Class类的实例对象.There is a class named Class class Foo{} public class ClassDemo{ public ...
- Redis 之list链表结构及命令详解
1.lpush key value 从左放一个值 2.rpush key value 从右放一个值 3.lrange key start stop 获取链表数据(start ...
- vue使用插槽分发内容slot的用法
将父组件的内容放到子组件指定的位置叫做内容分发 //在父组件里使用子组件 <son-tmp> <div>我是文字,我需要放到son-tmp组件里面制定的位置</div&g ...
- drf05 路由Routers
对于视图集ViewSet,我们除了可以自己手动指明请求方式与动作action之间的对应关系外,还可以使用Routers来帮助我们快速实现路由信息. REST framework提供了两个router ...
- python从TXT创建PDF文件——reportlab
使用reportlab创建PDF文件电子书一般都是txt格式的,某些电子阅读器不能读取txt的文档,如DPT-RP1.因此本文从使用python实现txt到pdf的转换,并且支持生成目录,目录能够生成 ...
- 504 Gateway Timeout 异常
生产销售系统出现 504 Gateway Timeout 异常,其实就是服务器响应太慢导致nginx带来超时,先不说服务端慢的优化问题:只是单纯的解决504.到网上发现了一篇文章fix it Add ...
- PAT_A1125#Chain the Ropes
Source: PAT A1125 Chain the Ropes (25 分) Description: Given some segments of rope, you are supposed ...