【发布时间】:2020-12-10 01:38:03
【问题描述】:
我正在尝试允许用户在 excel 文件中以 "GameLength" 的形式输入一列(单元格 B2),然后删除前面和末尾的双引号,然后生成一列对于数据库为 GameLength
我可以毫无问题地将列更新为 GameLength。
CREATE TABLE [dbo].[123](
[PTIKeyId] [integer] IDENTITY(1,1) NOT NULL,
[Game Number] [int] NULL,
[Game Length] [int] NULL
CONSTRAINT [PK_123] PRIMARY KEY CLUSTERED
(
[PTIKeyId] ASC
) ON [PRIMARY])
当我使用代码循环数据表行时,我看到了这个错误:
Column '"Game Length"' does not belong to table .
这是失败的代码行:
我的项目文本似乎是正确的,但数据表可能无法处理它?
我有什么选择?
我真的很想从目标字段中删除双引号,基本上让代码运行,而不告诉最终用户转到文件并在继续之前删除双引号。
我似乎无法弄清楚为什么它在数据表中找不到 "GameLength"?
这是循环数据表行的foreach循环:
SourceFields 是来自数据表的列
foreach (DataRow row in dataTableResults.Rows)
{
sbInsert.Clear();
// create start insert statement with the proper columns that will be used.
sbInsert.AppendFormat(CultureInfo.CurrentCulture, "INSERT INTO [{0}].[{1}]", schemaName, tableName).AppendLine();
sbInsert.Append("(");
foreach (var item in destinationFields.Where(w => w != PrimaryKeyId))
{
sbInsert.AppendFormat(CultureInfo.CurrentCulture, "[{0}], ", item);
}
// set to - 2 because we are adding ', ' (comma then a space for readability)
sbInsert.Remove(sbInsert.Length - 2, 1);
// Now create the actual values
sbInsert.Append(")").AppendLine();
sbInsert.Append("VALUES").AppendLine();
sbInsert.Append("(");
foreach (var item in sourceFields)
{
var cellValue = row[item];
var dataMappingRecord = dataMappings.Where(w => w.SourceFieldName == item).FirstOrDefault();
sbInsert.AppendFormat(CultureInfo.CurrentCulture, "{0}", GetCellValue(dataMappingRecord, cellValue, dataTypes));
}
// set to - 2 because we are adding ', ' (comma then a space for readability)
sbInsert.Remove(sbInsert.Length - 2, 1);
sbInsert.Append(");");
}
我如何填充 SourceFields:
private async Task<IList<DataMappingDTO>> GenerateCreateNewTableDataMappings(Workbook workbook, FileDetailDTO fileDetail)
{
var sourceFields = await GetSourceFields(workbook, fileDetail, false).ConfigureAwait(false);
foreach (var item in sourceFields)
{
// add code to remove any double quotes in the column name for the destination file
string destinationFieldUpdated = item.ColumnName.Replace("\"", string.Empty);
if (fileDetail.DataMappings.Any(a => a.SourceFieldName.ToLower(CultureInfo.CurrentCulture) == item.ColumnName.ToLower(CultureInfo.CurrentCulture)) == false)
{
var mapping = new DataMappingDTO
{
Index = item.ColumnIndex,
ImportDetailId = fileDetail.ImportDetailId,
ImportData = true,
SourceFieldName = item.ColumnName,
DestinationFieldName = destinationFieldUpdated,
DataType = item.DataType,
Precision = item.Precision ?? null,
ColumnLength = item.DataType == GetDataTypeList().Where(w => w.ImporterDataTypes == ImporterDataTypes.StringDT).Select(s => s.Id).FirstOrDefault() ? 255 : (int?)null,
Format = GetFormatTypeFromSourceField(item)
};
mapping.UpdateModifiedInfo(UserRequestingId);
fileDetail.DataMappings.Add(mapping);
}
}
return fileDetail.DataMappings;
}
【问题讨论】:
-
如何填充 soucefields 集合?您还注意到
Game Length中有一个空格吗?是预期的吗? -
@ChetanRanpariya 我添加了源字段的代码。我现在确实看到了空间!我在文件中没有看到它!让我再次检查代码并调试。
-
@ChetanRanpariya:我发布了我的答案!谢谢!