简体   繁体   中英

Read unicode characters from text or richtext field

I am trying to work out how to read and store odd unicode characters from a text field or rich text field into a string to then update in an SQL database.

I think I have the SQL side of things sorted out however I can't work out the user input side.

I have a text box or rich text box (tried both) on a form where the user inputs 参考附件 into the field, and they can input put it ok, but how do I retrieve it to send to SQL?

A string variable just converts it to <104cfu/g.

Any ideas?

I put the following example together to help you spot the issue that you may have in your code, and the reason why it isn't saving the unicode string into your table.

This example will save unicode text into the dbo.MyTable table, which has the following schema:

CREATE TABLE dbo.MyTable
(
    MyColumn NVARCHAR(MAX)
)

And this logic will insert a record into the dbo.MyTable table with the text from textbox1.Text :

using (var cn = new SqlConnection("MyConnectionString"))
{
    cn.Open();

    using (var cm = cn.CreateCommand())
    {
        cm.CommandType = CommandType.Text;
        cm.CommandText = "INSERT dbo.MyTable (MyColumn) VALUES (@MyText)";

        // Assuming textBox1 is your textbox...
        cm.Parameters.Add(
            new SqlParameter("@MyText", SqlDbType.NVarChar, -1) { Value = textBox1.Text }
            );

        cm.ExecuteNonQuery();
    }
}

UPDATE

Based on your comment below, I made the following changes to your code around the SqlCommand to use parameters instead of string concatenation, I explained why this is bad here ; The code below will save your unicode text into your table as expected and will be safe:

SqlCommand ins = new SqlCommand("UPDATE ProductTestSpecifications set Specification = @Specification where ProductID = @ProductID and TestID = @TestID", con);

// When building your SqlCommand, always use parameters if you are interacting with external input, this will protect you against SQL injection.
ins.Parameters.Add("@Specification", SqlDbType.NVarChar, -1).Value = myRichText.Text;

// Im assuming ProductID and TestID are System.Int32, if not, please change SqlDbType.Int to the appropriate type.
ins.Parameters.Add("@ProductID", SqlDbType.Int).Value = ProductID;
ins.Parameters.Add("@TestID", SqlDbType.Int).Value = TestID;

try
{
    ins.ExecuteNonQuery();
}
catch (Exception ex)
{
    result = ex.Message.ToString();
}

Switch from using the Text property of the RichTextBox to the Rtf property. It achieves the result I am after...

Thanks all

Well, .NET is unicode from the beginning, so it should be straightforward.

Make sure your database column collation is unicode as well (types such nvarchar or just collation itself that supports unicode characters).

It usually helps when you provide a sample of your code so that it's easier to find a problem.

一种简单的方法是:

byte[] bytes = Encoding.Unicode.GetBytes(unicodeString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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