非英文字符未从文件插入到 C# 应用程序中的 SQL 服务器

Non English characters not inserting from file to SQL Server in C# application

我们有这个table

CREATE TABLE [dbo].[PackageStatus] 
(
    [ID]                   INT            IDENTITY (1, 1) NOT NULL,
    [PackageStatusName]    NVARCHAR (200) NOT NULL,
    [PackageStatusName_FR] NVARCHAR (200) NULL,
    [IsActive]             BIT            
        CONSTRAINT [DF_PackageStatus_IsActive] DEFAULT ((0)) NOT NULL,
    [DisplayName]          NVARCHAR (200) 
        CONSTRAINT [DF_PackageStatus_DisplayName] DEFAULT ('') NOT NULL,
    [DisplayName_FR]       NVARCHAR (200) 
        CONSTRAINT [DF_PackageStatus_DisplayName_FR] DEFAULT ('') NOT NULL,
    CONSTRAINT [PK_PackageStatus] PRIMARY KEY CLUSTERED ([ID] ASC)
);

PackageStatus.sql 包含一些法语值的合并脚本

SET IDENTITY_INSERT [dbo].[PackageStatus] ON 

MERGE [dbo].[PackageStatus] AS T
USING ( VALUES 
    (1, N'New', N'En transit', 1, N'In Transit', N'En transit'),
    (2, N'Received', N'Prêt pour le ramassage', 1, N'Ready for Pickup', N'Prêt pour le ramassage'),
    (3, N'Received Damaged', N'Prêt pour le ramassage', 1, N'Ready for Pickup', N'Prêt pour le ramassage'),
    (4, N'Scheduled', N'Programmé', 1, N'Scheduled', N'Programmé')
) AS S ([ID], [PackageStatusName], [PackageStatusName_FR], [IsActive], [DisplayName], [DisplayName_FR])
ON T.[ID] = S.[ID]
WHEN MATCHED
    THEN UPDATE 
        SET 
            T.[PackageStatusName] = S.[PackageStatusName],
            T.[PackageStatusName_FR] =  S.[PackageStatusName_FR],
            T.[IsActive] = S.[IsActive],
            T.[DisplayName] =  S.[DisplayName],
            T.[DisplayName_FR] = S.[DisplayName_FR]         
WHEN NOT MATCHED BY TARGET
    THEN INSERT ([ID], [PackageStatusName], [PackageStatusName_FR], [IsActive], [DisplayName], [DisplayName_FR])
        VALUES ([ID], [PackageStatusName], [PackageStatusName_FR], [IsActive], [DisplayName], [DisplayName_FR])
WHEN NOT MATCHED BY SOURCE
    THEN DELETE;

SET IDENTITY_INSERT [dbo].[PackageStatus] OFF

如果我们 运行 来自 SQL Server Management Studio 的脚本,它会按预期工作。

但是当我们使用 C# 和 运行 SQL 从应用程序端使用 Entity Framework 核心读取该文件内容时

 string sql = File.ReadAllText(sourceFilePath);
 DbContext.Database.ExecuteSqlRaw(sql);

我们看到不需要的字符

如果我们使用硬编码查询,一切都符合预期

string sql = @"
    SET IDENTITY_INSERT [dbo].[PackageStatus] ON 
    MERGE [dbo].[PackageStatus] AS T
    USING ( VALUES 
        (1, N'New', N'En transit', 1, N'In Transit', N'En transit'),
        (2, N'Received', N'Prêt pour le ramassage', 1, N'Ready for Pickup', N'Prêt pour le ramassage'),
        (3, N'Received Damaged', N'Prêt pour le ramassage', 1, N'Ready for Pickup', N'Prêt pour le ramassage'),
        (4, N'Scheduled', N'Programmé', 1, N'Scheduled', N'Programmé')
    ) AS S ([ID], [PackageStatusName], [PackageStatusName_FR], [IsActive], [DisplayName], [DisplayName_FR])
    ON T.[ID] = S.[ID]
    WHEN MATCHED
        THEN UPDATE 
            SET 
                T.[PackageStatusName] = S.[PackageStatusName],
                T.[PackageStatusName_FR] =  S.[PackageStatusName_FR],
                T.[IsActive] = S.[IsActive],
                T.[DisplayName] =  S.[DisplayName],
                T.[DisplayName_FR] = S.[DisplayName_FR]         
    WHEN NOT MATCHED BY TARGET
        THEN INSERT ([ID], [PackageStatusName], [PackageStatusName_FR], [IsActive], [DisplayName], [DisplayName_FR])
            VALUES ([ID], [PackageStatusName], [PackageStatusName_FR], [IsActive], [DisplayName], [DisplayName_FR])
    WHEN NOT MATCHED BY SOURCE
        THEN DELETE;
    SET IDENTITY_INSERT [dbo].[PackageStatus] OFF
";
DbContext.Database.ExecuteSqlRaw(sql);

我们尝试过编码,但没有任何效果

 string sql = File.ReadAllText(sourceFilePath, Encoding.UTF8);

有什么办法可以解决这个问题吗?

尝试

string sql = File.ReadAllText(sourceFilePath, Encoding.GetEncoding("ISO-8859-1"));