【发布时间】:2016-06-15 23:30:31
【问题描述】:
已取得进展,我不再收到越界错误。我现在收到了我在代码中定义的消息框,告诉用户文件已上传(如下图所示)。但是,当我检查数据库或刷新数据集时,新数据不存在。我觉得这似乎与 try catch 中的连接语句有关,但我不确定。有什么建议吗?
private void newFileToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream myStream = null;
ofd.Title = "Select Text File";
ofd.InitialDirectory = @"C:\";
ofd.Filter = "Text Files (.txt) |*.txt";
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
if ((myStream = ofd.OpenFile()) != null)
{
using (myStream)
{
System.IO.StreamReader reader = new System.IO.StreamReader(myStream);
string text = reader.ReadLine(); //code to read each line word by word and divide it up//
string[] row = text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string c in row)
{
string[] rejects = c.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
string Column1 = rejects[0];
string Column2 = rejects[1];
string Column3 = rejects[2];
string Column4 = rejects[3];
string Column5 = rejects[4];
string Column6 = rejects[5];
string Column7 = rejects[6];
string Column8 = rejects[7];
string Column9 = rejects[8];
string Column10 = rejects[9];
string Column11 = rejects[10];
string Column12 = rejects[11];
string Column13 = rejects[12];
string Column14 = rejects[13];
string Column15 = rejects[14];
string Column16 = rejects[15];
string Column17 = rejects[16];
string Column18 = rejects[17];
string Column19 = rejects[18];
string Column20 = rejects[19];
string Column21 = rejects[20];
string Column22 = rejects[21];
string Column23 = rejects[22];
string Column24 = rejects[23];
string Column25 = rejects[24];
string Column26 = rejects[25];
string Column27 = rejects[26];
string Column28 = rejects[27];
string Column29 = rejects[28];
string Column30 = rejects[29];
try
{
string Column31 = "1";
string Insert = "INSERT INTO tblReject_test (sop_instance_uid, study_instance_uid, accession_number, patient_id, patient_name, date_of_birth, sex, study_date, study_time, mpm_code, body_part_examined, ank_menu_name, kanji_menu_name, s_value, l_value, ip_number, fcr_image_id, technologist, requesting_department, Kanji_requesting_department, size_code, film_mark1, film_mark2, status, technologist_code, ip_number2, reject_category, reject_comment, code, reject_image, department_id) VALUES ('" + Column1 + "," + Column2 + "," + Column3 + "," + Column4 + "," + Column5 + "," + Column6 + "," + Column7 + "," + Column8 + "," + Column9 + "," + Column10 + "," + Column11 + "," + Column12 + "," + Column13 + "," + Column14 + "," + Column15 + "," + Column16 + "," + Column17 + "," + Column18 + "," + Column19 + "," + Column20 + "," + Column21 + "," + Column22 + "," + Column23 + "," + Column24 + "," + Column25 + "," + Column26 + "," + Column27 + "," + Column28 + "," + Column29 + "," + Column30 + "," + Column31 + "')";
//database connection to execute insert command
string connectiontring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Database1.mdb";
DataConnection = new OleDbConnection(connectiontring);
DataConnection.Open();
OleDbCommand cmd = new OleDbCommand(Insert, DataConnection);
database1DataSet.tblReject_test.Clear();
this.tblReject_testTableAdapter.Fill(this.database1DataSet.tblReject_test);
MessageBox.Show("File has been uploaded.");
DataConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
}
}
}
【问题讨论】:
-
您假设您的行可拆分为 31 个元素。但是如果一行的元素少于 31 个呢?没有人检查这个条件,这给出了超出范围异常
标签: c# ms-access import openfiledialog csv