写入mssql数据乱码

1、出现乱码的场景如下:

------------------------------------------------------------------------------------------------------------------------------------------------------------------

1)c#代码如下:

private int InsertBaseReport(int year, long mainId, Guid? orgId, DateTime repordate, string indictorCode, string dataValue)
{
string insertSql =
$"insert into T_Report_Base{year} (F_MainID,F_OrgID,F_ReportDate,F_IndicatorCode,F_Value,F_LastDateTime) " +
$"values({mainId},‘{orgId}‘,‘{repordate}‘,‘{indictorCode}‘,‘{dataValue}‘,getdate())";
return db.Database.ExecuteSqlCommand(insertSql);
}

2)数据库字段设计F_Value类型varchar(100)

3)结果:F_Value字段出现乱码

2、解决方案:

------------------------------------------------------------------------------------------------------------------------------------------------------------------

1)c#代码修改如下:

private int InsertBaseReport(int year, long mainId, Guid? orgId, DateTime repordate, string indictorCode, string dataValue)
{
string insertSql =
$"insert into T_Report_Base{year} (F_MainID,F_OrgID,F_ReportDate,F_IndicatorCode,F_Value,F_LastDateTime) " +
$"values({mainId},‘{orgId}‘,‘{repordate}‘,‘{indictorCode}‘,N‘{dataValue}‘,getdate())";
return db.Database.ExecuteSqlCommand(insertSql);
}

2)数据库字段设计F_Value类型修改为nvarchar(100)

3)  将控制面板中->区域 设置更改为中文(简体,中国)

相关文章