当我尝试运行我的程序时遇到问题,当我在我的第一个循环中时,它可以工作,但是在第二个循环中
“vshost.exe 已停止工作”
显示错误,当我调试它时,错误出现在我的ExecuteReader()
. 有人可以帮我解决这个问题吗?
这是我的代码的第一部分:
//从这里开始
公共无效转换文本(字符串_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();
}
}