Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 9th, 2007, 10:09 AM
Friend of Wrox
Join Date: Mar 2006
Location: , , Portugal.
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts
Default read datareader and convert binary to string

Hello my friends!

I'm having some problems with binary fields!

I had starter with this script (by Englere): HERE

This application is for asp.net 2.0 and while read from a datareader insert fields into another database.

I want to use it in asp.net 1.1 to create an huge sql file with all insert lines!

I have a schema datatable that tell what type of data all columns have.

If my sqltype is varchar, nvarchar, int, text, etc... my application do:

    Private Shared Function setParamterTypeAndValue(ByVal table As String, _ 
      ByVal dr As SqlDataReader, _
      ByVal col As Integer, _
      ByVal sqlProviderType As String, _
      ByVal colName As String) As String
        Dim result As String = ""
        If (sqlProviderType.ToString = "Int") Or _
        (sqlProviderType.ToString = "SmallInt") Then
            If (dr(col) Is DBNull.Value) Then
                result += "NULL, "
                result += CType(dr(col), String) & ", "
            End If
        ElseIf (sqlProviderType.ToString = "TinyInt") Then
            If (dr(col) Is DBNull.Value) Then
                result += "NULL, "
                result += CType(dr(col), Byte).ToString
            End If
        ElseIf (sqlProviderType.ToString = "VarChar") Then
            If (dr(col) Is DBNull.Value) Then
                result += "NULL, "
                result += "'" & CType(dr(col), String) & "', "
            End If

My big problem is when my sqlType is "binary" !!!!
I have a binary filed on my database with passwords:
(binary / lenght = 20)

I have to do something like:
ElseIf (sqlProviderType.ToString = "Binary") Then
            If (dr(col) Is DBNull.Value) Then
                result += "NULL, "
                result += CType(dr(col), String) & ", "
            End If

But I couldn't use "result += CType(dr(col), String)" when dr(col) is binary!!
I just need to generate a string like :

I spent much time with this without success!
Can anyone tell wath can i do??

  #2 (permalink)  
Old July 9th, 2007, 12:29 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

In my experience, I have never found a way to create a T-SQL command string with binary data in it. However, to satisfy my own curiosity I used a sql data comparison tool on some tables with image type fields and looked at the scripts it generated...

Based on what I'm seeing, you can put the string of hex right in the T-SQL string as-is, like this:

UPDATE table SET field = 0xD62F739DA2796769AF9E727E7905F91A369F44A

So try putting the binary string in the sql command without any quotes around it.

  #3 (permalink)  
Old July 9th, 2007, 01:18 PM
Friend of Wrox
Join Date: Mar 2006
Location: , , Portugal.
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks planoie, but this is my problem!

If I use:
result += CType(dr(col), Byte()).ToString & ", "
result += (dr(col)).ToString & ", "
When i open my txt file i see:
(...)aaaaa', System.Byte[], 'aaaaaa(..)

With the value System.Byte[] I don't do anything!
My question Is, how can i retrieve:
(...)aaaaa', 0xD62F739DA2796769AF9E727E7905F91A369F44A, 'aaaaaa(..)

I hope you understand my doubt!


  #4 (permalink)  
Old July 9th, 2007, 07:14 PM
Friend of Wrox
Join Date: Mar 2006
Location: , , Portugal.
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts

I found a way!

My hosting company don't allow me to acess master db! master.dbo.fn_varbintohexstr solve my problems
I made a script with all the update I need.

If anyone has the same problem!
(I spent much time working on this)
declare @pstrout nvarchar(4000)
    ,@i int
    ,@firstnibble int
    ,@secondnibble int
    ,@tempint int
    ,@hexstring char(16)
    ,@cbytesin int
    ,@startoffset int
    ,@fsetprefix bit
    ,@pbinin varbinary(8000)
    ,@counter int
    ,@curPass varchar(8000)
    ,@curName varchar(15)

set @startoffset = 1
set @fsetprefix = 1
set @cbytesin = 0
set @counter = 0

    ID             int IDENTITY PRIMARY KEY,
    querystr        varchar(600)
    while @counter < (select (max(UserID)) from accounts_Users)
      set @counter = @counter + 1
      set @pbinin = (select password from accounts_users where userid = @counter)
      Set @curName = (select UserName from accounts_users where userid = @counter)
      set @curPass = NULL
      set @pstrout = NULL

    if (@pbinin IS NOT NULL)
        select @i = 0
                ,@cbytesin = case when (@cbytesin > 0) then @cbytesin else DATALENGTH(@pbinin) end
                ,@pstrout =  case when (@fsetprefix = 1) then N'0x' else N'' end
                ,@hexstring = '0123456789abcdef'

        if ( ((@cbytesin * 2) + 2 > 4000) or ((@cbytesin * 2) + 2 < 1) )
            --select NULL
            Set @curPass = NULL

        if ( ( @startoffset > @cbytesin ) or ( @startoffset < 1 ) )
            --select NULL
            Set @curPass = NULL

        -- do for each byte
        while (@i < @cbytesin)
            -- Each byte has two nibbles
            -- which we convert to character
            select @tempint = cast(substring(@pbinin, @i + @startoffset, 1) as int)
            select @firstnibble = @tempint / 16
            select @secondnibble = @tempint % 16

            -- we need to do an explicit cast with substring 
            -- for proper string conversion. 
            select @pstrout = @pstrout +
                cast(substring(@hexstring, (@firstnibble+1), 1) as nvarchar) +
                cast(substring(@hexstring, (@secondnibble+1), 1) as nvarchar)

            select @i = @i + 1

    -- All done
    --select @pstrout
    Set @curPass = @pstrout

    IF (@curPass IS NOT NULL AND @curName IS NOT NULL)
       INSERT INTO #TempUsers (querystr) VALUES
       ('UPDATE FORUMS_ACCOUNTS SET PASSWORD = ' + @curPass + ' WHERE UserID = ' + CONVERT(VARCHAR(4), @counter) + ' AND USERNAME =' + @curName )

SELECT querystr FROM #TempUsers


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert datareader into ArrayList SKhna ASP.NET 2.0 Basics 2 February 26th, 2008 10:48 AM
How to convert image to binary form deardp Python 0 September 26th, 2007 11:12 PM
How to convert a datetime into a binary... gbianchi SQL Language 2 September 10th, 2007 09:43 AM
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
Read Image and Convert to Binary... soccers_guy10 Pro VB.NET 2002/2003 1 September 22nd, 2003 10:03 AM

All times are GMT -4. The time now is 07:25 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.