السلام عليكم
1- عندي قاعدة بيانات اكسيس واستخدم فيجول بيسك دوت نت بالاتصال المنفصل
- عملت 2 عمود بقاعدة البيانات OLE Object
- احدهما لحفظ الصور والاخر لحفظ صفحات الويب و ملفات pdf
بحفظ الصور وبعرضها عادي جدا ولكن المشكلة في حفظ ملفات pdf وصفحات النت بقاعدة البيانات وعرضها بالفورم
كود اسناد الصورة لقاعدة البيانات
PictureBox1.DataBindings.Add("Image", BindingSource1, "pic_prod", True)
انا حاولت بطريقة حفظ كل ملفات الصفح بفولدر وحفظ مسارتها بقاعدة البيانات وعند الضغط علي قاعدة البيانات يحدد الصفح وملفات البي دي اف بالطريقة دية
كود :
Process.Start
DataGridView1.SelectedRows.Item(0).Cells(1).Value
بس طريقة فاشلة جدا
كود الحفظ بقاعدة البيانات للمدخلات
كود :
BindingSource1.EndEdit()
DataAdapter1.Update(DataSet1, "Tb1")
(09-11-17, 08:48 PM)Rabeea Qbaha كتب : [ -> ]
كود :
'First save the image to the table
Private Function SaveImageToDB(ByRef name As String) As Boolean
Try
Dim conn As New OleDbConnection
Dim cmd As OleDbCommand
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database1.mdb;PERSIST SECURITY INFO = false"
conn.Open()
cmd = conn.CreateCommand()
cmd.CommandText = "INSERT INTO Table1(Name, Image) VALUES (@Name, @Image)"
Dim imgByteArray() As Byte
Try
Dim stream As New MemoryStream
Dim bmp As New Bitmap(stream)
bmp.Save(stream, ImageFormat.Jpeg)
imgByteArray = stream.ToArray()
stream.Close()
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Image", imgByteArray)
If DirectCast(cmd.ExecuteNonQuery(), Integer) > 0 Then
Return True
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
End Try
conn.Close()
cmd.Dispose()
conn.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
End Try
End Function
'Retrieve image from table
Public Function GetImageFromDB(ByRef imageName As String) As Bitmap
Try
Dim conn As New OleDbConnection
Dim cmd As OleDbCommand
Dim reader As OleDbDataReader
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database1.mdb;PERSIST SECURITY INFO = false"
conn.Open()
cmd = conn.CreateCommand()
cmd.CommandText = "SELECT Image FROM ImageTable WHERE ImageName = '" & imageName & "'"
reader = cmd.ExecuteReader
If reader.Read Then
Dim imgByteArray() As Byte
Try
imgByteArray = CType(reader(0), Byte())
Dim stream As New MemoryStream(imgByteArray)
Dim bmp As New Bitmap(stream)
stream.Close()
Return bmp
Catch ex As Exception
MessageBox.Show(ex.Message)
Return Nothing
End Try
End If
reader.Close()
conn.Close()
cmd.Dispose()
conn.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message)
Return Nothing
End Try
End Function
اريد حفظ ملفات البي دي اف وليس الصور انا بحفظ الصور وباستعرضها
وكود الاتصال لدي
Public con As New OleDb.OleDbConnection("provider=microsoft.ace.oledb.12.0;data source=" & Application.StartupPath & "\product.accdb")
(09-11-17, 08:59 PM)محمد اسماعيل كتب : [ -> ] (09-11-17, 08:48 PM)Rabeea Qbaha كتب : [ -> ]
كود :
'First save the image to the table
Private Function SaveImageToDB(ByRef name As String) As Boolean
Try
Dim conn As New OleDbConnection
Dim cmd As OleDbCommand
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database1.mdb;PERSIST SECURITY INFO = false"
conn.Open()
cmd = conn.CreateCommand()
cmd.CommandText = "INSERT INTO Table1(Name, Image) VALUES (@Name, @Image)"
Dim imgByteArray() As Byte
Try
Dim stream As New MemoryStream
Dim bmp As New Bitmap(stream)
bmp.Save(stream, ImageFormat.Jpeg)
imgByteArray = stream.ToArray()
stream.Close()
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Image", imgByteArray)
If DirectCast(cmd.ExecuteNonQuery(), Integer) > 0 Then
Return True
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
End Try
conn.Close()
cmd.Dispose()
conn.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
End Try
End Function
'Retrieve image from table
Public Function GetImageFromDB(ByRef imageName As String) As Bitmap
Try
Dim conn As New OleDbConnection
Dim cmd As OleDbCommand
Dim reader As OleDbDataReader
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database1.mdb;PERSIST SECURITY INFO = false"
conn.Open()
cmd = conn.CreateCommand()
cmd.CommandText = "SELECT Image FROM ImageTable WHERE ImageName = '" & imageName & "'"
reader = cmd.ExecuteReader
If reader.Read Then
Dim imgByteArray() As Byte
Try
imgByteArray = CType(reader(0), Byte())
Dim stream As New MemoryStream(imgByteArray)
Dim bmp As New Bitmap(stream)
stream.Close()
Return bmp
Catch ex As Exception
MessageBox.Show(ex.Message)
Return Nothing
End Try
End If
reader.Close()
conn.Close()
cmd.Dispose()
conn.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message)
Return Nothing
End Try
End Function
اريد حفظ ملفات البي دي اف وليس الصور انا بحفظ الصور وباستعرضها
وكود الاتصال لدي
Public con As New OleDb.OleDbConnection("provider=microsoft.ace.oledb.12.0;data source=" & Application.StartupPath & "\product.accdb")
طريقه حفظ ملفات pdf نفس حفظ الصور لانه بالحالتين يقوم بتحويلهم الى bit وبد ذالك يخزنو في قاعده البيانات
اعتزر لك اخي جميل علي
لا استخدم هذة الطريقة لتعامل مع قاعدة البيانات ولم اقد ر علي استخلاص ما احتاج ببرنامجي
انا بستخدم الطريقة دية لحفظ الصور ومسحها ووضعت امتداد لكل الملفات ولكن لاتحفظ بقاعدة البيانات وتحفظ الصور فقط وذالك لان الكود يصف الصور كاملا ليست اكواد حفظ الصور متشابة مع اكواد حفظ html ,pdf ,doc
Private Sub savimage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles savimage.Click
' save
If BindingSource1.Count = 0 Then Beep() : Exit Sub
Try
If PictureBox1.Image IsNot Nothing Then
If MsgBox(" هل تريد استبدال الصورة الحالية " & " ؟ ", MsgBoxStyle.Exclamation + MsgBoxStyle.MsgBoxRight + MsgBoxStyle.OkCancel, "استبدال صورة") = MsgBoxResult.Cancel Then Exit Sub
Else
If MsgBox(" استمرار اضافة صورة جديدة للسجل رقم " & Label2.Text & " ؟ ", MsgBoxStyle.Exclamation + MsgBoxStyle.MsgBoxRight + MsgBoxStyle.OkCancel, "ادراج صورة") = MsgBoxResult.Cancel Then Exit Sub
End If
Application.DoEvents()
Dim OFG As New OpenFileDialog
OFG.Filter = "Image Files (*.bmp;*.jpg;*.jpeg;*.GIF)|*.bmp;*.jpg;*.jpeg;*.GIF|" + _
"PNG files (*.png)|*.png|text files (*.text)|*.txt|doc files (*.doc)|*.doc|pdf files (*.pdf)|*.pdf" '"Files(*.jpg)|*.jpg|Files(*.gif)|*.gif|Files(*.bmp)|*.bmp|Files(*.png)|*.png"
OFG.Title = "ادراج صورة"
OFG.FileName = ""
If OFG.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim fs As IO.FileStream = New IO.FileStream(OFG.FileName, IO.FileMode.Open, IO.FileAccess.Read)
Dim LoadImage As Image = Image.FromStream(fs)
fs.Close()
Dim Stream As New IO.MemoryStream()
Dim NewBitmap As New Bitmap(LoadImage, 200, 225)
NewBitmap.Save(Stream, System.Drawing.Imaging.ImageFormat.Jpeg)
Application.DoEvents()
BindingSource1.EndEdit()
DataAdapter1.Update(DataSet1.Tables("purch_tb"))
DataSet1.Tables("purch_tb").Rows(BindingSource1.Position).Item("pic_prod") = Stream.ToArray
Stream.Close()
BindingSource1.EndEdit()
DataAdapter1.Update(DataSet1.Tables("purch_tb"))
Application.DoEvents()
MsgBox("تم حفظ الصورة نجاح", MsgBoxStyle.MsgBoxRight + MsgBoxStyle.Information, "نجاح ")
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub deletimage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deletimage.Click
Try
If PictureBox1.Image Is Nothing Then Beep() : MsgBox("لايوجد صورة ليتم حذفها") : Exit Sub
If MsgBox(" استمرار حذف الصورة الحالية" & " ؟ ", MsgBoxStyle.Exclamation + MsgBoxStyle.MsgBoxRight + MsgBoxStyle.OkCancel, "حذف صورة") = MsgBoxResult.Cancel Then Exit Sub
PictureBox1.Image = Nothing
BindingSource1.EndEdit()
DataAdapter1.Update(DataSet1.Tables("purch_tb"))
MsgBox("تم حذف الصورة نجاح", MsgBoxStyle.MsgBoxRight + MsgBoxStyle.Information, "نجاح ")
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub