【原创】当C#遇到MySql (用C#访问MySql数据库)

基本上这个就是我的第一个C#程序了吧 o(∩_∩)o... 还挺顺利的

首先解决驱动问题
1:下载MySQL.VisualStudio-1.1.2
2:下载Mysql-connector-net-5.0.3
3:下载MySql数据库5.0x

using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
namespace CSDB
{
    
class MySQLResults
    
{
        
public MySQLResults(string sql)
        
{
            MySqlConnection conn 
= null;
            MySqlCommand command 
= null;
            MySqlDataReader reader 
= null;
            
try
            
{
                conn 
= new MySqlConnection("Server=localhost;User Id=baolu;Password=baolu;Persist Security Info=True;Database=baolu_db");
                command 
= conn.CreateCommand();
                command.CommandText 
= sql;
                conn.Open();
                reader 
= command.ExecuteReader();
                
while (reader.Read())
                
{
                    Console.WriteLine(reader[
0]);
                    Console.WriteLine(reader[
1]);
                    Console.WriteLine(reader[
2]);
                }

            }

            
catch (MySqlException se)
            
{
                Console.WriteLine(
"Database operation errors : " + se.StackTrace);
                conn.Close();
                command 
= null;
                reader.Close();
            }


        }


    }

}



using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;

namespace CSDB
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
new MySQLResults("select * from user");
        }

    }

}


参考书籍:【Sybex - Mastering C# Database Programming】

posted on 2007-05-13 03:37  宝露o(∩_∩)o  阅读(1032)  评论(2编辑  收藏  举报

导航