简体   繁体   中英

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"

error was shown, when I debugged it the error was on my 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) {

    //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 . 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.
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. (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.

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? That has helped me quite a few times in the past.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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