Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文本或richtext字段读取unicode字符_C#_.net_Sql Server_Ado.net - Fatal编程技术网

C# 从文本或richtext字段读取unicode字符

C# 从文本或richtext字段读取unicode字符,c#,.net,sql-server,ado.net,C#,.net,Sql Server,Ado.net,我正在尝试解决如何将文本字段或富文本字段中的奇数unicode字符读取并存储到字符串中,然后在SQL数据库中进行更新 我想我已经解决了SQL方面的问题,但是我无法解决用户输入方面的问题 我在表单上有一个文本框或富文本框(两者都尝试过),用户可以在其中输入 输入到字段中,他们可以输入并将其放入ok,但是如何检索它以发送到SQL 字符串变量只是将其转换为那么,.NET从一开始就是unicode,因此应该很简单 确保数据库列排序规则也是unicode(类型包括nvarchar或仅支持unicode字符

我正在尝试解决如何将文本字段或富文本字段中的奇数unicode字符读取并存储到字符串中,然后在SQL数据库中进行更新

我想我已经解决了SQL方面的问题,但是我无法解决用户输入方面的问题

我在表单上有一个文本框或富文本框(两者都尝试过),用户可以在其中输入 输入到字段中,他们可以输入并将其放入ok,但是如何检索它以发送到SQL


字符串变量只是将其转换为那么,.NET从一开始就是unicode,因此应该很简单

确保数据库列排序规则也是unicode(类型包括
nvarchar
或仅支持unicode字符的排序规则本身)


当您提供代码示例以便更容易发现问题时,它通常会有所帮助。

嗯,.NET从一开始就是unicode的,所以应该很简单

确保数据库列排序规则也是unicode(类型包括
nvarchar
或仅支持unicode字符的排序规则本身)


当您提供代码示例以便更容易发现问题时,它通常会有所帮助。

我将以下示例放在一起,以帮助您发现代码中可能存在的问题,以及无法将unicode字符串保存到表中的原因

此示例将unicode文本保存到
dbo.MyTable
表中,该表具有以下架构:

CREATE TABLE dbo.MyTable
(
    MyColumn NVARCHAR(MAX)
)
此逻辑将使用
textbox1.text
中的文本在
dbo.MyTable
表中插入一条记录:

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();
    }
}
更新 根据您在下面的评论,我对您的代码做了以下更改,即围绕
SqlCommand
使用参数而不是字符串连接,我解释了为什么这不好;下面的代码将按预期将unicode文本保存到表中,并且是安全的:

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();
}

我将下面的示例放在一起,以帮助您发现代码中可能存在的问题,以及它没有将unicode字符串保存到表中的原因

此示例将unicode文本保存到
dbo.MyTable
表中,该表具有以下架构:

CREATE TABLE dbo.MyTable
(
    MyColumn NVARCHAR(MAX)
)
此逻辑将使用
textbox1.text
中的文本在
dbo.MyTable
表中插入一条记录:

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();
    }
}
更新 根据您在下面的评论,我对您的代码做了以下更改,即围绕
SqlCommand
使用参数而不是字符串连接,我解释了为什么这不好;下面的代码将按预期将unicode文本保存到表中,并且是安全的:

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();
}
一个简单的方法是:

byte[] bytes = Encoding.Unicode.GetBytes(unicodeString);
一个简单的方法是:

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

从使用RichTextBox的Text属性切换到Rtf属性。它达到了我想要的结果


感谢大家

从使用RichTextBox的Text属性切换到Rtf属性。它达到了我想要的结果


谢谢大家

谢谢,我的专栏是NVARCHAR(MAX),我的代码是:SqlCommand ins=new-SqlCommand(“更新产品测试规范集规范=”“+myRichText.Text+”,其中ProductID=“+ProductID+”和TestID=“+TestID,con”);请尝试{ins.ExecuteNonQuery();}catch(Exception ex){result=ex.Message.ToString();},但它仍然保存,而SQL中没有“特殊”字符…Hi@Dean。请按照我的示例,在查询中使用参数(for、Specification、ProductID和TestID),这将解决问题。您刚才向我展示的代码对SQL注入攻击非常敏感!我在另一个答案中写到了这一点:同时,解释为什么代码要去除unicode字符;在SQL server中,
NVARCHAR
类型文字前面必须加上
N
,例如
N'My unicode text'
。当连接
myRichText.Text
时,您没有在那里执行此操作,因此,SQL server将其转换为
VARCHAR
类型。再次强调,请不要这样做,而是使用参数,这样您会很安全,您的代码将按预期工作。谢谢,我理解参数化查询中的逻辑,这更有意义。。。但是仍然没有正确保存值。它存储在数据库中,因为我想出了另一种方法来做它。。。如果我使用myRichText.Rtf值,那么我可以得到我要的reuslt。。。.Text属性不包含super/sub脚本中的值…谢谢,我的列是NVARCHAR(MAX),我的代码是:SqlCommand ins=new-SqlCommand(“更新产品测试规范集规范=”“+myRichText.Text+”,其中ProductID=“+ProductID+”和TestID=“+TestID,con”);请尝试{ins.ExecuteNonQuery();}catch(Exception ex){result=ex.Message.ToString();},但它仍然保存,而SQL中没有“特殊”字符…Hi@Dean。请按照我的示例,在查询中使用参数(for、Specification、ProductID和TestID),这将解决问题。您刚才向我展示的代码对SQL注入攻击非常敏感!我在另一个答案中写到了这一点:同时,解释为什么代码要去除unicode字符;在SQL server中,
NVARCHAR
类型文字前面必须加上
N
,例如
N'My unicode text'
。当连接
myRichText.Text
时,您没有在那里执行此操作,因此,SQL server将其转换为
VARCHAR
类型。再次强调,请不要这样做,而是使用参数,这样您会很安全,您的代码将按预期工作。谢谢,我理解参数化查询中的逻辑,这更有意义。。。但是仍然没有正确保存值。它存储在数据库中,因为我想出了另一种方法来做它。。。如果我使用myRichText.Rtf值,那么我可以得到我要的reuslt。。。.Text属性不包含超级/子脚本中的值……您不应该在2019年推荐
ntext
。它从2005年起就被弃用了……我没有注意到sql-s