SqlBulkCopy Unicode/UTF8 error

c# nvarchar special-characters sqlbulkcopy

Question

I am saving records with UTF8 encoded multilingual data using the SqlBulkCopy feature in C#. My database field's name is NVARCHAR(max), and the value that has to be stored is ae'è a. But it is being saved as "???????." Can this precise Japanese/Multilingual value be saved throughSqlBulkCopy .

row["ParameterValue"] = RegData.ParamValue;

I also examined and debugged the line above. However, the data is flowing in just well over the line. I'm running this line after it.

bulkCopy.WriteToServer(dataTable);

(I tested this with a basic database insert, and it worked well.) But I need to preserve millions of documents at once. Simple database inserts are undesirable for me.

1
3
5/26/2014 11:22:12 AM

Accepted Answer

'??????? ' is the outcome I'd anticipate from:

select cast(N'我多言語で我' as varchar(max)) 

attaching to aNVARCHAR column through a recently constructedDataTable with astring column seems to function for me...

A SQL profiler trace should reveal an error.insert bulk ... You won't see the values in the statement using the datatype used for the bulk dataset.

If both the target field and this are accurate,NVARCHAR type, maybe some server-side processing is taking place?

Edit: I find that the following works.

    // insert code to create SqlConnection
    var dbName = string.Format("Test{0}", Environment.TickCount);
    var command = connection.CreateCommand();
    command.CommandText = string.Format("create database {0}", dbName);
    command.ExecuteNonQuery();
    command.CommandText = string.Format("use {0}", dbName);
    command.ExecuteNonQuery();
    try
    {
        command.CommandText = "create table Test (Value nvarchar(max))";
        command.ExecuteNonQuery();

        var dataTable = new DataTable();
        dataTable.Columns.Add("Value", typeof(string));

        var row = dataTable.NewRow();
        row["Value"] = "我多言語で我";
        dataTable.Rows.Add(row);

        var sqlBulkCopy = new SqlBulkCopy(connection);
        sqlBulkCopy.DestinationTableName = "Test";
        sqlBulkCopy.WriteToServer(dataTable);

        Console.WriteLine("Please check the following query:");
        Console.WriteLine(string.Format("select * from {0}..Test", dbName));
        Console.ReadKey();                
    }
    finally
    {
        command.CommandText = string.Format("drop database {0}", dbName);
        command.ExecuteNonQuery();
    }
3
5/26/2014 1:47:40 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow