日期:2014-05-19  浏览次数:20779 次

C#如何把数据库记录导入到TXT文本
C#如何把数据库记录导入到TXT文本  
例如有A表,里面有4个字段,ID     NAME     AGE     DATATIME
根据时间查询相关记录,然后点击保存     把数据库内容写入到TXT文本中
哪位高手可以给个完整代码   我想看看

------解决方案--------------------
通过StreamReader来获取
------解决方案--------------------
ds -> XML


------解决方案--------------------
根据你给定的时间从数据库中select需要记录至DataSet中,然后循环DS中的datatable
foreach(datarow in ds.datatable.row)
{
string ss += datarow.Text;
}
然后那获得的字符串用(IO)StreamReader写到txt文件中.
------解决方案--------------------
先引用using System.IO;
再将你的数据放在dataset里..

private void button1_Click(object sender, System.EventArgs e)
{
WriteFile.filecreate(ds);
MessageBox.Show( "导出成功,文本存放在C:\\*.txt ");
}

public static void filecreate(DataSet ds)
{
FileStream fs = new FileStream( "C:\\*.txt ",FileMode.Append,FileAccess.Write);

StreamWriter sw = new StreamWriter(fs);
foreach(DataRow dr in ds.Tables[0].Rows)
{
sw.Write( "姓名: ");
sw.Write(dr[1]);
sw.Write( "\r\n ");
sw.Write( "类别: ");
sw.Write(dr[7]);
sw.Write( "\r\n ");
sw.Write( "性名: ");
sw.Write(dr[3]);
sw.Write( "\r\n ");
sw.Write( "称呼: ");
sw.Write(dr[8]);
sw.Write( "\r\n ");
sw.Write( "手机: ");
sw.Write(dr[4]);
sw.Write( "\r\n ");
sw.Write( "电话: ");
sw.Write(dr[9]);
sw.Write( "\r\n ");
sw.Write( "传真: ");
sw.Write(dr[10]);
sw.Write( "\r\n ");
sw.Write( "OICQ: ");
sw.Write(dr[11]);
sw.Write( "\r\n ");
sw.Write( "信箱: ");
sw.Write(dr[5]);
sw.Write( "\r\n ");
sw.Write( "地址: ");
sw.Write(dr[6]);
sw.Write( "\r\n ");
sw.Write( "备注: ");
sw.Write(dr[2]);
sw.Write( "\r\n ");
}



sw.Flush();
sw.Close();
fs.Close();
}