.net連接Mysql封裝類代碼 可直接調(diào)用
更新時(shí)間:2013年07月26日 12:03:38 作者:
下面是我封裝好的連接Mysql數(shù)據(jù)庫的類,直接調(diào)用即可。
微軟的visual studio沒有自帶連接Mysql的驅(qū)動(dòng),要去網(wǎng)上下載一個(gè)mysql-connector-net-6.4.3驅(qū)動(dòng),然后安裝就可以使用。
下面是我封裝好的連接數(shù)據(jù)庫的類,直接調(diào)用即可。
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>
/// 獲取數(shù)據(jù)庫連接橋
/// </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;遠(yuǎn)程連接的
//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>
/// 獲取表數(shù)據(jù)
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static MySqlDataReader GetDataRead(string sql)
{
MySqlCommand command = new MySqlCommand(sql, Connection);
MySqlDataReader read = command.ExecuteReader();
return read;
}
public static int NoSelect(string sql)
{
MySqlCommand command = new MySqlCommand(sql, Connection);
int row = command.ExecuteNonQuery();
return row;
}
public static DataTable GetDataTable(string sql)
{
MySqlCommand command = new MySqlCommand(sql, Connection);
DataTable dt = new DataTable();
MySqlDataAdapter sda = new MySqlDataAdapter(command);
sda.Fill(dt);
return dt;
}
/// <summary>
/// 執(zhí)行sql語句,返回一行一列。。
/// </summary>
/// <param name="sql">SQL語句</param>
/// <returns></returns>
public static string GetScalar(string sql)
{
MySqlCommand command = new MySqlCommand(sql, Connection);
return command.ExecuteScalar().ToString();
}
}
}
比如說你想執(zhí)行刪除的,你可以調(diào)用GetConnection.NoSelect("delete from UserInfo where Id=1");讀數(shù)據(jù)庫的某一張表,可以調(diào)用GetConnection.GetDataTable("select * from UserInfo");調(diào)用都很方便。
下面是我封裝好的連接數(shù)據(jù)庫的類,直接調(diào)用即可。
復(fù)制代碼 代碼如下:
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>
/// 獲取數(shù)據(jù)庫連接橋
/// </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;遠(yuǎn)程連接的
//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>
/// 獲取表數(shù)據(jù)
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static MySqlDataReader GetDataRead(string sql)
{
MySqlCommand command = new MySqlCommand(sql, Connection);
MySqlDataReader read = command.ExecuteReader();
return read;
}
public static int NoSelect(string sql)
{
MySqlCommand command = new MySqlCommand(sql, Connection);
int row = command.ExecuteNonQuery();
return row;
}
public static DataTable GetDataTable(string sql)
{
MySqlCommand command = new MySqlCommand(sql, Connection);
DataTable dt = new DataTable();
MySqlDataAdapter sda = new MySqlDataAdapter(command);
sda.Fill(dt);
return dt;
}
/// <summary>
/// 執(zhí)行sql語句,返回一行一列。。
/// </summary>
/// <param name="sql">SQL語句</param>
/// <returns></returns>
public static string GetScalar(string sql)
{
MySqlCommand command = new MySqlCommand(sql, Connection);
return command.ExecuteScalar().ToString();
}
}
}
比如說你想執(zhí)行刪除的,你可以調(diào)用GetConnection.NoSelect("delete from UserInfo where Id=1");讀數(shù)據(jù)庫的某一張表,可以調(diào)用GetConnection.GetDataTable("select * from UserInfo");調(diào)用都很方便。
相關(guān)文章
Asp.Net?Core配置多環(huán)境log4net配置文件的全過程
在.NET世界中有非常多的日志框架,然而log4net是目前為止最流行的一款日志框架,下面這篇文章主要給大家介紹了關(guān)于Asp.Net?Core配置多環(huán)境log4net配置文件的相關(guān)資料,需要的朋友可以參考下2022-04-04
ASP.NET Session會(huì)導(dǎo)致的性能問題
你的站點(diǎn)有被客戶投訴很慢嗎?是不是查了很多遍還是沒有完全解決?是不是數(shù)據(jù)庫沒有發(fā)現(xiàn)異常,CPU也沒有異常,內(nèi)存占用量沒有異常,GC計(jì)數(shù)沒有異常,硬盤IO也沒有異常,帶寬沒有異常,線路沒有異常,沒有丟包,但就是被投訴?2009-07-07
解決asp.net Sharepoint無法連接發(fā)布自定義字符串處理程序,不能進(jìn)行輸出緩存處理的方法
解決Sharepoint無法連接發(fā)布自定義字符串處理程序,不能進(jìn)行輸出緩存處理的方法2010-03-03
.Net語言Smobiler開發(fā)之如何仿微信朋友圈的消息樣式
這篇文章主要介紹了.Net語言Smobiler開發(fā)平臺(tái)如何仿微信朋友圈的消息樣式?本文為大家揭曉答案2016-09-09
ASP.NET:把a(bǔ)shx寫到類庫里并在頁面上調(diào)用的具體方法
最近在調(diào)整博客的架構(gòu),進(jìn)一步把表現(xiàn)和業(yè)務(wù)分離,所以要把之前用ashx搞的那些Http Handler放到類庫中,下面是具體的步驟及代碼2013-06-06
asp.net下實(shí)現(xiàn)URL重寫技術(shù)的代碼
asp.net下實(shí)現(xiàn)URL重寫技術(shù)的代碼...2007-10-10
SignalR Self Host+MVC等多端消息推送服務(wù)(二)
這篇文章主要為大家詳細(xì)介紹了SignalR Self Host+MVC等多端消息推送服務(wù)的第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
asp.net Repeater控件的說明及詳細(xì)介紹及使用方法
Repeater控件是Web 服務(wù)器控件中的一個(gè)容器控件,它使您可以從頁的任何可用數(shù)據(jù)中創(chuàng)建出自定義列表。2010-04-04

