将字节数组转换为vbscript中的字符串值

时间:2022-03-21 16:09:39

I have a logTable in my database for log changes on someTable.

我的数据库中有一个logTable,用于someTable上的日志更改。

logTable has three columns:

logTable有三列:

first for store column name which value was changed;
second for old value;
third for new value.

第一个用于存储列名称,其值已更改;旧价值的第二;第三是新价值。

Second and third column has sql_variant data type.

第二列和第三列具有sql_variant数据类型。

In my program I get data from this table and oldValue and newValue are an arrays of bytes.

在我的程序中,我从这个表中获取数据,oldValue和newValue是一个字节数组。

For example:

例如:

rows in logTable:

logTable中的行:


columnName(nvarchar(50)) oldValue(sql_variant)  newValue(sql_variant)

compName    name1       name2
address  "address1"  "address2"
zip      123         134
phone      123456789   987654321

But I get oldValue and newValue values in byte arrays.

但我在字节数组中得到oldValue和newValue值。

Like:

喜欢:


address "byte array"   "byte array"
zip     "byte array"   "byte array"
phone   "byte array"   "byte array"

instead:

代替:


address  "address1" "address2"
zip     123         134
phone    123456789   987654321

This code I use to get data from DB:

我用这个代码从DB获取数据:


Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open connStr
Set rsData = Server.CreateObject("ADODB.Recordset")
Set objComm = Server.CreateObject("ADODB.Command")
Set objComm.ActiveConnection= Conn
objComm.CommandText="usp_Log_GetOperationDetails"
objComm.CommandType = adCmdStoredProc 
objComm.Parameters.Append objComm.CreateParameter("@CompanyId",adInteger,adParamInput)
objComm.Parameters("@CompanyId")=Request("CompanyId")
objComm.Parameters.Append objComm.CreateParameter("@tableName",adVarChar,adParamInput, 50)
objComm.Parameters("@tableName")=Request("tableName")
rsData.CursorLocation = adUseClient
rsData.CursorType = adOpenStatic
Set rsData.Source = objComm
rsData.Open

If NOT rsData.EOF AND NOT rsData.BOF then
   Do While NOT rsData.EOF
    rsDataOldValue = rsData("OldValue")
    rsDatanewValue = rsData("NewValue")
        rsData.MoveNext
   Loop
End If

rsData.Close
Set rsData = Nothing
Conn.Close
Set Conn = Nothing
Set objComm = Nothing

How can I convert byte array to correct string value?

如何将字节数组转换为正确的字符串值?

Thanks

谢谢

1 个解决方案

#1


1  

Have you tried casting in your SQL query ?

您是否尝试过在SQL查询中进行投射?

SELECT CAST(oldvalue AS varchar(100)) from logTable

Edit: if the cast wont do good then you need to write a function like this one which converts the bytes to a string one at a time (note : I have not tested this as I am not currently on Windows)

编辑:如果演员不会做好,那么你需要编写一个像这样的函数,一次将字节转换为一个字符串(注意:我没有测试过这个,因为我目前不在Windows上)

function ByteArrayToString(theArray)
    dim i, str

    if vartype(theArray) < 8192 then
           exit function
    end if
    for i=lbound(theArray) to ubound(theArray) 
       str = str & asc(theArray(i))
    next
    ByteArrayToString = str
end function 

To test it try

试试吧

MsgBox(ByteArrayToString(rsDataOldvalue))

#1


1  

Have you tried casting in your SQL query ?

您是否尝试过在SQL查询中进行投射?

SELECT CAST(oldvalue AS varchar(100)) from logTable

Edit: if the cast wont do good then you need to write a function like this one which converts the bytes to a string one at a time (note : I have not tested this as I am not currently on Windows)

编辑:如果演员不会做好,那么你需要编写一个像这样的函数,一次将字节转换为一个字符串(注意:我没有测试过这个,因为我目前不在Windows上)

function ByteArrayToString(theArray)
    dim i, str

    if vartype(theArray) < 8192 then
           exit function
    end if
    for i=lbound(theArray) to ubound(theArray) 
       str = str & asc(theArray(i))
    next
    ByteArrayToString = str
end function 

To test it try

试试吧

MsgBox(ByteArrayToString(rsDataOldvalue))