萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> 關於c#中多種連接mysql數據方法

關於c#中多種連接mysql數據方法

關於c#中多種連接mysql教程數據方法
方法一:
使用mysql推出的mysql connector/net is an ado.net driver for mysql
該組件為mysql為ado.net訪問mysql數據庫教程設計的.net訪問組件。
安裝完成該組件後,引用命名空間mysql.data.mysqlclient;
使用命令行編譯時:csc /r:mysql.data.dll test.cs
方法二:
通過odbc訪問mysql數據庫
訪問前要先下載兩個組件:odbc.net和mysql的odbc驅動(mysql connector/odbc (myodbc) driver)目前為3.51版
安裝完成後,即可通過odbc訪問mysql數據庫
方法三:
使用corelab推出的mysql訪問組件,面向.net
安裝完成後,引用命名空間:corelab.mysql;
使用命令編譯時:csc /r:corelab.mysql.dll test.cs

mysqlconnection conn = null;   
 try  
 {   
//mysqlconnection conn = new mysqlconnection(new mysqlconnectionstring("myquest", "root", "amttgroup").asstring);   
//實例化一個連接對象其中myquest為數據庫名,root為數據庫用戶名,amttgroup為數據庫密碼    
     string connstr = "data source=mysql;password=root;user id=root;location=localhost";   
     conn =     new mysqlconnection(constr);   
     conn.open();   
     string query = "insert into test.dbtable values(10, 'disksidkfsdi', 'asdfaf', 'adsfasdf')";   
     string tmp = null;   
     mysqlcommand cmd = new mysqlcommand(query, conn);   
     for(int i = 0; i < 100000; i++)   
     {   
       cmd.executenonquery();   
     }   
     cmd.dispose();   
     conn.close();   
     query = "select * from test.dbtable";   
     mysqlcommand cmd2 = new mysqlcommand(query, conn);   
     conn.open();   
  
  //設定中文輸出   
  mysqlcommand firstcmd = new mysqlcommand("set names gb2312", myconn);   
  firstcmd.executenonquery();   
  
     mysqldatareader reader = cmd2.executereaderex();   
     while(reader.read())   
     {   
         tmp = reader[0].tostring();   
         tmp = reader[1].tostring();   
         tmp = reader[2].tostring();   
         tmp = reader[3].tostring();   
     }   
     conn.close();   
     query = "delete from test.dbtable";   
     mysqlcommand cmd3 = new mysqlcommand(query, conn);   
     conn.open();   
     cmd3.executenonquery();   
 }   
 catch(exception ex)   
 {   
     messagebox.show(ex.message);   
 }   
 finally  
 {   
     conn.close();   
 }


mysqldrivercs實現在.net環境下訪問mysql數據庫

 

using system;

using system.data;

using system.data.sqlclient;

using system.configuration;

using system.web;

using system.web.security;

using system.web.ui;

using system.web.ui.webcontrols;

using system.web.ui.webcontrols.webparts;

using system.web.ui.htmlcontrols;

using mysqldrivercs;

 

public partial class _default : system.web.ui.page

{

    protected void page_load(object sender, eventargs e)

    {

        mysqlconnection conn = null;

        conn = new mysqlconnection(new mysqlconnectionstring("127.0.0.1", "dvbbs", "root", "123456").asstring);

        conn.open();

        mysqlcommand commn = new mysqlcommand("set names gb2312",conn);

        commn.executenonquery();

        string sql = "select title,topicid, boardid ,dateandtime,hits  from  dv_topic order by dateandtime desc limit 10";

        mysqldataadapter mda = new mysqldataadapter(sql,conn);

        dataset ds = new dataset();

        mda.fill(ds, "table1");

        this.repeater1.datasource = ds;

        this.repeater1.databind();

        conn.close();

    }

}

就這樣就可以實現在.net環境下對mysql數據的訪問。在這之前還要做一些准備,下載mysqldrivercs,安裝,在安裝文件夾下面找到mysqldriver.dll,然後將mysqldriver.dll添加引用到項目中就ok了。也許你對這行代碼

mysqlcommand commn = new mysqlcommand("set names gb2312",conn);

        commn.executenonquery();


網友提供的方法

 

以下為訪問mysql數據庫實例

編譯指令:csc /r:corelab.mysql.dll /r:mysql.data.dll test.cs

using system;
using system.net;
using system.text;
using corelab.mysql;
using system.data.odbc;
using mysql.data.mysqlclient;

class connectmysql
{

public void connect_corelab()
{
string constr = "user id=root;host=localhost;database=qing;password=qing";
mysqlconnection mycn = new mysqlconnection(constr);
mycn.open();
mysqlcommand mycm = new mysqlcommand("select * from shop",mycn);
mysqldatareader msdr = mycm.executereader();
while(msdr.read())
{
if (msdr.hasrows)
{
console.writeline(msdr.getstring(0));
}
}
msdr.close();
mycn.close();
}

public void connect_odbc()
{
//string myconstring ="dsn=mysql;uid=root;pwd=qing";
string myconstring = "driver={mysql odbc 3.51 driver};" +
"server=localhost;" +
"database=test;" +
"uid=root;" +
"password=qing;" +
"option=3";
odbcconnection myconn = new odbcconnection(myconstring);
myconn.open();
odbccommand mycm = new odbccommand("select * from hello",myconn);
odbcdatareader msdr = mycm.executereader();
while(msdr.read())
{
if (msdr.hasrows)
{
console.writeline(msdr.getstring(0));
}
}
msdr.close();
myconn.close();
}
public void connect_net()
{
string myconnectionstring = "database=test;data source=localhost;user id=root;password=qing";
mysqlconnection mycn = new mysqlconnection(myconnectionstring);
mycn.open();
mysqlcommand mycm = new mysqlcommand("select * from hello",mycn);
mysqldatareader msdr = mycm.executereader();
while(msdr.read())
{
if (msdr.hasrows)
{
console.writeline(msdr.getstring(0));
}
}
msdr.close();
mycn.close();
}
public static void main()
{
connectmysql ms = new connectmysql();
ms.connect_corelab();
ms.connect_odbc();
connect_net();
}
}

copyright © 萬盛學電腦網 all rights reserved