.net连接Mysql封装类代码 可直接调用
网络编程 2021-07-04 22:40www.168986.cn编程入门
狼蚁网站SEO优化是我封装好的连接Mysql数据库的类,直接调用即可。
微软的visual studio没有自带连接Mysql的驱动,要去网上下载一个mysql-connector--6.4.3驱动,然后安装就可以使用。
狼蚁网站SEO优化是我封装好的连接数据库的类,直接调用即可。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using MySql.Data.MySqlClient;
namespace DAL
{
public class GetConnection
{
private static MySqlConnection _connection;
/// <summary>
/// 获取数据库连接桥
/// </summary>
private static MySqlConnection Connection
{
get
{
//string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
string connectionString = "server=localhost;user id=root; password=root; database=system; pooling=false";
//server=222.222.222.222;port=3306;uid=user;pwd=;database=basename;远程连接的
//string connectionString = "Data Source=202.192.72.22;Initial Catalog=wwj;Persist Security Info=True;User ID=wwj;Password=wwj123";
if (_connection == null)
{
_connection = new MySqlConnection(connectionString);
_connection.Open();
}
if (_connection.State == ConnectionState.Closed)
{
_connection.Open();
}
if (_connection.State == ConnectionState.Broken)
{
_connection.Close();
_connection.Open();
}
return GetConnection._connection;
}
}
/// <summary>
/// 获取表数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static MySqlDataReader GetDataRead(string sql)
{
MySqlCommand mand = new MySqlCommand(sql, Connection);
MySqlDataReader read = mand.ExecuteReader();
return read;
}
public static int NoSelect(string sql)
{
MySqlCommand mand = new MySqlCommand(sql, Connection);
int row = mand.ExecuteNonQuery();
return row;
}
public static DataTable GetDataTable(string sql)
{
MySqlCommand mand = new MySqlCommand(sql, Connection);
DataTable dt = new DataTable();
MySqlDataAdapter sda = new MySqlDataAdapter(mand);
sda.Fill(dt);
return dt;
}
/// <summary>
/// 执行sql语句,返回一行一列。。
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static string GetScalar(string sql)
{
MySqlCommand mand = new MySqlCommand(sql, Connection);
return mand.ExecuteScalar().ToString();
}
}
}
比如说你想执行删除的,你可以调用GetConnection.NoSelect("delete from UserInfo where Id=1");读数据库的某一张表,可以调用GetConnection.GetDataTable("select from UserInfo");调用都很方便。
狼蚁网站SEO优化是我封装好的连接数据库的类,直接调用即可。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using MySql.Data.MySqlClient;
namespace DAL
{
public class GetConnection
{
private static MySqlConnection _connection;
/// <summary>
/// 获取数据库连接桥
/// </summary>
private static MySqlConnection Connection
{
get
{
//string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
string connectionString = "server=localhost;user id=root; password=root; database=system; pooling=false";
//server=222.222.222.222;port=3306;uid=user;pwd=;database=basename;远程连接的
//string connectionString = "Data Source=202.192.72.22;Initial Catalog=wwj;Persist Security Info=True;User ID=wwj;Password=wwj123";
if (_connection == null)
{
_connection = new MySqlConnection(connectionString);
_connection.Open();
}
if (_connection.State == ConnectionState.Closed)
{
_connection.Open();
}
if (_connection.State == ConnectionState.Broken)
{
_connection.Close();
_connection.Open();
}
return GetConnection._connection;
}
}
/// <summary>
/// 获取表数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static MySqlDataReader GetDataRead(string sql)
{
MySqlCommand mand = new MySqlCommand(sql, Connection);
MySqlDataReader read = mand.ExecuteReader();
return read;
}
public static int NoSelect(string sql)
{
MySqlCommand mand = new MySqlCommand(sql, Connection);
int row = mand.ExecuteNonQuery();
return row;
}
public static DataTable GetDataTable(string sql)
{
MySqlCommand mand = new MySqlCommand(sql, Connection);
DataTable dt = new DataTable();
MySqlDataAdapter sda = new MySqlDataAdapter(mand);
sda.Fill(dt);
return dt;
}
/// <summary>
/// 执行sql语句,返回一行一列。。
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public static string GetScalar(string sql)
{
MySqlCommand mand = new MySqlCommand(sql, Connection);
return mand.ExecuteScalar().ToString();
}
}
}
比如说你想执行删除的,你可以调用GetConnection.NoSelect("delete from UserInfo where Id=1");读数据库的某一张表,可以调用GetConnection.GetDataTable("select from UserInfo");调用都很方便。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程