vshost.exe已停止在ExecuteReader上运行

时间:2013-05-26 09:57:03

标签: c# visual-studio-2008 oledb executereader

当我尝试运行程序时遇到问题,当我在第一次循环时,它工作正常,但是在第二次循环中

  

“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();

    }


}

2 个答案:

答案 0 :(得分:0)

我已经用这种方式重写了你的代码

//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文件的尺寸可能有问题。

答案 1 :(得分:0)

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