c# - SQL将unicode编码的varbinary转换为字符串

标签 c# sql encoding varbinary

好的,我必须导出 REG_BINARY 值并将它们存储在 SQL 中,稍后我需要将这些值转换为字符串..

我有什么:

在 c# 中,我将 REG_BINARY 读取为 Byte[] 数据类型,例如:

RegistryKey rk = Registry.CurrentUser.OpenSubKey...

byte[] value = rk.GetValue("TheKey")

然后我使用以下方法将它存储到 SQL VarBinary 字段中:

        private void SqlStorePstFileInfo(List<PersonalFolderObject> listOfPstObjects, int? userComputerId)
    {
        using (var sqlConnection = new SqlConnection(GetConnectionString()))
        {
            sqlConnection.Open();
            foreach (PersonalFolderObject pfo in listOfPstObjects)
            {
                string s = "INSERT INTO [InvPstFile] (UserComputerId, Name, Path, Size, OldRegBinary) VALUES(@UserComputerId, @Name, @Path, @Size, @OldRegBinary)";
                using (SqlCommand sqlCmd = new SqlCommand(s, sqlConnection))
                {
                    SqlParameter pUserComputerId = sqlCmd.Parameters.Add("@UserComputerId", SqlDbType.Int);
                    pUserComputerId.Value = userComputerId;
                    SqlParameter pName = sqlCmd.Parameters.Add("@Name", SqlDbType.NVarChar);
                    pName.Value = pfo.Name;
                    SqlParameter pPath  = sqlCmd.Parameters.Add("@Path", SqlDbType.NVarChar);
                    pPath.Value = pfo.OldPath;
                    SqlParameter pSize = sqlCmd.Parameters.Add("@Size", SqlDbType.Int);
                    pSize.Value = pfo.Size;
                    SqlParameter pOldRegBinary = sqlCmd.Parameters.Add("@OldRegBinary", SqlDbType.VarBinary);
                    pOldRegBinary.Value = pfo.OldRegBinary;
                    sqlCmd.ExecuteNonQuery();
                }

                //string s = string.Format("INSERT INTO [InvPstFile] (UserComputerId, Name, Path, Size) " +
                //                         "VALUES ('{0}','{1}','{2}','{3}')",
                //                         userComputerId, pfo.Name, pfo.OldPath, pfo.Size);

                //Helpers.Logging.Log.Info(string.Format("SQL: {0}", s));
                //var sqlCmd = new SqlCommand(s, sqlConnection);
                //sqlCmd.ExecuteNonQuery();
            }
            sqlConnection.Close();
        }
    }

当我使用 SQL Management Studio 读取存储值时:

select oldregbinary from InvPstFile where PersonalFolderId = 73 

我得到:

0x0000000038A1BB1005E5101AA1BB08002B2A56C200006D737073742E646C6C00000000004E495441F9BFB80100AA0037D96E0000000043003A005C00550073006500720073005C00630068007200690073007400690061006E002E006D007500670067006C0069005C0044006F00630075006D0065006E00740073005C004F00750074006C006F006F006B002000460069006C00650073005C006F006E0065006D006F0072006500730061006D0070006C0065002E007000730074000000

当我尝试使用 CAST(AboveString as VARCHAR(max)) 将其转换为字符串时,我没有得到任何结果(显示)。原因是unicode编码。当我替换/删除所有 00 时,我得到:

0x38A1BB15E5101AA1BB082B2A56C26D737073742E646C6C4E495441F9BFB801AA37D96E433A5C55736572735C63687269737469616E2E6D7567676C695C446F63756D656E74735C4F75746C6F6F6B2046696C65735C6F6E656D6F726573616D706C652E707374

当我将该值转换为 VARCHAR(MAX) 时,我得到: 8¡»å¡»+*VÂmspst.dllNITAù¿ª7ÙnC:\Users\MYREMOVEPROFILENAME\Documents\Outlook Files\onemoresample.pst

那么,有什么想法可以将 unicode 编码值转换/转换为字符串吗?

(我知道您可以解码上面的字符串以找到替换的 MYREMOVEPROFILENAME - 这不值得付出努力;))

最佳答案

您应该在特定的 unicode Text.Encoding 上使用 GetString() 方法将其转换回字符串。将列从阅读器转换为 byte[],然后使用 GetString() 将其转换为字符串。

string value;
using (SqlCommand command = new SqlCommand("select * from InvPstFile", sqlConnection))
{
    SqlDataReader sdr = command.ExecuteReader();
    //Assuming we are just reading 1 row here
    sdr.Read();
    var bytes = (byte[])sdr["OldRegBinary"];

    // Based on the original unicode format one of the following should 
    // give you the string value
    value = System.Text.Encoding.UTF8.GetString(bytes);
    value = System.Text.Encoding.Unicode.GetString(bytes);
    value = System.Text.Encoding.Default.GetString(bytes);
}

关于c# - SQL将unicode编码的varbinary转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17074859/

相关文章:

php - mysql where 子句基于另一个查询

mysql - sql中使用select子句获取记录的问题

java - Base64 和 MimeUtility 之间的不同结果

encoding - 将字符串转换为 base64

c# - 在 .NET 4.5 中使用 HttpClient 进行编码

c# - 构造函数中的实例化或字段定义中的实例化有什么区别?

c# - 如何在域服务上调用调用操作?

c# - 文本框上的 asp.net 范围验证器

c# - 在 C# 中移动一个字符串

mysql - 限制一列只接受 2 个值