.net連接Mysql封裝類代碼 可直接調(diào)用
更新時間:2013年07月26日 12:03:38 作者:
下面是我封裝好的連接Mysql數(shù)據(jù)庫的類,直接調(diào)用即可。
微軟的visual studio沒有自帶連接Mysql的驅(qū)動,要去網(wǎng)上下載一個mysql-connector-net-6.4.3驅(qū)動,然后安裝就可以使用。
下面是我封裝好的連接數(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;遠程連接的
//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;遠程連接的
//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 System.Net.Mail 發(fā)送郵件
一個師弟發(fā)了段代碼給我,說調(diào)試了很久發(fā)送郵件都沒有成功。自己使用過程中,也發(fā)現(xiàn)了很多問題,但最簡單的問題是“發(fā)件方”地址根本不支持smtp發(fā)送郵件。2009-04-04基于Asp.Net MVC4 Bundle捆綁壓縮技術(shù)的介紹
本篇文章,小編將為大家介紹,Asp.Net MVC4 Bundle捆綁壓縮技術(shù),有需要的朋友可以參考一下2013-04-04.NET實現(xiàn)在網(wǎng)頁中預(yù)覽Office文件的3個方法
這篇文章主要介紹了.NET實現(xiàn)在網(wǎng)頁中預(yù)覽Office文件的3個方法,本文最終采用了ASPOSE+pdf2swf+FlexPaper的方式解決了這個需求,需要的朋友可以參考下2014-10-10ASP.NET(C#)應(yīng)用程序配置文件app.config/web.config的增、刪、改操作
應(yīng)用程序配置文件,對于asp.net是 web.config,對于WINFORM程序是 App.Config(ExeName.exe.config)。2009-06-06.net core實用技巧——將EF Core生成的SQL語句顯示在控制臺中
這篇文章主要介紹了如何將EF Core生成的SQL語句顯示在控制臺中,幫助大家更好的理解和學(xué)習(xí).net core,感興趣的朋友可以了解下2020-08-08利用Asp.Net Core的MiddleWare思想如何處理復(fù)雜業(yè)務(wù)流程詳解
這篇文章主要給大家介紹了關(guān)于利用Asp.Net Core的MiddleWare思想如何處理復(fù)雜業(yè)務(wù)流程的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起看看吧2018-08-08未將對象引用設(shè)置到對象的實例 (System.NullReferenceException)
System.NullReferenceException:未將對象引用設(shè)置到對象的實例,這是一個新鳥,中鳥,老鳥都避不開的錯誤2012-03-03