熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

.net連接Mysql封裝類代碼 可直接調用

2013-11-13 10:34:01  來源: .NET編程 
下面是我封裝好的連接Mysql數據庫的類直接調用即可  

  微軟的visual studio沒有自帶連接Mysql的驅動要去網上下載一個mysqlconnectornet驅動然後安裝就可以使用
下面是我封裝好的連接數據庫的類直接調用即可

復制代碼 代碼如下:
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemData;
using SystemDataSqlClient;
using SystemConfiguration;
using MySqlDataMySqlClient;
namespace DAL
{
  public class GetConnection
    {
        private static MySqlConnection  _connection;
      /// <summary>
      /// 獲取數據庫連接橋
      /// </summary>
        private static MySqlConnection Connection
        {
            get
            {
               //string connectionString = ConfigurationManagerAppSettings["ConnectionString"];
                string connectionString = "server=localhost;user id=root; password=root; database=system; pooling=false";
                //server=;port=;uid=user;pwd=;database=basename;遠程連接的
                //string connectionString = "Data Source=;Initial Catalog=wwj;Persist Security Info=True;User ID=wwj;Password=wwj";
                if (_connection == null)
                {
                    _connection = new MySqlConnection(connectionString);
                    _connectionOpen();
                }
                if (_connectionState == ConnectionStateClosed)
                {
                    _connectionOpen();
                }
                if (_connectionState == ConnectionStateBroken)
                {
                    _connectionClose();
                    _connectionOpen();
                }
                return GetConnection_connection;
            }
        }
      /// <summary>
      /// 獲取表數據
      /// </summary>
      /// <param name="sql"></param>
      /// <returns></returns>
        public static MySqlDataReader GetDataRead(string sql)
        {
            MySqlCommand command = new MySqlCommand(sql Connection);
            MySqlDataReader read = commandExecuteReader();
            return read;
        }
        public static int NoSelect(string sql)
        {
            MySqlCommand command = new MySqlCommand(sql Connection);
            int row = commandExecuteNonQuery();
            return row;
        }
        public static DataTable GetDataTable(string sql)
        {
            MySqlCommand command = new MySqlCommand(sql Connection);
            DataTable dt = new DataTable();
            MySqlDataAdapter sda = new MySqlDataAdapter(command);
            sdaFill(dt);
            return dt;
        }
        /// <summary>
        /// 執行sql語句返回一行一列
        /// </summary>
        /// <param name="sql">SQL語句</param>
        /// <returns></returns>
        public static string GetScalar(string sql)
        {
            MySqlCommand command = new MySqlCommand(sql Connection);
            return commandExecuteScalar()ToString();
        }
    }
}

  
比 如說你想執行刪除的你可以調用GetConnectionNoSelect("delete from UserInfo where Id=");讀數據庫的某一張表可以調用GetConnectionGetDataTable("select * from UserInfo");調用都很方便


From:http://tw.wingwit.com/Article/program/net/201311/14014.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.