Solved

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

Posted on 2004-08-05
4
1,311 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now