数据读取(SQL)与文本写入(fileStream)

要求:从三个不同服务器中取数,对最近10的历史数据进行去重,写出到文本。

1.读取。

 1 public static DataTable ExecuteSql(string connectString, string querySqlString)
 2         {
 3             DataTable dt = new DataTable();
 4             using (SqlConnection conn = new SqlConnection(connectString))
 5             {
 6                 conn.Open();
 7                 SqlCommand cmd = conn.CreateCommand();
 8                 cmd.CommandText = querySqlString;
 9                 using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
10                 {
11                     adapter.SelectCommand.CommandTimeout = 60 * 30;
12                     adapter.Fill(dt);
13                     return dt;
14                 }
15             }
16         }

当然,另外还有一种,定义了一个我要使用的类型,返回List<T>:

 1 public static List<MarketUrl> GetUrlListFromDataBase(string serverName, string sqlString)
 2         {
 3             List<MarketUrl> list = new List<MarketUrl>();
 4             MarketUrl mu = new MarketUrl();
 5             string connectStr = ConfigurationManager.ConnectionStrings[serverName].ConnectionString;
 6             using (SqlConnection conn = new SqlConnection(connectStr))
 7             {
 8                 conn.Open();
 9                 SqlCommand cmd = conn.CreateCommand();
10                 cmd.CommandText = sqlString;
11                 cmd.CommandTimeout = 60 * 30;
12                 SqlDataReader reader = cmd.ExecuteReader();
13                 while (reader.Read())
14                 {
15                     var dr = new MarketUrl()
16                     {
17                         market = reader.GetString(reader.GetOrdinal("MarketCode")),
18                         url = reader.GetString(reader.GetOrdinal("Url"))
19                     };
20                     list.Add(dr);
21                 }
22             }
23             return list;
24         }

2.去重(我的思路是,在取数的时候,对各自的data select 的时候先进行distinct,然后三个data merge到一起时,再次duplicate):

 

1 //colmuns 是distinct的 数组,为columnName
2 public static DataTable GetDistinctRow(DataTable dt, string[] columns)
3         {
4             DataView dv = dt.DefaultView;
5             DataTable dtDistinct = dv.ToTable(true, columns);
6             return dtDistinct;
7         }

3.DataRows ConvertTo DataTable

 1 //这是个额外用的到操作,先记下
 2 public static DataTable ConvertToDataTable(DataRow[] rows)
 3         {
 4             if (rows == null || rows.Count() < 1)
 5                 return null;
 6             DataTable dtResult = rows[0].Table.Clone();
 7             foreach (DataRow r in rows)
 8             {
 9                 dtResult.Rows.Add(r.ItemArray);
10             }
11             return dtResult;
12         }

4.写入到文本,因code还涉及到别的一些操作,这里只写最简单部分:

 1 StringBuilder sb = new StringBuilder();
 2 foreach (DataRow row in dt.Rows)
 3 {
 4       sb.AppendLine(dateString + " " + row[0].ToString() + " " + row[1].ToString());
 5 }
 6 using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
 7 {
 8      using (TextWriter tw = new StreamWriter(fs, Encoding.Default))
 9      {
10          tw.Write(sb.ToString());
11      }
12 }
13 File.WriteAllText(logPath, "[" + DateTime.UtcNow + "] Successed...\r\n");
14 

 

你可能感兴趣的:(Stream)