Sql server 2008 SqlBulkCopy无法正确存储重音字符
我正在SQL server 2008的nvarchar列中插入法语文本。法语重音字符未正确存储在SQL DB中Sql server 2008 SqlBulkCopy无法正确存储重音字符,sql-server-2008,datatable,sqlbulkcopy,Sql Server 2008,Datatable,Sqlbulkcopy,我正在SQL server 2008的nvarchar列中插入法语文本。法语重音字符未正确存储在SQL DB中 string strData = "Accented chars- Les caractères accentués français "; DataTable dtTemp = new DataTable(); dtTemp.Columns.Add("ID", typeof(string)); dtTemp.Columns.Add("Value", typeof(string));
string strData = "Accented chars- Les caractères accentués français ";
DataTable dtTemp = new DataTable();
dtTemp.Columns.Add("ID", typeof(string));
dtTemp.Columns.Add("Value", typeof(string));
DataRow dr = dtTemp.NewRow();
dr["ID"] = "100";
dr["Value"] = strData;
dtTemp.Rows.Add(dr);
strSQLCon = GetSQLConnectionString();
using (SqlConnection cn = new SqlConnection(strSQLCon))
{
cn.Open();
using (SqlBulkCopy copy = new SqlBulkCopy(cn))
{
copy.ColumnMappings.Add("ID", "ID");
copy.ColumnMappings.Add("Value", "Value");
copy.DestinationTableName = "MYTABLE";
copy.WriteToServer(dtTemp);
}
}
法语字符未正确存储在SQL server数据库中。
当我执行普通的插入查询时,它工作得很好。在MYTABLEvalues中插入(1,“重音字符-法国重音符号”)
请让我知道为什么它不适用于SQL批量复制类。需要更改任何设置或修改C代码以正确存储非英语字符。我在下表中测试了您的代码,它运行良好,至少在SQL Server 2012上是这样
CREATE TABLE [dbo].[MYTABLE](
[ID] [varchar](20) NOT NULL,
[Value] [nvarchar](255) NOT NULL)
我在下表中测试了您的代码,它运行良好,至少在SQLServer2012上是这样
CREATE TABLE [dbo].[MYTABLE](
[ID] [varchar](20) NOT NULL,
[Value] [nvarchar](255) NOT NULL)
我正在设计这个表,每列的排序规则设置为
French_CI_AS
,法国文化,区分重音考虑的每种sql字符串类型
我正在为这个表构建一个类型化数据集(不是这个问题的目的)
Sql批量复制:
var ds = new FrenchCharacters.FrenchDataSet();
using (var destinationConnection = new SqlConnection(StackOverflow.Properties.Settings.Default.StackOverflowConnectionString))
{
destinationConnection.Open();
//all French characters http://en.wikipedia.org/wiki/French_orthography
string[] sArray = new string[] {
"Àà, Ââ, Ææ, Ää"
, "Çç"
, "Îî, Ïï"
, "Ôô, Œœ, Öö"
, "Ùù, Ûû, Üü"
, "Ÿÿ"
};
// open the connection
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection.ConnectionString))
{
bulkCopy.BatchSize = 500;
bulkCopy.NotifyAfter = 10000;
bulkCopy.DestinationTableName = "French";
//
// build data table to be written to the server
// data table is now strongly-typed ds.French
//
for (int i = 0; i < 100; i++)
{
foreach (string s in sArray)
ds.French.AddFrenchRow(s, s, s, s, s, s);
}
//
bulkCopy.WriteToServer(ds.French);
}
}
var ds=new FrenchCharacters.FrenchDataSet();
使用(var destinationConnection=newsqlconnection(StackOverflow.Properties.Settings.Default.StackOverflowConnectionString))
{
destinationConnection.Open();
//所有法语字符http://en.wikipedia.org/wiki/French_orthography
字符串[]sArray=新字符串[]{
"Àà, Ââ, Ææ, Ää"
, "Çç"
, "Îî, Ïï"
, "Ôô, Œœ, Öö"
, "Ùù, Ûû, Üü"
, "Ÿÿ"
};
//打开连接
使用(SqlBulkCopy bulkCopy=newsqlbulkcopy(destinationConnection.ConnectionString))
{
bulkCopy.BatchSize=500;
bulkCopy.NotifyAfter=10000;
bulkCopy.DestinationTableName=“法语”;
//
//生成要写入服务器的数据表
//数据表现在是强类型ds.French
//
对于(int i=0;i<100;i++)
{
foreach(sArray中的字符串s)
D.French.AddFrenchRow(s,s,s,s,s,s,s);
}
//
bulkCopy.WriteToServer(ds.French);
}
}
结果:
请注意,无论sql字符类型如何,都没有无效条目我正在设计此表,每列的排序规则设置为
法语CI\u AS
,法国文化,区分重音考虑的每种sql字符串类型
我正在为这个表构建一个类型化数据集(不是这个问题的目的)
Sql批量复制:
var ds = new FrenchCharacters.FrenchDataSet();
using (var destinationConnection = new SqlConnection(StackOverflow.Properties.Settings.Default.StackOverflowConnectionString))
{
destinationConnection.Open();
//all French characters http://en.wikipedia.org/wiki/French_orthography
string[] sArray = new string[] {
"Àà, Ââ, Ææ, Ää"
, "Çç"
, "Îî, Ïï"
, "Ôô, Œœ, Öö"
, "Ùù, Ûû, Üü"
, "Ÿÿ"
};
// open the connection
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection.ConnectionString))
{
bulkCopy.BatchSize = 500;
bulkCopy.NotifyAfter = 10000;
bulkCopy.DestinationTableName = "French";
//
// build data table to be written to the server
// data table is now strongly-typed ds.French
//
for (int i = 0; i < 100; i++)
{
foreach (string s in sArray)
ds.French.AddFrenchRow(s, s, s, s, s, s);
}
//
bulkCopy.WriteToServer(ds.French);
}
}
var ds=new FrenchCharacters.FrenchDataSet();
使用(var destinationConnection=newsqlconnection(StackOverflow.Properties.Settings.Default.StackOverflowConnectionString))
{
destinationConnection.Open();
//所有法语字符http://en.wikipedia.org/wiki/French_orthography
字符串[]sArray=新字符串[]{
"Àà, Ââ, Ææ, Ää"
, "Çç"
, "Îî, Ïï"
, "Ôô, Œœ, Öö"
, "Ùù, Ûû, Üü"
, "Ÿÿ"
};
//打开连接
使用(SqlBulkCopy bulkCopy=newsqlbulkcopy(destinationConnection.ConnectionString))
{
bulkCopy.BatchSize=500;
bulkCopy.NotifyAfter=10000;
bulkCopy.DestinationTableName=“法语”;
//
//生成要写入服务器的数据表
//数据表现在是强类型ds.French
//
对于(int i=0;i<100;i++)
{
foreach(sArray中的字符串s)
D.French.AddFrenchRow(s,s,s,s,s,s,s);
}
//
bulkCopy.WriteToServer(ds.French);
}
}
结果:
请注意,无论sql字符类型如何,都没有无效条目谢谢您的回复。在大容量插入之前将csv文件读入数据表时,似乎会出现此问题。我在读取csv文件时包含了编码参数。(Encoding.Default)并正确加载法语文本,并将其存储在SQL DB中,不会出现任何问题 旧代码: List lstData=File.ReadAllLines(stFile.ToList() 工作代码: List lstData=File.ReadAllLines(stFile,Encoding.Default).ToList() 谢谢
Ashok谢谢您的回复。在大容量插入之前将csv文件读入数据表时,似乎会出现此问题。我在读取csv文件时包含了编码参数。(Encoding.Default)并正确加载法语文本,并将其存储在SQL DB中,不会出现任何问题 旧代码: List lstData=File.ReadAllLines(stFile.ToList() 工作代码: List lstData=File.ReadAllLines(stFile,Encoding.Default).ToList() 谢谢
Ashok打开dr[“Value”]=strData,进入代码;并验证数据是否仍包含非英语字符?也许C#代码中的某些编码错误。欢迎来到StackOverflow!请看,这里的共识是“不,他们不应该”。通过打破dr[“Value”]=strData,进入您的代码;并验证数据是否仍包含非英语字符?也许C#代码中的某些编码错误。欢迎来到StackOverflow!请看,共识是“不,他们不应该”。