在 VB6 中填充 ADODB 命令二进制参数

发布于 2024-08-16 14:42:13 字数 1128 浏览 5 评论 0原文

我需要修改别人的VB代码,而且我对VB6没有太多经验。 我需要使用 ADODB 调用 SQL2000 存储过程。其中一个参数是 Binary 类型,它给我带来了问题。每次创建参数时,都会收到错误“应用程序在当前操作中使用了错误类型的值”。错误发生在 cmd.parameter.append 行,它甚至没有给我调用 cmd.execute 的机会。

Dim HexPassword As String
Dim BinPassword As String
Dim AsciiCode As Integer
Dim unitDigit As String
Dim TensDigit As String

Set obj_hash = New EDCrypt

' Returns Hash of password hex encoded
HexPassword = obj_hash.GetTextHash(Trim(txtPassword.text), haSHA1)

' Converts Hex Encoded string to Binary encoded string
Dim i As Integer


For i = 1 To 40 Step 2
    unitDigit = Mid(HexPassword, i + 1, 1)
    TensDigit = Mid(HexPassword, i, 1)
    AsciiCode = HexStrtoInt(TensDigit + unitDigit)
    BinPassword = BinPassword + Chr(AsciiCode)
Next i

conn.Open ConnectionString
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc

cmd.CommandText = "ValidatePasswordNew"
cmd.Parameters.Append cmd.CreateParameter("LoginID", adVarChar, adParamInput, 30, UserID)
cmd.Parameters.Append cmd.CreateParameter("ShaPassword", adBinary, adParamInput, 20, BinPassword)

I need to modify someone else's VB code, and I don't have much experience with VB6.
I need to call a SQL2000 stored procedure using ADODB. One of the parameters is of type Binary and it's giving me problems. Every time I create the parameter, I get an error "Application uses a value of the wrong type for the current operation". The error happens at the cmd.parameter.append line, it doesn't even give me a chance call the cmd.execute.

Dim HexPassword As String
Dim BinPassword As String
Dim AsciiCode As Integer
Dim unitDigit As String
Dim TensDigit As String

Set obj_hash = New EDCrypt

' Returns Hash of password hex encoded
HexPassword = obj_hash.GetTextHash(Trim(txtPassword.text), haSHA1)

' Converts Hex Encoded string to Binary encoded string
Dim i As Integer


For i = 1 To 40 Step 2
    unitDigit = Mid(HexPassword, i + 1, 1)
    TensDigit = Mid(HexPassword, i, 1)
    AsciiCode = HexStrtoInt(TensDigit + unitDigit)
    BinPassword = BinPassword + Chr(AsciiCode)
Next i

conn.Open ConnectionString
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc

cmd.CommandText = "ValidatePasswordNew"
cmd.Parameters.Append cmd.CreateParameter("LoginID", adVarChar, adParamInput, 30, UserID)
cmd.Parameters.Append cmd.CreateParameter("ShaPassword", adBinary, adParamInput, 20, BinPassword)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以可爱出名 2024-08-23 14:42:13

尝试这样的字符串连接:

BinPassword = BinPassword & ChrB(AsciiCode)

+ 不是正确的字符串连接运算符,使用 ChrB 应该让 VB 和 ADO 相信您实际上传递的是二进制数据,而不是字符数据。

DOK 关于尺寸的判断也有可能是正确的。您可以尝试将大小设置为您确定比您需要的长的值,或者您可以通过将操作分成两个语句来绕过设置大小:

cmd.Parameters.Append cmd.CreateParameter("ShaPassword", adBinary, adParamInput)
cmd.Parameters("ShaPassword").Value = BinPassword

这很奇怪,但它过去对我有用。

只是为了强调以防有人再次看到此情况:Parameter 对象上的 size 属性不必与参数的确切字节长度匹配;它只需至少足够大来支持你的论点。

Try this for your string concatenation:

BinPassword = BinPassword & ChrB(AsciiCode)

+ is not the right string concatenation operator, and using ChrB should convince VB and ADO that you're really passing it binary data, not character data.

There's also a chance DOK is right about the size. You can try setting the size to something you're certain is longer than you need, or you can probably get around setting the size at all by splitting the operation into two statements:

cmd.Parameters.Append cmd.CreateParameter("ShaPassword", adBinary, adParamInput)
cmd.Parameters("ShaPassword").Value = BinPassword

It's weird, but it's worked for me in the past.

Just for added emphasis in case someone sees this again: the size property on Parameter objects does not have to match the exact byte length of your argument; it merely has to be at least big enough to hold your argument.

少年亿悲伤 2024-08-23 14:42:13

我认为问题可能在于设置 size = 20。这可能是一个可选参数。尝试忽略它。您可能需要在此处使用不同的 CreateParameter 重载。

查看此 MSDN 页面,其中提供了此内容一般模式:

command.CreateParameter (Name, Type, Direction, Size, Value)

此外,您已将 BinPassword 声明为字符串。您不能将字符串传递给 adBinary 的参数。您需要将一个二进制对象传递给其中,或者将 adBinary 更改为 adVarChar。

I think the problem may be in setting size = 20. That may be an optional argument. Try leaving it out. There might be a different overload of CreateParameter that you need to use here.

Check out this MSDN page which gives this general pattern:

command.CreateParameter (Name, Type, Direction, Size, Value)

Also, you have declared BinPassword as a String. You can't pass a string into a parameter of adBinary. You need to pass a binary object into that, or change adBinary to adVarChar.

眸中客 2024-08-23 14:42:13

您可能需要 adVarBinary。您很可能应该将字节数组作为值传递。

You probably want adVarBinary. Most likely you should be passing Byte arrays as values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文