Solved

How to save binary data to SQL-Server using VB.NET

Posted on 2004-08-05
4
1,313 Views
Last Modified: 2011-08-18
I'm having following problem: In my SQL-Server DB I have a table that has only one column "BytesPassWord" of type varbinary(24). Now, in my VB.NET app (WinForm) I'll create an array of type Byte,  bytPassWord() as Byte with 24 bytes in the array,
after creating the array I'll fill it with some byte values.

How I can save this binary data into my table in SQL-Server ??
0
Comment
Question by:vpalmu
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
AerosSaga earned 250 total points
ID: 11726359
Here ya go:

<form runat="server" enctype="multipart/form-data" ID="Form2">
  <P>
    <input type="file" id="file1" runat="server" NAME="file1">
  </P>
  <P>
    <asp:Button id="btn1" runat="server" text="Upload" onclick="upload" />
  </P>
</form>

--------------------------------------------------------------------------------------

Imports System.Data
Imports System.Data.SqlClient

Public Sub Upload(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim b(file1.PostedFile.InputStream.Length - 1) As Byte

        file1.PostedFile.InputStream.Read(b, 0, file1.PostedFile.InputStream.Length)

        Dim con As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringSQL"))

        Dim sql As String = "INSERT INTO MY_TABLE(MyID, DATABLOB) VALUES(1,@BlobData) "
        Dim cmd As New SqlCommand(sql, con)

        Dim parmBlob As New SqlParameter("@BlobData", SqlDbType.Image, _
                     b.Length, ParameterDirection.Input, False, 0, _
                     0, Nothing, DataRowVersion.Current, b)
                     
        cmd.Parameters.Add(parmBlob)

        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
End Sub

Regards,

Aeros
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11726367
Thats for ASP.NET/VB.NET obviously, but the vb should be the same for forms
0
 
LVL 8

Expert Comment

by:rfgkev
ID: 11733694
you should really use SqlDbType.VarBinary instead of SqlDbType.Image in the sqlparameter thou, since you said it was a varbinary. Won't make much difference either way, just me being picky :)
0
 

Author Comment

by:vpalmu
ID: 11734634
It worked fine, I only had to change the param type to SqlDbType.VarBinary..Thanks !
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question