Save/open OLE object from .mdb in vb winform - vb.net

I am trying to add a feature to my vb winform program where a user can attach files (.doc, .docx, .jpg, .pdf) to a mdb file that holds other text data. The binary file and the file name are stored in the DB. The file name with path is passed as variable 'fpath'. below is what I have thus far (It is now working, meaning it saves the file name and binary data). Now, how can the user open the saved file? And, if it is a .doc or PDF etc, how do I make the default associated program open it? . Can someone help me with the rest?
Here is the code to store the OLE object:
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'data connection
Dim cn As New OleDb.OleDbConnection
cn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & _
Application.StartupPath & "\data.mdb"
cn.Open()
'file name without path
Dim flName As String = filename.Text
'open file from the disk (file path is the path to the file to be opened)
Using fileStream As FileStream = File.OpenRead(fpath)
'create new MemoryStream object
Dim memStream As New MemoryStream()
memStream.SetLength(fileStream.Length)
'read file to MemoryStream
fileStream.Read(memStream.GetBuffer(), 0, CInt(Fix(fileStream.Length)))
Dim strImage As String = "?"
Dim arr As Byte()
arr = memStream.GetBuffer
Dim cmd As New OleDb.OleDbCommand
cmd.Connection = cn
cmd.CommandText = "INSERT INTO tblstudent(name, photo) VALUES( ?, ?)"
cmd.Parameters.Add("#name", OleDbType.Char).Value = flName
cmd.Parameters.Add("#photo", OleDb.OleDbType.Binary).Value = arr
cmd.ExecuteNonQuery()
MsgBox("Data save successfully!")
cn.Close()
End Using
End Sub

After two weeks of searching and trying a bunch of methods, I got it to work. The code above is corrected and working for uploading a file to a .mdb file. The code below will retrieve it. Yes, I know saving files to a mdb is not the best, but there will only be a couple docs or pdf, and I need it all in one file for easy sharing between users.
Private Builder As New OleDbConnectionStringBuilder With _
{ _
.DataSource = IO.Path.Combine(Application.StartupPath & "\data.mdb"), _
.Provider = "Microsoft.Jet.OleDb.4.0" _
}
Private Sub btnGetfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetfile.Click
Dim selfile As String = fileDgv.CurrentCell.Value.ToString
Dim cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
Dim cmd As New OleDbCommand With _
{ _
.Connection = cn, _
.CommandText = "SELECT photo FROM tblstudent WHERE name='" & selfile & "'" _
}
Dim NoDataList As New List(Of String)
Dim dr As OleDbDataReader = Nothing
Dim FileStream As System.IO.FileStream
Dim Reader As OleDbDataReader
Dim Data() As Byte = Nothing
Dim Writer As System.IO.BinaryWriter = Nothing
Dim bufferSize As Integer = 1000
Dim buffer(bufferSize - 1) As Byte
Dim startIndex As Long = 0
Dim numberOfBytes As Long = 0
cn.Open()
Reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
Reader.Read()
FileStream = New System.IO.FileStream(
IO.Path.Combine("C:\temp3", "temp", selfile),
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)
Writer = New System.IO.BinaryWriter(FileStream)
Do
numberOfBytes = Reader.GetBytes(0, startIndex, buffer, 0, bufferSize)
If numberOfBytes = 0 Then
Exit Do
End If
Writer.Write(buffer, 0, CInt(Fix(numberOfBytes)))
startIndex += numberOfBytes
Loop While True
Writer.Flush()
If Writer IsNot Nothing Then
Writer.Close()
End If
If FileStream IsNot Nothing Then
FileStream.Close()
End If
If Reader IsNot Nothing Then
Reader.Close()
End If
cn.Close()
MessageBox.Show("Done")
End Sub

Related

Import and Export csv File from DataGridView

High there, I'm currently making a small piece of software for a college project, The goal of the project is for the user to import ingredients and infomration and then they can create meals with these ingredients.
I'm having an issue though where when I load my csv file in after I've used the software once to add in a new ingredient I get the error: "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" and it highlights the section of the code "newRow("Protien") = columns(1)" Now I think is because when it loads in the data from the recently saved csv file it has a row at the top that is filled with the headers, is there any way that I can save the csv file without including the headers from the data table?
Code that laods in the csv file:
'Sets up the data table that will be used to store the ingredients and their information.'
With ingredientTable
.Columns.Add("Name", System.Type.GetType("System.String"))
.Columns.Add("Protien", System.Type.GetType("System.Decimal"))
.Columns.Add("Fat", System.Type.GetType("System.Decimal"))
.Columns.Add("Salt", System.Type.GetType("System.Decimal"))
.Columns.Add("Carbs", System.Type.GetType("System.Decimal"))
.Columns.Add("Calories", System.Type.GetType("System.Decimal"))
End With
'Loads in the information from the CSV file to display all the previouly saved ingredients'
Dim fileReader As New IO.StreamReader("C:\Users\Grant\Desktop\FitnessAppNew\Save Files\Saved_Ingredients.csv", System.Text.Encoding.Default)
Dim ingredientString As String = ""
Do
ingredientString = fileReader.ReadLine
If ingredientString Is Nothing Then Exit Do
'Reads what is on the CSV file and sets up the columns and the rows.'
Dim columns() As String = ingredientString.Split(",")
Dim newRow As DataRow = ingredientTable.NewRow
newRow("Name") = columns(0)
newRow("Protien") = columns(1)
newRow("Fat") = columns(2)
newRow("Salt") = columns(3)
newRow("Carbs") = columns(4)
newRow("Calories") = columns(5)
ingredientTable.Rows.Add(newRow)
Loop
fileReader.Close()
DataGridView1.DataSource = ingredientTable
Me.Text = ingredientTable.Rows.Count & "rows"
Code that saves the CSV file:
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
#Region "Save ingredients"
Dim csvFile As String = String.Empty
csvFile = csvFile.TrimEnd(",")
csvFile = csvFile & vbCr & vbCrLf
'Used to ge the rows
For Each row As DataGridViewRow In DataGridView1.Rows
'Used to get each cell in the row
For Each cell As DataGridViewCell In row.Cells
csvFile = csvFile & cell.FormattedValue & ","
Next
csvFile = csvFile.TrimEnd(",")
csvFile = csvFile & vbCr & vbCrLf
Next
My.Computer.FileSystem.WriteAllText("C:\Users\Grant\Desktop\FitnessAppNew\Save Files\Saved_Ingredients.csv", csvFile, False)
#End Region
Here are a few options to consider, for loading data from a CSV to a DGV.
Imports System.Data.SqlClient
Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Imports System.Data
Imports System.Data.Odbc
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim headers = (From header As DataGridViewColumn In DataGridView1.Columns.Cast(Of DataGridViewColumn)() Select header.HeaderText).ToArray
Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() Where Not row.IsNewRow Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, c.Value.ToString, ""))
Dim str As String = ""
Using sw As New IO.StreamWriter("C:\Users\Excel\Desktop\OrdersTest.csv")
sw.WriteLine(String.Join(",", headers))
'sw.WriteLine(String.Join(","))
For Each r In rows
sw.WriteLine(String.Join(",", r))
Next
sw.Close()
End Using
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
'Dim m_strConnection As String = "server=Excel-PC\SQLEXPRESS;Initial Catalog=Northwind;Trusted_Connection=True;"
'Catch ex As Exception
' MessageBox.Show(ex.ToString())
'End Try
'Dim objDataset1 As DataSet()
'Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Dim da As OdbcDataAdapter
Dim OpenFile As New System.Windows.Forms.OpenFileDialog ' Does something w/ the OpenFileDialog
Dim strFullPath As String, strFileName As String
Dim tbFile As New TextBox
' Sets some OpenFileDialog box options
OpenFile.Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*" ' Shows only .csv files
OpenFile.Title = "Browse to file:" ' Title at the top of the dialog box
If OpenFile.ShowDialog() = DialogResult.OK Then ' Makes the open file dialog box show up
strFullPath = OpenFile.FileName ' Assigns variable
strFileName = Path.GetFileName(strFullPath)
If OpenFile.FileNames.Length > 0 Then ' Checks to see if they've picked a file
tbFile.Text = strFullPath ' Puts the filename in the textbox
' The connection string for reading into data connection form
Dim connStr As String
connStr = "Driver={Microsoft Text Driver (*.txt; *.csv)}; Dbq=" + Path.GetDirectoryName(strFullPath) + "; Extensions=csv,txt "
' Sets up the data set and gets stuff from .csv file
Dim Conn As New OdbcConnection(connStr)
Dim ds As DataSet
Dim DataAdapter As New OdbcDataAdapter("SELECT * FROM [" + strFileName + "]", Conn)
ds = New DataSet
Try
DataAdapter.Fill(ds, strFileName) ' Fills data grid..
DataGridView1.DataSource = ds.Tables(strFileName) ' ..according to data source
' Catch and display database errors
Catch ex As OdbcException
Dim odbcError As OdbcError
For Each odbcError In ex.Errors
MessageBox.Show(ex.Message)
Next
End Try
' Cleanup
OpenFile.Dispose()
Conn.Dispose()
DataAdapter.Dispose()
ds.Dispose()
End If
End If
End Sub
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
Dim tblReadCSV As New DataTable()
tblReadCSV.Columns.Add("FName")
tblReadCSV.Columns.Add("LName")
tblReadCSV.Columns.Add("Department")
Dim csvParser As New TextFieldParser("C:\Users\Excel\Desktop\Employee.txt")
csvParser.Delimiters = New String() {","}
csvParser.TrimWhiteSpace = True
csvParser.ReadLine()
While Not (csvParser.EndOfData = True)
tblReadCSV.Rows.Add(csvParser.ReadFields())
End While
Dim con As New SqlConnection("Server=Excel-PC\SQLEXPRESS;Database=Northwind;Trusted_Connection=True;")
Dim strSql As String = "Insert into Employee(FName,LName,Department) values(#Fname,#Lname,#Department)"
'Dim con As New SqlConnection(strCon)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = strSql
cmd.Connection = con
cmd.Parameters.Add("#Fname", SqlDbType.VarChar, 50, "FName")
cmd.Parameters.Add("#Lname", SqlDbType.VarChar, 50, "LName")
cmd.Parameters.Add("#Department", SqlDbType.VarChar, 50, "Department")
Dim dAdapter As New SqlDataAdapter()
dAdapter.InsertCommand = cmd
Dim result As Integer = dAdapter.Update(tblReadCSV)
End Sub
End Class
You must try this code.
OpenButton
Dim OpenFileDialog1 As New OpenFileDialog()
Dim constr As String
Dim con As OleDb.OleDbConnection
Try
OpenFileDialog1.Filter = "Excel Files | *.xlsx; *.xls; *.xlsm;"
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Me.txtOpen.Text = OpenFileDialog1.FileName
constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtOpen.Text + ";Excel 12.0 Xml;HDR=YES"
con = New OleDb.OleDbConnection(constr)
con.Open()
cboSheet.DataSource = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
cboSheet.DisplayMember = "TABLE_NAME"
cboSheet.ValueMember = "TABLE_NAME"
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Load Button
Dim constr As String
Dim dt As DataTable
Dim con As OleDbConnection
Dim sda As OleDbDataAdapter
Dim row As DataRow
Try
constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtOpen.Text + ";Excel 12.0 Xml;HDR=YES"
con = New OleDbConnection(constr)
sda = New OleDbDataAdapter("Select * from [" + cboSheet.SelectedValue + "]", con)
dt = New DataTable
sda.Fill(dt)
For Each row In dt.Rows
DataGridView3.DataSource = dt
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
I hope it will works! :)

visual basic 2010, Importing excel files to my SQL using bulkcopy

hello guys I'm having a problem with a project that I build
I'm trying to import excel files to sql server 2016 using bulkcopy and I got this error
"'DAFTAR1$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long"
I think there is a problem with this code
Dim query_excel As String = "SELECT * from [" & file & "$]"
i tried all of this code on another pc and it works perfectly but on my pc it's getting that error, I don't know what did I wrong, I'm using visual basic 2010 professional edition, sql management server 2016 and microsoft office 2016
can anyone help me to figure what is wrong with the code ?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
file = System.IO.Path.GetFileNameWithoutExtension(TextBox1.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim koneksi_excel As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ace.OLEDB.12.0;Data Source='" & TextBox1.Text & "';Extended Properties=""Excel 12.0 Xml;HDR=YES;""")
koneksi_excel.Open()
Dim query_excel As String = "SELECT * from [" & file & "$]"
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(query_excel, koneksi_excel)
Dim rd As OleDb.OleDbDataReader
rd = cmd.ExecuteReader()
Dim koneksi As New SqlClient.SqlConnection()
Dim koneksidatabase As String = "server=DESKTOP-KJQ8PNO\SQLEXPRESS;database=otto;Integrated Security=True"
koneksi.ConnectionString = koneksidatabase
koneksi.Open()
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet()
Dim dt As New DataTable
ds.Tables.Add(dt)
da = New OleDb.OleDbDataAdapter(query_excel, koneksi_excel)
da.Fill(dt)
Using bulkcopy As SqlClient.SqlBulkCopy = New SqlClient.SqlBulkCopy(koneksi)
bulkcopy.DestinationTableName = file
bulkcopy.BulkCopyTimeout = 600
bulkcopy.WriteToServer(rd)
rd.Close()
MsgBox("Data uploaded to database", MsgBoxStyle.Information, "Uploaded")
TextBox1.Text = ""
End Using
End Sub
If you're using a DataGridView or some other grid to view the data, this is one way that you can view the data and load it into the grid.
Using ofd As New OpenFileDialog
ofd.InitialDirectory = dir
ofd.Filter = "Excel Files (*.xlsx)|*.xlsx| XLS Files (*.xls)|*.xls"
ofd.FilterIndex = 1
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
txtFile.Text = ofd.FileName
fi = New FileInfo(ofd.FileName)
fileName = ofd.FileName
excel = fi.FullName
eCon = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
excel & ";Extended Properties='Excel 12.0;IMEX=1;';")
eCon.Open()
Dim dtSheets As DataTable = eCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim listSheet As New List(Of String)
Dim drSheet As DataRow
For Each drSheet In dtSheets.Rows
If drSheet.Item("TABLE_NAME").ToString() <> "Sheet1$" Then
listSheet.Add(drSheet("TABLE_NAME").ToString())
Else
End If
Next
Me.Cursor = Cursors.WaitCursor
Dim myTable = ""
myTable = listSheet.Item(0)
da = New OleDbDataAdapter(String.Format("SELECT * FROM [{0}]", myTable), eCon)
ds = New DataSet
da.Fill(ds, myTable)
Dim dt As DataTable = ds.Tables(0).Copy()
From here, set the dt as the grid DataSource. This way you know you're always getting the first worksheet, and don't have to worry about what the name is as such, the program is getting it for you.
Have a play around a let me know. Alternatively, you can just use this code to get the Excel sheet and use what you have already for the import.

Insert & Retrieve Image in Postgresql database form VBNet

I want to insert and retrieve image in Postgresql database from VB.net on button click event. My table has one column "image" with datatype bytea. I have written below code but not getting desired output:
'Insert
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
Dim image1 As Byte()
Dim FS As FileStream = New FileStream("C:\0.jpg", FileMode.Open, FileAccess.Read)
Dim BR As BinaryReader = New BinaryReader(FS)
Dim lg As Long = FS.Length
Dim length As Integer = Convert.ToInt32(lg)
image1 = BR.ReadBytes(length)
conn.Open()
Dim Command As NpgsqlCommand = New NpgsqlCommand("INSERT INTO table2 Values(#image1)", conn)
'Dim Command As NpgsqlCommand = New NpgsqlCommand("INSERT INTO table1 values('" & timestamp & "','" & serialno & "','" & modelname & "','0','0','0','0','0','0','0','0','0','0','0','0')", conn)
Command.Parameters.Add(New NpgsqlParameter("#image1", NpgsqlTypes.NpgsqlDbType.Bytea))
Command.ExecuteNonQuery()
conn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
'retrieve
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Try
conn.Open()
Dim com As NpgsqlCommand = New NpgsqlCommand("select image from table2", conn)
Dim reader As NpgsqlDataReader = com.ExecuteReader()
Dim imageInBytes As Byte() = reader("image")
Dim memoryStream As System.IO.MemoryStream = _
New System.IO.MemoryStream(imageInBytes, False)
Dim image As System.Drawing.Image = _
System.Drawing.Image.FromStream(memoryStream)
image.Save("E:\image")
PictureBox6.Image = image
conn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
You need values in parameters in 'Insert'...Change this...
Dim Command As NpgsqlCommand = New NpgsqlCommand("INSERT INTO table2 Values(#image1)", conn)
Command.Parameters.Add(New NpgsqlParameter("image1", NpgsqlTypes.NpgsqlDbType.Bytea))
For this...
Dim Command As NpgsqlCommand = New NpgsqlCommand("INSERT INTO table2 Values(:image1)", conn)
Dim param As NpgsqlParameter = New NpgsqlParameter("image1", NpgsqlTypes.NpgsqlDbType.Bytea)
param.Value = image1
Command.Parameters.Add(param)
And in 'Retrieve' you need take length before... Change this...
Dim imageInBytes As Byte() = reader("image")
Dim memoryStream As System.IO.MemoryStream = _
New System.IO.MemoryStream(imageInBytes, False)
Dim image As System.Drawing.Image = _
System.Drawing.Image.FromStream(memoryStream)
image.Save("E:\image")
PictureBox6.Image = image
For something similar to this...
Dim imageInBytes As Byte()
reader.Read()
' Retrieve the length of the necessary byte array.
Dim column as integer = reader.GetOrdinal("name_column")
Dim len As Long = reader.GetBytes(column, 0, Nothing, 0, 0)
' Create a buffer to hold the bytes, and then
' read the bytes from DataReader.
ReDim imageInBytes(CInt(len))
reader.GetBytes(column, 0, imageInBytes, 0, CInt(len))
PictureBox1.Image = New Bitmap(New MemoryStream(imageInBytes))

Loading image from database to picturebox

I'm new to visual basic and I have a problem in loading the image from my database. I'm currently using image data type. This is how I save the image
Imports System.Data
Imports System.Data.SqlClient
Public Class ScannerX_Add
Dim ImageFilename As String
Dim ImageUpload As Image
Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Scan.Click
Try
OpenFileDialog1.Title = "Please Select a File"
OpenFileDialog1.InitialDirectory = "C:\Users\ITTestServer\Desktop\Dekstop\"
OpenFileDialog1.ShowDialog()
ScannerX_Pic.Image = Image.FromFile(OpenFileDialog1.FileName)
ImageFilename = OpenFileDialog1.FileName
ImageUpload = Image.FromFile(OpenFileDialog1.FileName)
Catch ex As Exception
MsgBox("Please insert scan finger")
Exit Sub
End Try
End Sub
Private Sub Btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Save.Click
If ImageFilename <> "" Then
Dim imageNameTemp As String
imageNameTemp = ImageFilename
While (imageNameTemp.Contains("\"))
imageNameTemp = imageNameTemp.Remove(0, imageNameTemp.IndexOf("\") + 1)
End While
Dim ms As New IO.MemoryStream
If ImageFilename.Contains("jpeg") Or ImageFilename.Contains("jpg") Then
ImageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
End If
If ImageFilename.Contains("png") Then
ImageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
End If
If ImageFilename.Contains("gif") Then
ImageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
End If
If ImageFilename.Contains("bmp") Then
ImageUpload.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
End If
Dim b() As Byte = ms.ToArray()
Dim cmd As New SqlCommand("Insert into Scanner (Name,Finger) VALUES('" & TxtBox_Name.Text.Trim & "','" & imageNameTemp & "')", Connection)
cmd.Parameters.Add("#BLOBData", SqlDbType.Image, b.Length).Value = b
cmd.ExecuteNonQuery()
MsgBox("Profile Has Been Saved")
Me.Close()
End If
End Sub
Now I need to load my image to picturebox which is currently in the Main form.
Private Sub ButtonX1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
Command.CommandText = ("select Finger FROM Scanner")
Command.Connection = Connection
Dim da As New SqlDataAdapter(Command)
Dim ds As New DataSet()
da.Fill(ds, "projectimages")
Dim c As Integer = ds.Tables(0).Rows.Count
If c > 0 Then
Dim bytBLOBData() As Byte = _
ds.Tables(0).Rows(c - 1)("imagedate")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
ScannerX_Pic2.Image = Image.FromStream(stmBLOBData)
End If
End Sub
Now I get the error Object reference not set to an instance of an object.
To save an image you could do something like this
Dim sql As String = "INSERT INTO Information VALUES(#name,#photo)"
Image Img = PictureBox1.BackgroundImage
Dim cmd As New SqlCommand(sql, con)
cmd.Parameters.AddWithValue("#name", TextBox1.Text)
Dim ms As New MemoryStream()
Img.Save(ms, Img.RawFormat)
Dim data As Byte() = ms.GetBuffer()
Dim p As New SqlParameter("#photo", SqlDbType.Image)
p.Value = data
cmd.Parameters.Add(p)
cmd.ExecuteNonQuery()
And to retrieve an image
cmd = New SqlCommand("select photo from Information where name='Rose'", con)
Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
If Not imageData Is Nothing Then
Using ms As New MemoryStream(imageData, 0, imageData.Length)
ms.Write(imageData, 0, imageData.Length)
PictureBox1.BackgroundImage = Image.FromStream(ms, True)
End Using
Also check out this article it's doing exactly what you are trying to achieve
http://www.codeproject.com/Articles/437937/Save-and-Retrieve-Image-from-a-SQL-Server-Database
The sql is selecting a column named "Finger":
"select Finger FROM Scanner"
But it's later trying to build an Image object from a column named "imagedate":
ds.Tables(0).Rows(c - 1)("imagedate")
It needs to use the same column name as the SQL result set:
ds.Tables(0).Rows(c - 1)("Finger")
There may be other problems as well, but that's what jumped out at me right away.

Save binary file from SQL Server

I'm trying to save a binary file stored in a SQL database with a SaveDialog, the function RetrieveFile retrieves the specified file from the database as a Byte array, here's what I have so far:
Private Shared Function RetrieveFile(ByVal filename As String) As Byte()
Dim connection As New SqlConnection("Data Source=SERVER\SQL2008;Initial Catalog=NorthPole;Integrated Security=True")
Dim command As New SqlCommand("SELECT pcfFile FROM Items WHERE pcfFileName=#Filename", connection)
command.Parameters.AddWithValue("#Filename", filename)
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)
reader.Read()
Dim memory As New MemoryStream()
Dim startIndex As Long = 0
Const ChunkSize As Integer = 256
While True
Dim buffer As Byte() = New Byte(ChunkSize - 1) {}
Dim retrievedBytes As Long = reader.GetBytes(1, startIndex, buffer, 0, ChunkSize)
memory.Write(buffer, 0, CInt(retrievedBytes))
startIndex += retrievedBytes
If retrievedBytes <> ChunkSize Then
Exit While
End If
End While
connection.Close()
Dim data As Byte() = memory.ToArray()
memory.Dispose()
Return data
End Function
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "PCF File|*.pcf|"
saveFileDialog1.Title = "Save an pcf File"
saveFileDialog1.ShowDialog()
If saveFileDialog1.FileName <> "" Then
Dim fs As System.IO.FileStream = CType(RetrieveFile("FakePCF.pcf"), System.IO.FileStream)
fs.Close()
End If
End Sub
Files are saved as "SqlDbType.VarBinary" within the database.
I get: "Index was outside the bounds of the array." on:
Dim retrievedBytes As Long = reader.GetBytes(1, startIndex, buffer, 0, ChunkSize)
The MemoryStream appears to not be retrieving the data yet the SQL sytax is correct.
What am I doing wrong?
Well, first of all, your method returns byte[] and you're trying to cast it to FileStream. You should change your handler to following:
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "PCF File|*.pcf|"
saveFileDialog1.Title = "Save an pcf File"
saveFileDialog1.ShowDialog()
If saveFileDialog1.FileName <> "" Then
Dim fs As New System.IO.FileStream (saveFileDialog1.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)
Dim data As Byte() = RetrieveFile("FakePCF.pcf")
fs.Write(data, 0, data.Length)
fs.Flush()
fs.Close()
End If
End Sub