c# - 在 ColumName 中带有双引号的 DataTable 列名并在没有它的情况下生成 SQL 列
问题描述
我试图允许用户在 excel 文件中输入列(单元格 B2)作为“GameLength”,然后删除前面和结尾的双引号,然后为数据库生成一个列作为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;
}
解决方案
推荐阅读
- database - 运输区 - 表费率 - 不保存信息
- vue.js - 如何使用 wepack devserver 代理静态文件?
- python - TypeError:“类”对象不可迭代-flask/sqlalchemy/jinja
- python - 在烧瓶应用程序中使用 sqlalchemy 更新 postgresql 记录
- python - python:测试文本字段中是否存在多个字符串以设置新列值
- .net-core - contentrootpath 指向 C:/Program Files/IISExpress
- reactjs - Focus Trap React 和 2 个弹出窗口中的一些组件
- android - 在 Ubuntu 中为 HAXM 获取更多可用内存
- c++ - 如何在 C++ 中编写此伪代码?函数返回两个数组到两个初始化的数组
- dart - 你可以在 Dart 中将扩展方法作为函数参数传递吗?