C# CSV 导出

static void Main(String[] args)
        {
            Console.WriteLine("--------");

            string myconn = "Database='test';Data Source=localhost;User ID=root;Password=123456;CharSet=utf8;";
            //需要执行的SQL语句
            string mysql = "SELECT * from users";
            //创建数据库连接
            MySqlConnection myconnection = new MySqlConnection(myconn);

            myconnection.Open();
            //创建MySqlCommand对象
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);
            //通过MySqlCommand的ExecuteReader()方法构造DataReader对象
            MySqlDataReader myreader = mycommand.ExecuteReader();

            //int size = 1024;
            //int sizeCnt = (int)Math.Ceiling((double)dataGrid.RowCount / (double)2000);
            StreamWriter write = new StreamWriter("F:\\test.csv", false, Encoding.Default, 1024 * 4);

            write.Write("ID" + "," + "user_name" + "," +"user_passwd");

            write.WriteLine();

            while (myreader.Read())
            {
                string Tem = myreader.GetInt32(0) + "," + myreader.GetString(1) + "," + myreader.GetString(2);
                write.WriteLine(Tem);
            }

            write.Flush();
            write.Close();

            myreader.Close();

            myconnection.Close();
        }

 

posted @ 2012-12-19 13:52  残星  阅读(2079)  评论(1编辑  收藏  举报