C#使用MySQLDriverCSS连接MySQL数据库的方法

作者:袖梨 2022-06-25

第一步:下载和安装MySQLDriverCS.

下载地址:http://sourceforge.net/projects/mysqldrivercs/

(下载的版本为 MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe)

下载下来.exe文件,点击下一步,下一步…在安装的文件夹下面有个dll文件夹,dll文件下面有个MySQLDriver.dll. 这个就是我们需要的。

 

第二步:VS2010上添加引用 

 第三步:C# 连接数据库实例

第一,添加 using MySQLDriverCS;

C# 操作MySQL实例教程代码,发给喜欢代码的朋友,呵呵。

本文使用MySQLDriverCS操作MYSQL数据库,www.111com.net 使用前请先引入MySQLDriverCS.dll文件,支持PHP4.0和PHP5.0
C#代码

 代码如下 复制代码

 

    static void Main(string[] args)  
            {  
                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 
                {  
                    while (DBReader.Read())  
                    {  
                        //Console.WriteLine("11"); 
             Console.WriteLine("DBReader:{0},tttddddd:{1},tt {2}",DBReader.GetString(0), DBReader.GetString(1),DBReader.GetString(3));  
                    }  
                    Console.WriteLine("0000");  
                }  
                catch (Exception e)  
                {   
                    Console.WriteLine("读入失败!"+e.ToString());  
                }  
                finally 
                {  
                    Console.WriteLine("DBReader关闭");  
                    Console.WriteLine("DBConn关闭");  
                    DBReader.Close();  
                    //DBConn.Close(); 
                }  
                  
                for (int i = 0; i < myDataSet.Tables["manavatar"].Rows.Count; i++)  
                {  
                    Console.WriteLine("{0}",myDataSet.Tables["manavatar"].Rows[2]["user"]);  
                }  
                  
                  
            }  

 

相关文章

精彩推荐