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数据库的类, ...
随机推荐
- C#异步Async、Task、Await
参考http://www.cnblogs.com/jesse2013/p/async-and-await.html 事例: static void Main(string[] args) { ; i ...
- 完全掌握vuex
公司项目中大量的使用了vue,感觉对vue知识的掌握也越来越熟练了,录制视频教程也让我受益匪浅,自己成长的同时,我更希望帮助其他前端小伙伴一起成长.这篇文章我们主要讲解vuex. vuex是一个专门为 ...
- count(*)实现原理+两阶段提交总结
count(*)实现原理 不同引擎的实现: MyISAM引擎把表的总行数存在了磁盘上,执行COUNT(*)就会直接返回,效率很高: InnoDB在count(*)时,需要把数据一行一行的从引擎里面取出 ...
- 无需编写代码,API业务流程测试,零代码实现
引言 除了测试单个接口,我们常常需要对多个有数据或者逻辑关联的接口进行业务流程测试,例如获取验证码-注册-登录.传统测试业务流程需要编写一系列测试代码,现在通过eoLinker全UI界面,无需编写任何 ...
- top问题
1. 从10万个数中找10个最大的数 对于这种题目,最普通的想法是先对这10万个数进行排序,然后再选取数组中前10个数,即为最后的答案,排序算法的时间复杂度不下于O(N lgN).最好的方法是建立一个 ...
- 爬虫框架Scrapy初步使用
本文转载自: Scrapy 爬取并分析酷安 6000 款 App,找到良心佳软(抓取篇) https://www.makcyun.top/web_scraping_withpython10.html ...
- MySQL7.5.15数据库配置主从服务器实现双机热备实例教程
环境说明 程序在:Web服务器192.168.0.57上面 数据库在:MySQL服务器192.168.0.67上面 实现目的:增加一台MySQL备份服务器(192.168.0.68),做为MySQL服 ...
- 【ZOJ 4060】Flippy Sequence
[链接] 我是链接,点我呀:) [题意] [题解] 按照两个区间的排列方式 我们可以分成以下几种情况 会发现这两个区间的作用 最多只能把两段连续不同的区间变为相同. 那么写个for处理出连续不相同的一 ...
- hdu 4612 双联通缩点+树形dp
#pragma comment(linker,"/STACK:102400000,102400000")//总是爆栈加上这个就么么哒了 #include<stdio.h> ...
- Timus - 1209 - 1, 10, 100, 1000...
先上题目: 1209. 1, 10, 100, 1000... Time limit: 1.0 secondMemory limit: 64 MB Let's consider an infinite ...