【问题标题】:Using a OpenfileDialog to open and import a tab delimited text file into an MS Access database in C#使用 OpenfileDialog 打开制表符分隔的文本文件并将其导入 C# 中的 MS Access 数据库
【发布时间】: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


【解决方案1】:

可能当您拆分 c 字符串时,生成的数组 rejects 的元素少于 31 个。

也许数组“行”的末尾有一个空行。

更好地使用重载Split(char[], StringSplitOptions):

string[] row = text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
string[] rejects = c.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);

您可以在查询之前添加一个if 语句检查数组的长度:

string[] row = text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string c in row)
{
    string[] rejects = c.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);

    if (rejects.Length > 30)
    {
        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];
        string Column31 = rejects[30];

        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 + "')";
    }
}

编辑:

在更新的代码中,您正在创建命令cmd,但您尚未执行。你需要调用ExecuteNonQuery()方法:

OleDbCommand cmd = new OleDbCommand(Insert, DataConnection);
cmd.ExecuteNonQuery();

【讨论】:

  • 使用您的代码我不再收到错误消息。我决定在 INSERT 语句下添加另一个消息框以确保代码实际上正在执行某些操作,不幸的是消息框没有出现,但是我没有收到任何错误。但是数据也没有被插入到数据库中。更新后的代码。
  • @iiAaronXiX:我更新了答案,但问题是你没有运行命令。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-08
  • 2019-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多