标题: 帮忙看下,怎么可以在保存文件之后,调整数据的格式了 [打印本页] 作者: 梁健1 时间: 2012-9-13 17:43 标题: 帮忙看下,怎么可以在保存文件之后,调整数据的格式了 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;
using System.Configuration;
namespace 导出数据到txt文件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOutput_Click(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
int para1 = Convert.ToInt32(txt1.Text);
int para2 = Convert.ToInt32(txt2.Text);
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() != DialogResult.OK)
{
return;
}
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Select * from T_Persons where id >=@txt1 and id<=@txt2";
cmd.Parameters.Add(new SqlParameter("txt1",para1));
cmd.Parameters.Add(new SqlParameter("txt2",para2));
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string[] strs = new string[2];
strs[0] = reader.GetString(reader.GetOrdinal("name"));
strs[1] = Convert.ToString(reader.GetInt32(reader.GetOrdinal("age")));
//strs[2] = "\n";
File.AppendAllLines(sfd.FileName,strs,Encoding.UTF8);