简体   繁体   English

通过SqlBulkCopy保存UTF8字符串c#

[英]Save UTF8 string by SqlBulkCopy c#

I have this Sql query: 我有这个SQL查询:

INSERT INTO myTable (myField) VALUES (N'Thermal Oxide: 0 Å to 40 μm')

and I want to exeute it by SqlBulkCopy like that: 我想像这样通过SqlBulkCopy执行它:

DataTable myDataTable=myDb.getData("select top 1* from myTable").Clone();
DataRow dr = myDataTable.NewRow();  
dr["myField"] ="N'Thermal Oxide: 0 Å to 40μm'";
myDataTable.Rows.Add(dr);
this.openCon();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(con))   {
    bulkCopy.DestinationTableName = dt.TableName;
    bulkCopy.BulkCopyTimeout = 100;
    bulkCopy.WriteToServer(myDataTable);
}   this.closeCon();

It is work perfect, but my question is: where can I initilize the 'N' prefix? 这是完美的工作,但是我的问题是:我可以在哪里初始化'N'前缀? and maybe there is no need? 也许没有必要?

Thanks, chani 谢谢,chani

Just: 只是:

dr["myField"] = "Thermal Oxide: 0 Å to 40μm";

The N'...' is only required for literals in TSQL; N'...'仅对于TSQL中的文字是必需的; but since you are taking data (not TSQL) you do not need to worry about that. 但是由于您要获取数据 (而不是TSQL),因此无需担心。 In the case of SqlBulkCopy , it will go down to the server in a raw format, not directly as TSQL. 对于SqlBulkCopy ,它将以原始格式(而不是直接作为TSQL格式)发送到服务器。

In the more common case of an adapter or ORM, the library will either (most likely) use parameters, or it will worry about the escapting itself. 在更常见的适配器或ORM情况下,该库将(很可能)使用参数,否则将担心转义自身。

It perhaps should also be noted that adding 1 row via SqlBulkCopy is overkill. 也许还应该指出的是,通过SqlBulkCopy添加1行是过大的。 But it should work. 但它应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM