首页 > 解决方案 > 在 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;
        }

标签: c#datatable

解决方案


谢谢@ChetanRanpariya

开头有个空格:

在此处输入图像描述

我去了excel文档并完全删除了B2中的所有内容。然后我回去把

"Game Length"

一切正常!

再次感谢。


推荐阅读