store/update a image in sql server

srdva59

Active member
Joined
Feb 21, 2009
Messages
28
Programming Experience
Beginner
hi,
i looking for a easy way to save a image to a sql server i
have found a lot of examples but many is for asp.net and others have
many functions for do a simple process that with vb6 i do with a few lines:
"mstream.LoadFromFile .FileName
RS.Fields("imagem_sql").Value = mstream.Read
Image1.Picture = LoadPicture(.FileName)
RS.update"

so i looking for a simple way to do that i have found this:

connetionString = "Data Source=ip;Initial Catalog=bd;User ID=user;Password=pass"

Dim con As String = connetionString
Dim str As String = "select * from store where id like '" & TextBox23.Text & "'"
Dim da As New SqlDataAdapter(str, con)
Dim cmd As New SqlCommand(str, New SqlConnection(con))
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()

connection.Open()
command = New SqlCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds)
adapter.Dispose()
command.Dispose()
connection.Close()


Dim fs As New FileStream(txtImageFile.Text, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
Dim MyData(fs.Length) As Byte
fs.Read(MyData, 0, fs.Length)
fs.Close()
ds.Tables(0).Rows(0)("imagem_sql") = MyData

da.Update(ds, 0)

fs = Nothing
MyCB = Nothing
ds = Nothing
da = Nothing
cmd.Connection.Close()
con = Nothing

but this simple dont update nothing
i have tryed to update a text value but the result is the same nothing is updated!
but he actully dont return any error and i can see that the record is opened but simple can´t update

any one can help me to understand what is wrong and why this dont update?
thanks a lot for your help :)
 
VB.NET:
Expand Collapse Copy
    Private Sub File2SqlBlob(ByVal FileInformation As FileInfo)
        Dim cn As New SqlConnection(My.Settings.DocStoreConnectionString.ToString())
        Dim cmd As New SqlCommand("INSERT INTO Documents (DocName, DocContentType, DocSize, DocData, CreateDate, UploadDate)" & _
            "VALUES (@DocName, @DocContentType, @DocSize, @DocData, @CreateDate, @UploadDate)", cn)

        Dim fs As FileStream = New FileStream(FileInformation.FullName, FileMode.Open, FileAccess.Read)
        Dim b(fs.Length - 1) As Byte
        fs.Read(b, 0, b.Length)
        fs.Close()

        cmd.Parameters.AddWithValue("@DocName", FileInformation.Name)
        cmd.Parameters.AddWithValue("@DocContentType", MimeType(FileInformation.Extension))
        cmd.Parameters.AddWithValue("@DocSize", b.Length)
        cmd.Parameters.AddWithValue("@DocData", b)
        cmd.Parameters.AddWithValue("@CreateDate", FileInformation.CreationTime)
        cmd.Parameters.AddWithValue("@UploadDate", Date.Now)

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

Only thing not readily apparent is that MimeType is a dictionary.

VB.NET:
Expand Collapse Copy
		Dim MimeType As New Dictionary(Of String, String)

		With MimeType
			.Add(".txt", "text/plain")
			.Add(".csv", "text/csv")
			.Add(".xml", "text/xml")
			.Add(".htm", "text/html")
			.Add(".html", "text/html")
			.Add(".doc", "application/msword")
			.Add(".docx", "application/msword")
			.Add(".xls", "application/excel")
			.Add(".xlsx", "application/excel")
			.Add(".ppt", "application/mspowerpoint")
			.Add(".pdf", "application/pdf")
			.Add(".zip", "application/zip")
			.Add(".gif", "image/gif")
			.Add(".jpg", "image/jpeg")
			.Add(".jpeg", "image/jpeg")
			.Add(".png", "image/png")
			.Add(".tif", "image/tiff")
			.Add(".tiff", "image/tiff")
		End With
 
Back
Top