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(); 
        }

 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/13404.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论