C# 仅读取第一行的csv文件
我试图通过csv上传一系列客户信息,我在登录时遇到了一些问题,但我之前的帖子得到了回复,所以我能够开始读取数据,但它只读取第一行。只是想知道有没有人有什么想法。我已经包括了下面的代码C# 仅读取第一行的csv文件,c#,string,csv,C#,String,Csv,我试图通过csv上传一系列客户信息,我在登录时遇到了一些问题,但我之前的帖子得到了回复,所以我能够开始读取数据,但它只读取第一行。只是想知道有没有人有什么想法。我已经包括了下面的代码 private void btnUpload_Click(object sender, EventArgs e) { //Browse for file OpenFileDialog ofd = new OpenFileDialog(); //Onl
private void btnUpload_Click(object sender, EventArgs e)
{
//Browse for file
OpenFileDialog ofd = new OpenFileDialog();
//Only show .csv files
ofd.Filter = "Microsoft Office Excel Comma Separated Values File|*.csv";
DialogResult result = ofd.ShowDialog();
//If the user selects a valid file
if (result == DialogResult.OK)
{
//File is delimited by a comma
char[] laClientDelim = { ',' };
//New object for string manipulation
objStringManipulation = new StringManipulation();
// Parse the csv file
List<string[]> lsClientList = objStringManipulation.parseCSV(ofd.FileName, laClientDelim);
foreach (string[] laClient in lsClientList)
{
//Create new object for manipulating the database
objSqlCommands = new SqlCommands("Client", "ClientName");
string[] records = File.ReadAllLines(ofd.FileName); // read the file completely line by line
char splitChar = ',';
int splitCharCount = 0;
int k = 0;
string[] fields = records[k].Split(splitChar); // reads all the single values per line. 'splitChar' should be the delimiter.
splitCharCount++;
if (splitCharCount >= 4 && splitCharCount <= 10)
{
var stuff = from l in File.ReadLines(ofd.FileName)
let x = l.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.Select(s => char.Parse(s))
select new
{
Client = x,
ClientName = x
};
}
//Inserts the client info into datbase
objSqlCommands.sqlCommandInsertorUpdate("INSERT", records[k]);//laClient[0]);
k++;
//Refreshs the Client table on display from the
this.clientTableAdapter.Fill(this.kIIDImplementationCalcDataSet.Client);
//MAKE SURE TO ONLY ADD IN CLIENT AND CLIENT NAME
//update the view
dgvClientlst.Update() ;
}
}
}
private void btnUpload\u单击(对象发送方,事件参数e)
{
//浏览文件
OpenFileDialog ofd=新建OpenFileDialog();
//仅显示.csv文件
ofd.Filter=“Microsoft Office Excel逗号分隔值文件|*.csv”;
DialogResult结果=ofd.ShowDialog();
//如果用户选择了有效的文件
if(result==DialogResult.OK)
{
//文件由逗号分隔
char[]laClientDelim={',};
//用于字符串操作的新对象
objStringManipulation=新StringManipulation();
//解析csv文件
List lsClientList=objStringManipulation.parseCSV(ofd.FileName,laClientDelim);
foreach(lsClientList中的字符串[]laClient)
{
//创建用于操作数据库的新对象
OBJSQLComands=新的SqlCommands(“客户端”、“客户端名称”);
string[]records=File.ReadAllLines(ofd.FileName);//逐行完整读取文件
char splitChar=',';
int splitCharCount=0;
int k=0;
string[]fields=records[k].Split(splitChar);//读取每行的所有单个值。“splitChar”应为分隔符。
splitCharCount++;
if(splitCharCount>=4&&splitCharCount char.Parse)
选择新的
{
Client=x,
ClientName=x
};
}
//将客户端信息插入数据库
sqlcommands.sqlCommandInsertorUpdate(“插入”,记录[k]);//laClient[0]);
k++;
//从中刷新显示的客户端表
this.clientTableAdapter.Fill(this.kIIDImplementationCalcDataSet.Client);
//确保只添加客户端和客户端名称
//更新视图
dgvClientList.Update();
}
}
}
您的循环基本上如下所示:
foreach (string[] laClient in lsClientList)
{
int k = 0;
string[] records = File.ReadAllLines(ofd.FileName);
string[] fields = records[k].Split(splitChar);
k++;
}
每个laClient的“k”值都不会超过0。您需要对每一行进行内部循环。我知道我的答案并不完全符合您的要求,但如果您将.csv文件转换为.xls文件,则可以非常轻松地对其进行操作。我已经多次这样做了,如果您愿意,我可以为您提供代码说明。扩展Jonesy的答案(因为我还不能评论),在循环外声明变量
k
。每次都会被重置为零
int k = 0;
foreach (string[] laClient in lsClientList)
{
string[] records = File.ReadAllLines(ofd.FileName);
string[] fields = records[k].Split(splitChar);
k++;
}
我认为你们理解变量的范围。变量K是在for循环中定义的,因此在每个循环中一次又一次地实例化为0。在循环外声明K。将K放在循环外修复了仅获取第一行以外的内容的问题,但现在我面临的问题是select不起作用,因此我获取的是所有数据,而不是指定的substringDuh。需要循环foreach块中的记录和行。正如我所证明的,一个人经验丰富,决不会犯错误。