我有一個包含表名和數據的 .csv 文件。我需要構建它。這是我的代碼問題是列表不包含定義'Length'
class Program
{
public static string fileName = @".\file.csv";
static void Main(string[] args)
{
List<string> lines = fileReader(fileName);
Console.WriteLine("Insert INTO " + lines[0].Trim(',') + " (" + lines[1] + ")");
Console.WriteLine("VALUES");
var reciept_num = new List<string>();
var reciept_date = new List<string>();
var reciept_total = new List<string>();
var reciept_payment = new List<string>();
var reciept_change = new List<string>();
var item_sold = new List<string>();
var item_price = new List<string>();
var item_quantity = new List<string>();
var item_total = new List<string>();
這是錯誤。'Length'
請幫我解決這個問題。
for (int i = 1; i < lines.Length; i++)
{
string[] col = lines[i].Split(',');
reciept_num.Add(col[0]);
reciept_date.Add(col[1]);
reciept_total.Add(col[2]);
reciept_payment.Add(col[3]);
reciept_change.Add(col[4]);
item_sold.Add(col[5]);
item_price.Add(col[6]);
item_quantity.Add(col[7]);
item_total.Add(col[8]);
}
Console.ReadLine();
}
這是我的 csv 文件的文件閱讀器。我認為這個讀取 csv 文件的代碼很好。
public static List<string> fileReader(string filePath)
{
List<string> lines = new List<string>();
try
{
using (StreamReader sr = new StreamReader(filePath))
{
string line = "";
while ((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
}
}
catch (Exception e)
{
Console.WriteLine("Error Message: Please close the file and try again");
}
return lines;
}
}
我想這就是 Daniel Mann 想說的。
for (int i = 1; i <lines.Count; i++)
使用Count
屬性。而不是Length
財產。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句