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数据库的类, ...
随机推荐
- 【sqli-labs】 less25 GET- Error based -All you OR&AND belong to us -string single quote(GET型基于错误的去除了or和and的单引号注入)
加单引号 order by一下 http://localhost/sqli-labs-master/Less-25/?id=1' order by 1%23 order by 变成了der by 下面 ...
- 【转载】使用IntelliJ IDEA提示找不到struts-default文件
创建strus,参考文如下: https://blog.csdn.net/u010358168/article/details/79769137 使用IntelliJ IDEA创建struts2工程时 ...
- dup、文件锁、库函数、函数调用(day07)
一.lseek()重新定位文件的读写位置. #include <sys/types.h> #include <unistd.h> off_t lseek(int fd, off ...
- Linux思维导图之rpm、yum、编译
yum安装失败: 1.yum client 路径指向不正确:2.yum server 缓存未清理(yum clean all;yum makecache):3.网络不连通
- 09.正则表达式re-1.正则表达式
1.正则表达式概述 正则表达式(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计算机科学的一个概念. 正则表达式使用单个字符串来描述.匹配一系列匹配某 ...
- JUnit单元测试实践:测试工具类和方法(EmptyUtils)
以前的时候(读大学时),我认为写单元测试太费事了.现在,我改变看法了. 工作中,为了提高Web开发的质量和效率,近期又为了保证自己的工具类等一系列可复用组件的质量,我煞费苦心地开始认真学习和撰写单元测 ...
- cnblogs正式启用
额,因为最近发现CSDN越来越过分了...现在连数学公式都显示错字体了--于是决定把博客搬至cnblogs. Markdown 测试 \(\frac{-b\pm \sqrt{b^2-4ac}}{2a} ...
- JavaSE 学习笔记之接 口(六)
接 口: 1:是用关键字interface定义的. 2:接口中包含的成员,最常见的有全局常量.抽象方法. 注意:接口中的成员都有固定的修饰符. 成员变量:public static final ...
- 洛谷 P1131 BZOJ 1060 [ZJOI2007]时态同步
题目描述 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点 ...
- 楼控-西门子insight BBMD设置
BBMD设置的目的就是让两个不同网段的设备可以同时在一个系统中访问的操作. 比如你有两个bacnet的网络,但是一个是192.168.0.1-192.168.0.255的网段,另一个是10.0.0.1 ...