我希望能够遍历CSV文件中的所有记录,并将所有好记录添加到一个集合中,并分别处理所有“坏”记录。我似乎无法做到这一点,我认为我必须遗漏一些东西。
如果我尝试捕获BadDataException,则后续读取将失败,这意味着我无法继续读取文件的其余部分 -
while (true)
{
try
{
if (!reader.Read())
break;
var record = reader.GetRecord<Record>();
goodList.Add(record);
}
catch (BadDataException ex)
{
// Exception is caught but I won't be able to read further rows in file
// (all further reader.Read() result in same exception thrown)
Console.WriteLine(ex.Message);
}
}
讨论的另一个选项是设置BadDataFound回调操作来处理它 -
reader.Configuration.BadDataFound = x =>
{
Console.WriteLine($"Bad data: <{x.RawRecord}>");
};
然而,尽管回调被称为坏记录仍然在我的“好名单”
中有什么方法可以在将记录添加到我的列表之前查询读者以查看记录是否合适?
对于此示例,我的记录定义是 -
class Record
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
并且数据(第一行坏,第二行好) -
"Jo"hn","Doe",43
"Jane","Doe",21
有趣的是,使用MissingFieldException处理缺少的字段似乎完全按照我的意愿运行 - 抛出异常,但后续行仍然可以读取。
答案 0 :(得分:5)
这是我提供的example。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace PeaceGate
{
public partial class ScheduledAppointments : Form
{
string constring = ConfigurationManager.ConnectionStrings["myDatabaseConnection"].ConnectionString;
MySqlDataAdapter mdap;
DataSet ds;
public ScheduledAppointments()
{
InitializeComponent();
toolStripStatusLabel1.Text = "Connected as Gate";
}
private void ScheduledAppointments_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'visitor_managerDataSet1.scheduled_appointments' table. You can move, or remove it, as needed.
this.scheduled_appointmentsTableAdapter.Fill(this.visitor_managerDataSet1.scheduled_appointments);
}
private void confirmAppointmentToolStripMenuItem_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
id.Text = dataGridView1.SelectedRows[0].Cells[0].Value + string.Empty;
visit_time.Text = dataGridView1.SelectedRows[0].Cells[1].Value + string.Empty;
visit_date.Text = dataGridView1.SelectedRows[0].Cells[2].Value + string.Empty;
fullname.Text = dataGridView1.SelectedRows[0].Cells[3].Value + string.Empty;
visitor_address.Text = dataGridView1.SelectedRows[0].Cells[4].Value + string.Empty;
visitor_city.Text = dataGridView1.SelectedRows[0].Cells[5].Value + string.Empty;
visitor_telephone.Text = dataGridView1.SelectedRows[0].Cells[6].Value + string.Empty;
id_method.Text = dataGridView1.SelectedRows[0].Cells[7].Value + string.Empty;
organization.Text = dataGridView1.SelectedRows[0].Cells[8].Value + string.Empty;
visit_type.Text = dataGridView1.SelectedRows[0].Cells[9].Value + string.Empty;
reason.Text = dataGridView1.SelectedRows[0].Cells[10].Value + string.Empty;
person_visit.Text = dataGridView1.SelectedRows[0].Cells[11].Value + string.Empty;
pictureBox1.Image = byteArrayToImage((byte[])dataGridView1.CurrentRow.Cells[12].Value);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
}
private Image byteArrayToImage(byte[] byteArray)
{
MemoryStream ms = new MemoryStream(byteArray);
Image img = Image.FromStream(ms);
return img;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
MySqlConnection con = new MySqlConnection(constring);
string sql = "select * from scheduled_appointments where id ='"+id.Text+"'";
mdap = new MySqlDataAdapter(sql,con);
MySqlCommandBuilder cmbl = new MySqlCommandBuilder(mdap);
mdap.Update(ds, "final_appointments");
MessageBox.Show("Appointment Confirmed!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}