20070510 c# mysql 资料
http://www.yippeesoft.com

http://dev.mysql.com/tech-resources/articles/dotnet/index.html#OLEDB.NET
Microsoft .NET is a set of Microsoft software technologies for connecting your world of information, people, systems, and devices. It enables an unprecedented level of software integration through the use of XML Web services: small, discrete, building-block applications that connect to each other as well as to other, larger applications via the Internet.

For more information about .NET, refer to the following FAQ: http://www.microsoft.com/net/defined/faq.asp

Presently, developers can access MySQL using three different methods.

   * The ODBC.NET Solution – MyODBC Driver
   * Using MySQL Native .NET Providers
   * Using the OLEDB.NET Solution – MyOLDDB Provider

其中
this is the current stable release of the MySQL Connector/ODBC (MyODBC) driver. It is recommended for most users.

Using MySQL Native .NET Providers
There are a number of fully managed .NET providers available to help MySQL users develop applications in the .NET environment.
Two popular choices are:
  1. ByteFX.Data
  MySQL Connector/Net 1.0 is a fully-managed ADO.NET driver for MySQL. These packages are usable from a .NET 2.0 application but does not support the new features available with ADO.Net 2.0. For those features, you should be using Connector/Net 5.0 or later.
MySQL Connector/Net is an ADO.NET driver for MySQL.

 2. CoreLabs
   商业版本

Using the OLEDB.NET Solution – MyOLDDB Provider
The OLDDB.NET provider can be used in a similar way to ODBC.NET, for exploring MySQL through the MyOLEDB Provider.
MySQL currently doesn\’t officially support MyOLEDB, so this solution will not be discussed here.

还有一个MySQLDriverCS
A free simple .NET compliant MySQL driver. Made in C# but it would be used in all .NET compatible languages (VB.NET, Managed C++,…). This project was developed by M.L. Vi�as Livschitz in collaboration with CeDEI, Ramon Llull University, Barcelona, Spa

用C#连接mysql数据库
1.连接:
1.安装Microsoft ODBC.net。
2.安装MySQL的ODBC驱动程序。
2.解决方案管理中添加引用Microsoft.Data.Odbc.dll(1.0.3300)
3.代码中增加引用
using Microsoft.Data.Odbc;
4.编写代码
string MyConString = "DRIVER=&leftsign;MySQL ODBC 3.51 Driver&rightsign;;" +
"SERVER=localhost;" +
"DATABASE=samp_db;" +
"UID=root;" +
"PASSWORD=;" +
"OPTION=3";
//Connect to MySQL using Connector/ODBC
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
Console.WriteLine("\\n !!! success, connected successfully !!!\\n");
MyConnection.Close();

使用MySQLDriverCS
可能大部分的人都不知道这个东西,MySQLDriverCS是MySQL数据库的一个免费开源的.NET驱动程序。和Sql .NET Data Provider是为Sql Server一样,它是专门为MySQL设计的,可以叫做MySQL .NET Data Provider。使用他不需要额外的去设置ODBC数据源,基本上只要能连接到MySQL就能通过MySQLDriverCS来访问。
MySQLDriverCS是SourceForge.NET上的一个项目,不过不知道什么原因,这个网站在国内访问不到。
下面是使用MySQLDriverCS的代码示例:

           
MySQLConnection conn = null;
try
&leftsign;
   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++)
   &leftsign;
            cmd.ExecuteNonQuery();
   &rightsign;
   cmd.Dispose();
   conn.Close();
   query = "select * from test.dbtable";
   MySQLCommand cmd2 = new MySQLCommand(query, conn);
   conn.Open();
   MySQLDataReader reader = cmd2.ExecuteReaderEx();
   while(reader.Read())
   &leftsign;
       tmp = reader[0].ToString();
       tmp = reader[1].ToString();
       tmp = reader[2].ToString();
       tmp = reader[3].ToString();
   &rightsign;
   conn.Close();
   query = "delete from test.dbtable";
   MySQLCommand cmd3 = new MySQLCommand(query, conn);
   conn.Open();
   cmd3.ExecuteNonQuery();
&rightsign;
catch(Exception ex)
&leftsign;
   MessageBox.Show(ex.Message);
&rightsign;
finally
&leftsign;
   conn.Close();
&rightsign;

和上面的那段代码几乎一模一样,所不同的是Odbc变成了MySQL,另外,需要注意的一点是Command的ExecuteReader方法在MySQLDriverCS中变成了ExecuteReaderEx,还有些细微的差别请参考附带的文档详细的介绍。

static void Main(string[] args)
       &leftsign;
           string sqlstr = "select * from manavatar";
           MySQLConnection DBConn = new MySQLConnection(new MySQLConnectionString("192.168.0.13", "flashdata", "root", "root", 3306).AsString);
           DBConn.Open();
           //MySQLDataAdapter myadap = new MySQLDataAdapter(sqlstr, conn);
           MySQLCommand DBComm = new MySQLCommand(sqlstr,DBConn);
           MySQLDataReader DBReader = DBComm.ExecuteReaderEx(); //DBComm.ExecuteReaderEx();
           MySQLDataAdapter DTAdapter = new MySQLDataAdapter(sqlstr,DBConn);
           
           DataSet myDataSet = new DataSet();
           DTAdapter.Fill(myDataSet,"manavatar");
         
       
           try
           &leftsign;
               while (DBReader.Read())
               &leftsign;
                   //Console.WriteLine("11");
                   Console.WriteLine("DBReader:&leftsign;0&rightsign;,\\t\\t\\tddddd:&leftsign;1&rightsign;,\\t\\t &leftsign;2&rightsign;",DBReader.GetString(0), DBReader.GetString(1),DBReader.GetString(3));
               &rightsign;
               Console.WriteLine("0000");
           &rightsign;
           catch (Exception e)
           &leftsign;
               Console.WriteLine("读入失败!"+e.ToString());
           &rightsign;
           finally
           &leftsign;
               Console.WriteLine("DBReader关闭");
               Console.WriteLine("DBConn关闭");
               DBReader.Close();
               //DBConn.Close();
           &rightsign;
           
           for (int i = 0; i < myDataSet.Tables["manavatar"].Rows.Count; i++)
           &leftsign;
               Console.WriteLine("&leftsign;0&rightsign;",myDataSet.Tables["manavatar"].Rows[2]["user"]);
           &rightsign;
           
           
       &rightsign;

这是一个简单的例子。
在这里有个问题:dataset如果没设主键的话,可能会引起一些对数库操作的问题,比如会造成updata出现错误。
一、         odbc
通过使用针对mysql的odbc驱动,在页面中连接到mysql。
1.         下载地址:3.51版本
http://dev.mysql.com/downloads/connector/odbc/3.51.html
有两个版本,安装版和不用安装版

5.0版本 http://dev.mysql.com/downloads/connector/odbc/5.0.html
This is a Beta release of the new ODBC connector and has some bugs and lacks some features – please do not use in a production environment.

二  Connector/Net
这里边有两个版本
Connector/Net 1.0
1.         下载地址:
http://dev.mysql.com/downloads/connector/net/1.0.html
MySQL Connector/Net 1.0 is a fully-managed ADO.NET driver for MySQL. These packages are usable from a .NET 2.0 application but does not support the new features available with ADO.Net 2.0. For those features, you should be using Connector/Net 5.0 or later.

Connector/Net 5.0.2
1.         下载地址
http://dev.mysql.com/downloads/connector/net/5.0.html
MySQL Connector/Net is an ADO.NET driver for MySQL.
 MySQL.VisualStudio-1.0.1-beta

这个主要作用在于可以在vs2005种使用服务资源管理器添加数据库

历史博文

标签:, , ,
十月 12, 2007 at 3:19 下午 by yippee 1,037 次
Category: Info
Tags: , , ,