当前位置:数据库 > 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();
}
}

补充:数据库,mysql教程
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,