static void Main(string[] args)
{
Stopwatch sp = new Stopwatch();
sp.Start();
// select * from TestDataBase..Student;
string connStr = @"server=.\sqlexpress;database=TestDataBase;uid=sa;pwd=123;";
string sql = "select * from TestDataBase..Student;";
#region 使用StreamWriter
//using (SqlConnection conn = new SqlConnection(connStr))
//{
// using (SqlCommand cmd = new SqlCommand(sql, conn))
// {
// conn.Open();
// using (SqlDataReader reader = cmd.ExecuteReader())
// {
// using (StreamWriter writer = new StreamWriter("student.txt", true, Encoding.Default))
// {
// List<string> list = new List<string>();
// while (reader.Read())
// {
// list.Clear();
// // 读取每一行数据
// // FileStream 二进制数据
// // File 封装了的方法
// // StreamWriter 文本文件的写操作
// // Encoding.GetEncoding("gb2312").GetBytes(
// // File.App
// for (int i = 0; i < reader.FieldCount; i++)
// {
// list.Add(reader[i].ToString());
// }
// writer.WriteLine(string.Join(",", list.ToArray()));
// }
// }
// }
// }
//}
#endregion
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
List<string> list = new List<string>();
while (reader.Read())
{
list.Clear();
// 读取每一行数据
// FileStream 二进制数据
// File 封装了的方法
// StreamWriter 文本文件的写操作
// Encoding.GetEncoding("gb2312").GetBytes(
// File.App
for (int i = 0; i < reader.FieldCount; i++)
{
list.Add(reader[i].ToString());
}
File.AppendAllText("Student1.txt", string.Join(",", list.ToArray()) + "\r\n");
}
}
}
}
sp.Stop();
Console.WriteLine(sp.Elapsed); // 00:00:01.4922520
Console.ReadKey();
}
{
Stopwatch sp = new Stopwatch();
sp.Start();
// select * from TestDataBase..Student;
string connStr = @"server=.\sqlexpress;database=TestDataBase;uid=sa;pwd=123;";
string sql = "select * from TestDataBase..Student;";
#region 使用StreamWriter
//using (SqlConnection conn = new SqlConnection(connStr))
//{
// using (SqlCommand cmd = new SqlCommand(sql, conn))
// {
// conn.Open();
// using (SqlDataReader reader = cmd.ExecuteReader())
// {
// using (StreamWriter writer = new StreamWriter("student.txt", true, Encoding.Default))
// {
// List<string> list = new List<string>();
// while (reader.Read())
// {
// list.Clear();
// // 读取每一行数据
// // FileStream 二进制数据
// // File 封装了的方法
// // StreamWriter 文本文件的写操作
// // Encoding.GetEncoding("gb2312").GetBytes(
// // File.App
// for (int i = 0; i < reader.FieldCount; i++)
// {
// list.Add(reader[i].ToString());
// }
// writer.WriteLine(string.Join(",", list.ToArray()));
// }
// }
// }
// }
//}
#endregion
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
List<string> list = new List<string>();
while (reader.Read())
{
list.Clear();
// 读取每一行数据
// FileStream 二进制数据
// File 封装了的方法
// StreamWriter 文本文件的写操作
// Encoding.GetEncoding("gb2312").GetBytes(
// File.App
for (int i = 0; i < reader.FieldCount; i++)
{
list.Add(reader[i].ToString());
}
File.AppendAllText("Student1.txt", string.Join(",", list.ToArray()) + "\r\n");
}
}
}
}
sp.Stop();
Console.WriteLine(sp.Elapsed); // 00:00:01.4922520
Console.ReadKey();
}