所以我正在编写一个 C# 控制台应用程序。我有一个要发送到数据库的文本文件。计划是有多个文本文件,只有一个插入。第一行似乎一切顺利。一旦我到达第二行,数组就认为它的长度只有 2。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UKImporter
{
class Program
{
static void Main(string[] args)
{
string[] lines = System.IO.File.ReadAllLines(@"C:\out\output.txt");
System.Console.WriteLine("Contents of writeLines2.txt =:");
foreach (string line in lines)
{
string sellername, sku, date1, quantity1, date2, asin, date3, date4, FNSKU;
char[] tabs = { '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t', };
string[] words = line.Split(tabs);
sellername = words[0];
sku = words[1];
date1 = words[2];
quantity1 = words[3];
date2 = words[4];
asin = words[5];
date3 = words[6];
date4 = words[7];
FNSKU = words[8];
Console.WriteLine("\t" + line);
UKDataBLL u = new UKDataBLL();
//u.AddToDatabase(sku, DateTime.Now, Convert.ToInt16(quantity1), DateTime.Now, asin, DateTime.Now, DateTime.Now, FNSKU);
foreach (string s in words)
{
System.Console.WriteLine(s);
}
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
编辑这里是文件的一些文本
A2LQ9QFN82X636 ACD_fivecrowns 6/1/11 5:30 0 6/1/11 5:30 B00000IV35 6/1/11 5:30 6/1/11 5:30 X0000PGTT9
A2LQ9QFN82X63 ACD_caylus_magna_carta 6/1/11 5:30 0 6/1/11 5:30 B000N3SOUM 6/1/11 5:30 6/1/11 5:30 X0000PGM23
A2LQ9QFN82X63 AMX_JrSpaceHelmet-LBL 6/1/11 5:30 0 6/1/11 5:30 B0008F6WMM 6/1/11 5:30 6/1/11 5:30 X0000PQBUL
最佳答案
你只需要
string[] words = line.Split('\t');
然后你必须验证内容和你的操作匹配,一个粗略的想法:
System.Diagnostics.Trace.Assert (words.Count == 9);
关于c# - 奇怪的数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6217929/