1

I'm trying to get the same BLOB beginning with 0x (like 0x12345679....) in C# that i get in VB6... the problem is that the old VB6 program is still used, and i have to create in C# a program that will transform pdf files in BLOB before inserting them into the database... I found the VB6 code (or i hope i found it, because i'm extremely new to VB6), that uses AppendChunk method to import (create and import???) the BLOB...

Dim rs, rs2 As Recordset
Dim arrData() As Byte, strFile As String
Const BlockSize = 10240

srcFile = FreeFile
Dim fullname
fullname = IMPORTFOLDER & strFile  // This is the path + the file

Open fullname For Binary Access Read As srcFile

FileLength = LOF(srcFile)                // Get the total size of the file

NumBlocks = FileLength \ BlockSize       // Set number of chunks
LeftOver = FileLength Mod BlockSize      // Set number of remaining bytes

ReDim arrData(LeftOver)

Get srcFile, , arrData()

rs.AddNew
myDocID = NextDocID()
rs!DocumentId = myDocID

rs("DocumentData").AppendChunk arrData()
ReDim arrData(BlockSize)
For i = 1 To NumBlocks
  Get srcFile, , arrData()
  rs("DocumentData").AppendChunk arrData()
Next i
rs.Update

I tried to use the method from here but it doesn't work... I used NHibernate to automatically create the BLOB and insert it into a database, and seemed that the result i was getting was very close tho the one the old program in VB6 does, but again.. it wasn't the same :( Please, tell me, is it possible to get the same BLOB in C# that i'm getting in VB6, and what would be the code in C#? At least an idea.. please.. Thank You in advance.

1 Answer 1

1

I found the answer to this question. The best way is to use SQL parameters when inserting the BLOB into the database. So first we will need the document to be read as an array of bytes. Next we will show that the parameter to be inserted will be of type "Image" like in the code below:

private void AddDocument(string photoFilePath)
    {
        //GetFileStream() is a custom function that converts the doc into a stream
        byte[] photo = GetFileStream(photoFilePath);
        var conn = new SqlConnection(@"ConnectionString here");
        var updPhoto = new SqlCommand("UPDATE DT_Document "
                   + "SET DocumentData = @Photo WHERE Id=399", conn);
        updPhoto.Parameters.Add("@Photo", SqlDbType.Image, photo.Length).Value = photo;
        conn.Open();
        updPhoto.ExecuteNonQuery();
        conn.Close();
    }

Someone might use a stored procedure. It doesn't really matter. Even if in the question in VB6 the file is uploaded by Blocks, here isn't, but in the end we will have the same result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.