我正在创建一个通过三层体系结构从.csv文件读取的应用程序。我已经编写了一个代码,可以读取.csv数据并将其存储到数据链接层的列表中。但我在业务逻辑中遇到问题,或者我什至不知道从哪里开始。下面是我的代码
using System;
using System.Collections.Generic;
using System.IO;
namespace Data_Layer
{
class dataConnection
{
static void Main(string[] args)
{
}
public class CardsList
{
public string cardTypeCode { get; set; }
public string cardTypeName { get; set; }
public string issuingBank { get; set; }
public int cardNumber { get; set; }
public string cardHoldersName { get; set; }
public int cvvCvv2 { get; set; }
public DateTime issueDate { get; set; }
public DateTime expiryDate { get; set; }
public DateTime billingDate { get; set; }
public int cardPin { get; set; }
public int creditLimit { get; set; }
}
class DataConnection
{
public List<CardsList> ReadDataFile()
{
List<CardsList> cardRecords = new List<CardsList>();
string[] path = File.ReadAllLines(@"C:\Users\tevin\Desktop\credit_card_records.csv");
foreach (string CardsList in path)
{
if ((CardsList != "") && !CardsList.EndsWith("Code"))
{
string[] columns = CardsList.Split(',');
CardsList holder = new CardsList();
holder.cardTypeCode = Convert.ToString(columns[0]);
holder.cardTypeName = Convert.ToString(columns[1]);
holder.issuingBank = Convert.ToString(columns[2]);
holder.cardNumber = Convert.ToInt32(columns[3]);
holder.cardHoldersName = Convert.ToString(columns[4]);
holder.cvvCvv2 = Convert.ToInt32(columns[5]);
holder.issueDate = DateTime.ParseExact(columns[6], "M/yyyy-dd", null);
holder.expiryDate = DateTime.ParseExact(columns[7], "M/yyyy-dd", null);
holder.billingDate = DateTime.ParseExact(columns[8], "M/yyyy-dd", null);
holder.cardPin = Convert.ToInt32(columns[9]);
holder.creditLimit = Convert.ToInt32(columns[10]);
cardRecords.Add(holder);
}
else { }
}
return cardRecords;
}
}
}
}
读取.csv文件时,请确保代码正确。
我修改了您的代码,可以尝试。
using System;
using System.Collections.Generic;
using System.IO;
namespace Data_Layer
{
class dataConnection
{
static void Main(string[] args)
{
DataConnection test = new DataConnection();
test.ReadDataFile();
}
/// <summary>
/// Model
/// </summary>
public class CardsList
{
public string cardTypeCode { get; set; }
public string cardTypeName { get; set; }
public string issuingBank { get; set; }
public int cardNumber { get; set; }
public string cardHoldersName { get; set; }
public int cvvCvv2 { get; set; }
public DateTime issueDate { get; set; }
public DateTime expiryDate { get; set; }
public DateTime billingDate { get; set; }
public int cardPin { get; set; }
public int creditLimit { get; set; }
}
class DataConnection
{
public List<CardsList> ReadDataFile()
{
List<CardsList> cardRecords = new List<CardsList>();
string[] path = File.ReadAllLines(@"C:\Users\tevin\Desktop\credit_card_records.csv");
foreach (string CardsList in path)
{
if ((CardsList != "") && !CardsList.EndsWith("Code"))
{
string[] columns = CardsList.Split(',');
CardsList holder = new CardsList();
holder.cardTypeCode = Convert.ToString(columns[0]);
holder.cardTypeName = Convert.ToString(columns[1]);
holder.issuingBank = Convert.ToString(columns[2]);
holder.cardNumber = Convert.ToInt32(columns[3]);
holder.cardHoldersName = Convert.ToString(columns[4]);
holder.cvvCvv2 = Convert.ToInt32(columns[5]);
holder.issueDate = DateTime.ParseExact(columns[6], "M/yyyy-dd", null);
holder.expiryDate = DateTime.ParseExact(columns[7], "M/yyyy-dd", null);
holder.billingDate = DateTime.ParseExact(columns[8], "M/yyyy-dd", null);
holder.cardPin = Convert.ToInt32(columns[9]);
holder.creditLimit = Convert.ToInt32(columns[10]);
cardRecords.Add(holder);
}
else { }
}
return cardRecords;
}
}
}
}