不確定發生了什麼,因爲我之前使用OleDbDataReader
從Excel電子表格中提取數據,但突然間我的C#批處理作業並未通過循環在空行停止。OleDbDataReader從Excel電子表格中讀取空白行
這是我正在使用的C#代碼。我只是試圖將所有記錄從Excel電子表格中取出並循環訪問數據。不過,出於某種原因,無論如何,_reader.Read()
函數都會返回true
。
private static OleDbDataReader _reader;
static void Main(string[] args)
{
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= \\prdhilfs03\l&i-sales&mkt\WORKAREA\Agencyservices\Shared\AIC\Analysts_and_Reporting\Realignments\2014\MassUpdateTesting\Validate\ZipCodeTest.xlsx;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
string queryString = "SELECT * FROM [Query1$]";
Validation validation;
ZipCodeTerritory record;
bool flag = true;
try
{
Stopwatch watch = new Stopwatch();
watch.Start();
using (OleDbConnection connection = new OleDbConnection(connString))
{
//Set connection objects to pull from spreadsheet
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
_reader = command.ExecuteReader();
//Instantiate validation object with zip and channel values
_allRecords = GetRecords();
validation = new Validation();
validation.SetLists(_allRecords);
while (_reader.Read())
{
record = new ZipCodeTerritory();
record.ChannelCode = _reader[0].ToString();
record.DrmTerrDesc = _reader[1].ToString();
record.IndDistrnId = _reader[2].ToString();
record.StateCode = _reader[3].ToString().Trim();
record.ZipCode = _reader[4].ToString().Trim();
record.EndDate = Convert.ToDateTime(_reader[5].ToString());
record.EffectiveDate = Convert.ToDateTime(_reader[6].ToString());
record.LastUpdateId = _reader[7].ToString();
record.ErrorCodes = _reader[8].ToString();
record.Status = _reader[9].ToString();
record.LastUpdateDate = DateTime.Now;
if (string.IsNullOrEmpty(record.LastUpdateId))
{
//Add to error list
_issues++;
_errorList.Add(record, "Missing last update Id");
continue;
}
if (_reader[10] != DBNull.Value)
{
record.Id = Convert.ToInt32(_reader[10]);
}
_errorMsg = validation.ValidateZipCode(record);
if (!string.IsNullOrWhiteSpace(_errorMsg))
{
_errorList.Add(record, _errorMsg);
continue;
}
else if (flag)
{
//Separate updates to appropriate list
SendToUpdates(record);
}
//Console.WriteLine(record.Id.ToString());
}
我發現this question對堆棧溢出,並試圖通過改變queryString
到下面跟隨選擇的答案。但是,當OleDbCommand
運行其.ExecuteReader()
方法時,這隻會引發OleDbException
。 (例外的Message
財產寫着「沒有爲一個或多個必需的參數給出值」)
SELECT * FROM [Query1$] WHERE NOT ([ChannelCode] = '' and [DrmTerrDec] = '' and [StateCode] = '' and [ZipCode] = '' and [EndDate] = '' and [EffectiveDate] = '')
編輯
剛剛添加的while
循環內所有代碼。
兩者可能要粘貼在_reader.Read()使用,而塊中的代碼。 – Sorceri
剛添加到帖子。謝謝 – NealR