Jos*_*hee 1 c# sql-server string
对于类型的SQL Server列
VARCHAR(100)
在插入列之前,我想确保插入的值不大于100. 该数字是指定它可以存储的最大字符数,还是它可以存储的字符数据的字节数?
我问的原因是一些unicode特殊字符使用多个字节.因此,以unicode编码的100个字符串可能占用超过100个字节.
由于varchar是ASCII编码,任何ASCII字符是否可能占用多个字节(可能需要检查字节长度)?
(编辑:根据我对问题的反馈,我看到varchar应该用于ASCII和nvarcharunicode.)
将GetByteCount用于适当的编码器 - 在本例中为VarChar的ASCII和NVarChar的Unicode).
var s = "This is a string";
var len1 = s.Length;
var len2 = System.Text.Encoding.Unicode.GetByteCount(s);
var len3 = System.Text.Encoding.ASCII.GetByteCount(s);
Console.WriteLine("'{0}' has {1} characters and is {2} bytes with Unicode encoding and {3} bytes with ASCII encoding.", s, len1, len2, len3);
Run Code Online (Sandbox Code Playgroud)
输出:
'This is a string' has 16 characters and is 32 bytes with Unicode encoding and 16 bytes with ASCII encoding.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
535 次 |
| 最近记录: |