[英]trying to read some data from a csv file c#
Im trying to read some data from a csv file and display it in c# this works fine but i have 2 different rows in my csv file which i'll be adding too. 我试图从csv文件中读取一些数据并将其显示在c#中,效果很好,但我在csv文件中也有2个不同的行,我也会添加这些行。
I want them to be accessible if say someonet types '1' into the ukNumber field it will pull all of their data. 如果有人在ukNumber字段中键入“ 1”,我希望它们可以访问,它将提取所有数据。
atm no matter what i type it always displays the last line in my csv file. atm不管我输入什么,它总是在csv文件中显示最后一行。
namespace Appraisal
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ukNumber_TextChanged(object sender, EventArgs e)
{
}
public void search_Click(object sender, EventArgs e)
{
using (var reader = new StreamReader(File.OpenRead("C:\\Users\\hughesa3\\Desktop\\details.csv"),
Encoding.GetEncoding("iso-8859-1")))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string idStr = values[0];
string firstnameStr = values[0];
string surnameStr = values[0];
string jobroleStr = values[0];
string salaryStr = values[0];
richTextBox1.Text = "Name: " + values[1] + "\nSurname: " + values[2] + "\nJob Role: " + values[3] + "\nSalary: £" + values[4];
}
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void apprasialCode_TextChanged(object sender, EventArgs e)
{
}
private void apprasialBtn_Click(object sender, EventArgs e)
{
}
private void ukNumberLabel_Click(object sender, EventArgs e)
{
}
}
}
There is a CsvHelper available via Nuget 通过Nuget有一个CsvHelper
https://www.nuget.org/packages/CsvHelper/ https://www.nuget.org/packages/CsvHelper/
See following question: using csvhelper (nuGET) with C# MVC to import CSV files 看到以下问题: 将csvhelper(nuGET)与C#MVC一起导入CSV文件
to read data from csv file: 从csv文件读取数据:
using (var reader = new StreamReader(File.OpenRead("c:/yourfile.csv"),
Encoding.GetEncoding("iso-8859-1")))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';'); // replace ';' by the your separator
string idStr = values[0];
string firstnameStr = values[0];
string surnameStr = values[0];
string jobroleStr = values[0];
string salaryStr = values[0];
//convert string
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.