C#使用MySQLDriverCS操作MySQL實例教程代碼

C# 操作MySQL實例教程代碼,發給喜歡代碼的朋友,呵呵。

本文使用MySQLDriverCS操作MYSQL數據庫,使用前請先引入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},\t\t\tddddd:{1},\t\t {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"]);  
            }  
              
              
        }  


文章來源於《傻貓網絡日誌》 http://www.samool.com/archives/11124/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章