C#链接mysql

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


namespace sqltest
{
    class Program
    {
        static void Main(string[] args)
        {
            //new Program().getData("show tables");  
            Program pr = new Program();
            pr.getData("select * from user");
            Console.ReadKey();
        }

        public void getData(String sql)
        {
            MySqlConnection conn = null;
            MySqlCommand command = null;
            MySqlDataReader reader = null;
            try
            {
                conn = new MySqlConnection("Server=localhost;User Id=root;Password=xman;Persist Security Info=True;Database=test");
                //conn = new MySqlConnection("server=127.0.0.1;uid=root;" + "pwd=xman;database=sauc4;");
                //conn = new MySqlConnection("server=127.0.0.1;user id=root; password=xman; database=mysql; pooling=false");
                command = conn.CreateCommand();
                command.CommandText = sql;
                conn.Open();
                Console.WriteLine("打开MYSQL成功");
               
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(reader[0]);
                    Console.WriteLine(reader[1]);
                    Console.WriteLine(reader[2]);
                }

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

    }
}

猜你喜欢

转载自gyl868.iteye.com/blog/1130067
今日推荐