c# 提取数据库中的信息按指定的文书要求生成txt文档

io方面的问题,最近初学c#,比如数据库中只有一个表,包含姓名,学号之类的信息,那么我想去其中取出这些,按照姓名12位,学号10位的要求生成一个文本文档,那么应该怎么写呢... io方面的问题,最近初学c#,比如数据库中只有一个表,包含姓名,学号之类的信息,那么我想去其中取出这些,按照姓名12位,学号10位的要求生成一个文本文档,那么应该怎么写呢,主要是写这个部分不会,希望大哥大姐们指点 展开
 我来答
iwlyx
2011-06-01 · TA获得超过4825个赞
知道大有可为答主
回答量:5599
采纳率:50%
帮助的人:4793万
展开全部
我的一个把数据库的内容读出到一个txt文件的代码,你可以参考下,写入文件主要看button2_Click_1这个类
static void insert(string name, int score)
{
using (SqlConnection conn = new SqlConnection(@"Data Source =DESTINY-PC;Initial Catalog=ADO.NET;Integrated Security=true"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//cmd.CommandText = "Select Id from T_Users where UserName = '" + username + "' and PassWord='" + password + "'";
//cmd.CommandText = "Select Id from T_Users where UserName = @username and PassWord = @password";
cmd.CommandText = "Insert into dbo.T_NameAndScore (Name,Score) values (@name,@score)";
cmd.Parameters.Add(new SqlParameter("name", name));
cmd.Parameters.Add(new SqlParameter("score", score));
cmd.ExecuteNonQuery();
}
}
}
static int count()
{
using (SqlConnection conn = new SqlConnection(@"Data Source =DESTINY-PC;Initial Catalog=ADO.NET;Integrated Security=true"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//cmd.CommandText = "Select Id from T_Users where UserName = '" + username + "' and PassWord='" + password + "'";
//cmd.CommandText = "Select Id from T_Users where UserName = @username and PassWord = @password";
cmd.CommandText = "select COUNT(*) as number from T_NameAndScore";
//cmd.Parameters.Add(new SqlParameter("name", name));
//cmd.Parameters.Add(new SqlParameter("score", score));
SqlDataReader dr = cmd.ExecuteReader();
int i = 0;
while (dr.Read())
{
i = Convert.ToInt32(dr["number"]);
}
//string str = dr["number"].ToString();
//int i = Convert.ToInt32(str);
//int i = dr.GetInt32(dr.GetOrdinal("number"));
return i;

}
}
}
static string[] select()
{
//string[] str = new String[20];
//StringBuilder[] str = new StringBuilder();
using (SqlConnection conn = new SqlConnection(@"Data Source =DESTINY-PC;Initial Catalog=ADO.NET;Integrated Security=true"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//cmd.CommandText = "Select Id from T_Users where UserName = '" + username + "' and PassWord='" + password + "'";
//cmd.CommandText = "Select Id from T_Users where UserName = @username and PassWord = @password";
cmd.CommandText = "select Name,Score from T_NameAndScore";
//cmd.Parameters.Add(new SqlParameter("name", name));
//cmd.Parameters.Add(new SqlParameter("score", score));
SqlDataReader dr = cmd.ExecuteReader();
//int a = dr.GetSchemaTable().Rows.Count;
int a = count();
string[] str = new String[a];
int i = 0;
//string name, score;
while (dr.Read() && dr != null)
{
//string name = dr.GetString(0);
//name = dr["Name"].ToString();
//string score = Convert.ToString(dr.GetInt32(1));
//score = dr["Score"].ToString();
//str[i] = name + "|" + score;
str[i] = dr["Name"].ToString() + "|" + dr["Score"].ToString();
//Console.WriteLine("Str是{0}",str[i]);
i++;
}
return str;
}

}
}
int number = 0;
private void button1_Click(object sender, EventArgs e)
{
if (ofd1.ShowDialog() == DialogResult.OK)
{
using (FileStream fs = File.OpenRead(ofd1.FileName))
{
using(StreamReader sr = new StreamReader(fs))
{
string line = null;
while ((line = sr.ReadLine()) != null)
{
string username = line.Split('|')[0];
int score = Convert.ToInt32(line.Split('|')[1]);
insert(username, score);
number++;
}
MessageBox.Show(string.Format("操作成功,共插入了{0}条数据", number));
Application.Exit();
}
}

}
}

private void button2_Click_1(object sender, EventArgs e)
{
if (sfd1.ShowDialog() == DialogResult.OK)
{
using (FileStream fs = File.OpenWrite(sfd1.FileName))
{
using (StreamWriter sw = new StreamWriter(fs))
{
string[] str = select();
for (int i = 0; i < str.Length; i++)
{
//MessageBox.Show(str[i]);
sw.WriteLine(str[i]);
}

Application.Exit();
}

}
匿名用户
2011-06-01
展开全部
datatable dt = ....//去数据库读数据
for(int i=0;i<=dt.Rows.Count-1;i++)
{
输出(dt.Rows[i]["姓名"].ToString() + dt.Rows[i]["学号"].ToString() +dt.Rows[i]["性别"].ToString() )
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友d242235
2011-06-02 · TA获得超过2385个赞
知道大有可为答主
回答量:1403
采纳率:84%
帮助的人:822万
展开全部
IO部分并不难,这里贴一段示例代码,表示如何向一个文本文件内写入内容。
using System;
using System.IO;

class Test
{
public static void Main()
{
// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
using (StreamWriter sw = new StreamWriter("TestFile.txt"))
{
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
kunjust
2011-06-01 · 超过25用户采纳过TA的回答
知道答主
回答量:156
采纳率:0%
帮助的人:61.2万
展开全部
多馀的不写了。查出数据来。然后用下面两个类。就可以实现了
FileStream
StreamWriter
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式