好贷网好贷款

C#中从数据库导出至txt

发布时间:2016-12-5 2:18:14 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"C#中从数据库导出至txt",主要涉及到C#中从数据库导出至txt方面的内容,对于C#中从数据库导出至txt感兴趣的同学可以参考一下。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; namespace 导出数据库表至txt { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (sfd.ShowDialog() == DialogResult.OK) { using (FileStream fstream = File.OpenWrite(sfd.FileName)) { StreamWriter sw = new StreamWriter(fstream, System.Text.Encoding.GetEncoding("GB2312")); using (SqlConnection conn = new SqlConnection("server=.;database=test;user id=sa;password=123456")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from T_persons"; using (SqlDataReader reader=cmd.ExecuteReader()) { while (reader.Read()) { string name = reader.GetString(reader.GetOrdinal("name")); int age = reader.GetInt32(reader.GetOrdinal("age")); sw.WriteLine(name + "|" + age); } sw.Flush(); } } } MessageBox.Show("数据导出成功!"); } } } } }

上一篇:Struts has detected an unhandled exception: Messages: Bean named
下一篇:c++11简单介绍

相关文章

相关评论