文本中有很多行,这只是其中一小部分。
年 月 日
1 12 3 17 22 0 0.0
2 12 3 18 4 0 0.0
3 12 10 19 12 59 44.0
13 12 3 18 12 0 0.0
16 12 3 18 11 59 44.0
17 02 3 18 11 59 44.0
36 02 12 18 11 59 44.0
年 月 日
1 12 3 17 22 0 0.0
2 12 3 18 4 0 0.0
3 12 10 19 12 59 44.0
13 12 3 18 12 0 0.0
16 12 3 18 11 59 44.0
17 02 3 18 11 59 44.0
36 02 12 18 11 59 44.0
然后根据拼接的日期去 检索数据库中是否有数据 有则 进入下一行读取 没则Insert
HttpFileCollection files = context.Request.Files;
HttpPostedFile file = files[0];
int FileLen = file.ContentLength;
byte[] input = new byte[FileLen];
System.IO.Stream uploadStream = file.InputStream;
uploadStream.Read(input, 0, FileLen);
uploadStream.Position = 0;
System.IO.StreamReader sr = new System.IO.StreamReader(uploadStream, Encoding.GetEncoding("gb2312"));
while ((line = sr.ReadLine()) != null)
{
string s = line.Trim();
string sLine = System.Text.RegularExpressions.Regex.Replace(s, @" +", " ");
if (sLine != "" && sLine.Split(' ').Length > 0)
{
string[] arrayList =sLine.Split(' ');
string date = DateTime.Parse(arrayList[1] + "-" + arrayList[2] + "-" + arrayList[3]).ToString("yyyy-MM-dd");
///
/// 数据库操作 没有则进行Insert
///
}
}
没有顺序要求,可以用HashSet<DateTime>
要生成数组,用ToArray扩展方法
string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));//读取tx
string pattern = @"(?i)(?<=\d+ +)(\d+\s+){3}";
string[] ss = Regex.Matches(tempStr, pattern).Cast<Match>().Select(a => a.Value).Distinct().ToArray();
/*
* [0] "12 3 17 " string
[1] "12 3 18 " string
[2] "12 10 19 " string
[3] "02 3 18 " string
[4] "02 12 18 " string
*/