.net連接Mysql封裝類代碼 可直接調(diào)用
下面是我封裝好的連接數(shù)據(jù)庫(kù)的類,直接調(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ù)庫(kù)連接橋
/// </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ù)庫(kù)的某一張表,可以調(diào)用GetConnection.GetDataTable("select * from UserInfo");調(diào)用都很方便。
相關(guān)文章
asp.net System.Net.Mail 發(fā)送郵件
一個(gè)師弟發(fā)了段代碼給我,說調(diào)試了很久發(fā)送郵件都沒有成功。自己使用過程中,也發(fā)現(xiàn)了很多問題,但最簡(jiǎn)單的問題是“發(fā)件方”地址根本不支持smtp發(fā)送郵件。2009-04-04基于Asp.Net MVC4 Bundle捆綁壓縮技術(shù)的介紹
本篇文章,小編將為大家介紹,Asp.Net MVC4 Bundle捆綁壓縮技術(shù),有需要的朋友可以參考一下2013-04-04.NET實(shí)現(xiàn)在網(wǎng)頁(yè)中預(yù)覽Office文件的3個(gè)方法
這篇文章主要介紹了.NET實(shí)現(xiàn)在網(wǎng)頁(yè)中預(yù)覽Office文件的3個(gè)方法,本文最終采用了ASPOSE+pdf2swf+FlexPaper的方式解決了這個(gè)需求,需要的朋友可以參考下2014-10-10ASP.NET(C#)應(yīng)用程序配置文件app.config/web.config的增、刪、改操作
應(yīng)用程序配置文件,對(duì)于asp.net是 web.config,對(duì)于WINFORM程序是 App.Config(ExeName.exe.config)。2009-06-06.net core實(shí)用技巧——將EF Core生成的SQL語句顯示在控制臺(tái)中
這篇文章主要介紹了如何將EF Core生成的SQL語句顯示在控制臺(tái)中,幫助大家更好的理解和學(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)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧2018-08-08未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例 (System.NullReferenceException)
System.NullReferenceException:未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例,這是一個(gè)新鳥,中鳥,老鳥都避不開的錯(cuò)誤2012-03-03