[RESOLVED] [2005] looping through files and importing
Ok I think I am close but am doing something wrong... Basically I have a directory of images that I am trying to import into a SQL table via Stored Proc... Getting the error:
No mapping exists from object type System.IO.FileStream to a known managed provider native type.
vb Code:
Dim stFileDir As String = "U:\styleimages"
Dim cmd As New SqlCommand
conn.Open()
With cmd
.Connection = conn
.CommandType = CommandType.StoredProcedure
.CommandText = "InsertStyleImage"
For Each FileName As String In Directory.GetFiles(stFileDir)
Dim MyFileInfo As New FileInfo(FileName)
.Parameters.Clear()
.Parameters.AddWithValue("@ImageName", MyFileInfo.Name)
.Parameters.AddWithValue("@ImageData", MyFileInfo.OpenRead)
.Parameters.AddWithValue("@ImageSize", MyFileInfo.Length)
.Parameters.AddWithValue("@ImageType", MyFileInfo.GetType)
.Parameters.AddWithValue("@StyleNo", " ")
.ExecuteNonQuery()
Next
MsgBox("Done!")
End With
conn.Close()
End Sub
I am guessing it is in MyFileInfo.OpenRead but not sure what I should use.
Re: [2005] looping through files and importing
Take a look at JMC's code on saving an image into a database field:
http://www.vbforums.com/showthread.p...ighlight=image
Re: [2005] looping through files and importing
thanks.... but I would think I could stick to this format and treat it as any type of file... What it if it wasn't a image?
Re: [RESOLVED] [2005] looping through files and importing
As long as you get it into a memory stream, JMC's method will work for any file type, just when you extract it out, you have to know what it is.
Re: [RESOLVED] [2005] looping through files and importing
wondering if this will do
Code:
.Parameters.AddWithValue("@ImageData", New IO.BinaryReader(MyFileInfo.OpenRead).ReadBytes(MyFileInfo.OpenRead.Length))