FoxPro OLE-DB ドライバーを使用して、FoxPro データベースから Sql Server データベースにデータをインポートしています。私が取っているアプローチは、FoxPro テーブルをループし、すべてのレコードを DataTable に選択してから、SqlBulkCopy を使用してそのテーブルを Sql Server に挿入することです。次のエラーが発生するいくつかのインスタンスを除いて、これは正常に機能します。
System.InvalidOperationException: The provider could not determine the Decimal value. For example, the row was just created, the default for the Decimal column was not available, and the consumer had not yet set a new Decimal value.
これを調査し、表示される行をログに記録しました。問題は、FoxPro テーブルの数値の幅が固定されていることです。1 は 1.00 として保存されますが、10 は 10.0 として保存され、問題の原因となっているのは小数点以下 1 桁です。問題を見つけたので、修正に苦労しています。次の関数は、OLEDBReader を DataTable に変換するために使用しているものです。
private DataTable FPReaderToDataTable(OleDbDataReader dr, string TableName)
{
DataTable dt = new DataTable();
//get datareader schema
DataTable SchemaTable = dr.GetSchemaTable();
List<DataColumn> cols = new List<DataColumn>();
if (SchemaTable != null)
{
foreach (DataRow drow in SchemaTable.Rows)
{
string columnName = drow["ColumnName"].ToString();
DataColumn col = new DataColumn(columnName, (Type)(drow["DataType"]));
col.Unique = (bool)drow["IsUnique"];
col.AllowDBNull = (bool)drow["AllowDBNull"];
col.AutoIncrement = (bool)drow["IsAutoIncrement"];
cols.Add(col);
dt.Columns.Add(col);
}
}
//populate data
int RowCount = 1;
while (dr.Read())
{
DataRow row = dt.NewRow();
for (int i = 0; i < cols.Count; i++)
{
try
{
row[((DataColumn)cols[i])] = dr[i];
}
catch (Exception ex) {
if (i > 0)
{
LogImportError(TableName, cols[i].ColumnName, RowCount, ex.ToString(), dr[0].ToString());
}
else
{
LogImportError(TableName, cols[i].ColumnName, RowCount, ex.ToString(), "");
}
}
}
RowCount++;
dt.Rows.Add(row);
}
return dt;
}
私がしたいのは、小数点以下1桁の問題がある値をチェックすることですが、これらの場合、データリーダーからまったく読み取ることができません。問題のある行で dr.GetString(i) を使用できたはずですが、これにより次のエラーが返されます。
The provider could not determine the String value. For example, the row was just created, the default for the String column was not available, and the consumer had not yet set a new String value.
列がこれを許可していないため、FoxPro データを更新できません。DataReader からレコードを読み取って修正するにはどうすればよいですか? キャスト/ dr.GetValue / dr.GetDataのすべての組み合わせを試しましたが、すべて同じエラーのバリエーションがあります。
FoxPro テーブルの構造は次のとおりです。
Number of data records: 1664
Date of last update: 11/15/10
Code Page: 1252
Field Field Name Type Width Dec Index Collate Nulls Next Step
1 AV_KEY Numeric 6 Asc Machine No
2 AV_TEAM Numeric 6 No
3 AV_DATE Date 8 No
4 AV_CYCLE Numeric 2 No
5 AV_DAY Numeric 1 No
6 AV_START Character 8 No
7 AV_END Character 8 No
8 AV_SERVICE Numeric 6 No
9 AV_SYS Character 1 No
10 AV_LENGTH Numeric 4 2 No
11 AV_CWEEKS Numeric 2 No
12 AV_CSTART Date 8 No
** Total ** 61
問題を引き起こしているのは av_length 列です