简体   繁体   English

vshost.exe已停止在ExecuteReader上工作

[英]vshost.exe has stopped working on ExecuteReader

I have problem when I'm trying to run my program, When I was on my first loop, it worked, but on the second loop 我在尝试运行程序时遇到问题,当我在第一个循环中运行时,但在第二个循环中运行

"vshost.exe has stopped working" “ vshost.exe停止工作”

error was shown, when I debugged it the error was on my ExecuteReader() . 显示错误,当我对其进行调试时,该错误位于ExecuteReader() can somebody please help me regarding this? 有人可以帮我吗?

here's the first part of my code: 这是我的代码的第一部分:

//start here //从这里开始

public void ConvertToText(string _fileUrl, string _fileName) { 公共无效的ConvertToText(字符串_fileUrl,字符串_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();

    }


}

I have rewritten your code in this way 我已经用这种方式重写了您的代码

//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);
     }
}         

I have changed the way in which you build the output file name using the simple GetFileNameWithoutExtension . 我已经更改了使用简单的GetFileNameWithoutExtension构建输出文件名的方式。 Then I have added the appropriate using statement to your code to effectively dispose the objects involved in the database access and in the file writing. 然后,我在代码中添加了适当的using statement ,以有效地处理数据库访问和文件编写中涉及的对象。
Finally, I have used a StringBuilder to create the buffer to write your first the column names and then the data extracted by your rows in the output file with only one call. 最后,我使用StringBuilder创建缓冲区,以首先写入列名,然后仅通过一次调用就将行中提取的数据写入输出文件中。 (And removed internal checks for the last column) (并删除了最后一列的内部检查)

Caveats: No checking for null values in rows. 注意事项:不检查行中的空值。 The dimension of your Excel file could be a problem. Excel文件的尺寸可能是个问题。

Have you tried placing a breakpoint at the location where the program crashes and then walk through the rest of the code with F11 and the Locals window? 您是否尝试过在程序崩溃的位置放置断点,然后使用F11和“本地”窗口浏览其余的代码? That has helped me quite a few times in the past. 过去,这对我有很多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM