vshost.exe已停止在ExecuteReader上工作

本文关键字:ExecuteReader 工作 exe vshost | 更新日期: 2023-09-27 18:15:17

我有问题,当我试图运行我的程序,当我在我的第一个循环,它工作,但在第二个循环

"vshost.exe已停止工作"

显示

错误,当我调试它时,错误出现在我的ExecuteReader()上。有人能帮我一下吗?

这是我的代码的第一部分:

//从这里开始

public void ConvertToText(string _fileUrl, string _fileName) {

    //The connection string to the excel file
    string connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _fileUrl + ";Extended Properties=Excel 12.0;";
    //The query
    string strSQL = "SELECT * FROM [Sheet1$]";
    //The connection to that file
    using(OleDbConnection conn = new OleDbConnection(connstr))

    //The command 
    using (OleDbCommand cmd = new OleDbCommand(strSQL, conn))
    {
        conn.Open();
        DataTable dt = new DataTable();
        try
        {

            string extension = System.IO.Path.GetExtension(_fileName);
            string result = _fileName.Substring(0, _fileName.Length - extension.Length);
            using (OleDbDataReader dr1 = cmd.ExecuteReader())
            {
                StreamWriter sw = new StreamWriter(@"C:'Users'jhrnavarro'Documents'From SIr Boo'GBOC'Activation'Destination'" + result + ".txt");
                if (dr1.Read())
                {
                    dt.Load(dr1);
                }
                int iColCount = dt.Columns.Count;
                for (int i = 0; i < iColCount; i++)
                {
                    sw.Write("'" + dt.Columns[i] + "'");
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);
                // Now write all the rows.
                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < iColCount; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            sw.Write("'" + dr[i].ToString() + "'");
                        }
                        if (i < iColCount - 1)
                        {
                            sw.Write(",");
                        }
                    }
                    sw.Write(sw.NewLine);
                }
                sw.Close();
                Console.WriteLine("File is saved");
            }
        }
        catch (OleDbException caught)
        {
            Console.WriteLine(caught.Message);
        }
        finally
        {
            conn.Close();
        }
        Console.Read();
    }

}

vshost.exe已停止在ExecuteReader上工作

我已经这样重写了你的代码

//The connection string to the excel file
string connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _fileUrl + ";Extended Properties=Excel 12.0;";
//The query
string strSQL = "SELECT * FROM [Sheet1$]";
//The connection to that file
using(OleDbConnection conn = new OleDbConnection(connstr))
using(OleDbCommand cmd = new OleDbCommand(strSQL, conn))
{
    conn.Open();
    DataTable dt = new DataTable();
    try
    {
        using(OleDbDataReader dr1 = cmd.ExecuteReader())
        {
            dt.Load(dr1);
        }
        string result = Path.GetFileNameWithoutExtension(_fileName);
        string outFile = Path.Combine(@"C:'Users'Administrator'Desktop'GBOC'Activation'Destination", result + ".txt");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < dt.Columns.Count - 1; i++)
            sb.AppendFormat("'{0}',", dt.Columns[i].ColumnName);
        sb.Length--;
        sb.AppendLine();
        foreach(DataRow r in dt.Rows)
        {
            for (int i = 0; i < dt.Columns.Count - 1; i++)
                sb.AppendFormat("'{0}',", r[i].ToString());
            sb.Length--;
            sb.AppendLine();
        }
        using(StreamWriter sw = new StreamWriter(outFile))
            sw.Write(sb.ToString());
     }
     catch(Exception ex)
     {
        MessageBox.Show(ex.Message);
     }
}         

我已经改变了使用简单的GetFileNameWithoutExtension构建输出文件名的方式。然后,我在代码中添加了适当的using statement,以有效地处理数据库访问和文件写入中涉及的对象。
最后,我使用StringBuilder创建缓冲区,首先写入列名,然后写入输出文件中按行提取的数据,仅使用一次调用。(并删除了最后一列的内部检查)

警告:不检查行中的空值。您的Excel文件的尺寸可能有问题。

您是否尝试在程序崩溃的位置放置一个断点,然后用F11和Locals窗口遍历其余代码?这在过去帮助了我很多次。