تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[VB.NET] اضافة الصور لقاعدة البيانات SQL
#1
السلام عليكم 

الرجاء تصحيح كود حفظ الصور بقاعدة البيانات SQL SERVER 

الكود المستخدم
  Dim addteo As String = MsgBox("هل حقأ ترغب اضافة البيانات ", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "تأكيد عملية اضافة البيانات")
            If addteo = vbYes Then

                Cmd = New OleDbCommand(" Insert Into invest (patient_Name,[DATE],investigation_name,Report,eye,photo) 
                         values ('" & textname.Text & "','" & Trim(Textdate.Text) & "','" & TEXTINVE1.Text & "','" & TextREPORT1.Text & "','" & texteye1.Text & "',@photo)", Con)

                Dim ms1 As New MemoryStream()
                PBOX1.BackgroundImage.Save(ms1, PBOX1.BackgroundImage.RawFormat)
                Dim picture1 As Byte() = ms1.GetBuffer()
                Dim p As New OleDbParameter("@photo", SqlDbType.Image)
                p.Value = picture1
                Cmd.Parameters.Add(p)

                Cmd.ExecuteNonQuery()
                Con.Close()
                MsgBox("تمت عملية حفظ البيانات بنجاح", MsgBoxStyle.Exclamation, "تأكيد عملية الحفظ")
                cler()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

تظهر رساله خطاء   object reference not set to an instance of an object


تم استخدام كود اخر 
  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            Dim open As New OpenFileDialog

            If open.ShowDialog = DialogResult.OK Then

                pbox.Image = Image.FromFile(open.FileName)

            End If

            Dim ms1 As New MemoryStream()

            pbox.Image.Save(ms1, pbox.Image.RawFormat)

            Dim arrimg() As Byte = ms1.GetBuffer

            ms1.Close()

            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            'Con.Open()

            Dim sqlc As New OleDbCommand

            sqlc.Connection = Con

            sqlc.CommandText = "insert into invest (patient_Name,[DATE],investigation_name,Report,eye,photo)" & "values('" & textname.Text & "','" & Trim(Textdate.Text) & "','" & TEXTINVE1.Text & "','" & TextREPORT1.Text & "','" & texteye1.Text & "'," & "@img" & ")"

            sqlc.Parameters.Add(New OleDbParameter("@img", SqlDbType.Image)).Value = arrimg

            sqlc.ExecuteNonQuery()

            Con.Close()
            MsgBox("تمت عملية حفظ البيانات بنجاح", MsgBoxStyle.Exclamation, "تأكيد عملية الحفظ")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

وايضا يظهر الخطاء     must declare the scalar variable "@img"
برجاء المساعدة
الرد }}}
تم الشكر بواسطة:
#2
(23-10-17, 03:23 AM)khodor1985 كتب : تابع الفيديوهات :
https://www.youtube.com/watch?v=utheGCVkvs0

https://www.youtube.com/watch?v=r0pLHP7x4R8

اول شي صديقي تحتاج ان يكون العمود في قاعدة البيانات image 

كود :
Dim imagepath As String
   Dim myStream As IO.Stream = Nothing
   Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

       Dim openFileDialog1 As New OpenFileDialog()
       'Set the Filter.
       openFileDialog1.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"
       If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
           Try
               myStream = openFileDialog1.OpenFile()
               If (myStream IsNot Nothing) Then
                   imagepath = openFileDialog1.FileName
               End If
           Catch Ex As Exception
               MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
           Finally
               ' Check this again, since we need to make sure we didn't throw an exception on open.
               If (myStream IsNot Nothing) Then
                   myStream.Close()
               End If
           End Try
       Else
           Return
       End If
   End Sub
المثود الخاصه بتحويل الصوره 
PHP كود :
Private Function ImageToStream(ByVal fileName As String) As Byte()
 
       Dim stream As New MemoryStream()
tryagain:
 
       Try
            Dim image 
As New Bitmap(fileName)
 
           image.Save(streamSystem.Drawing.Imaging.ImageFormat.Jpeg)
 
       Catch ex As Exception
            GoTo tryagain
        End 
Try

 
       Return stream.ToArray()
 
   End Function 

كود :
Private Sub btn_with_pic_Click(sender As Object, e As EventArgs) Handles btn_with_pic.Click
       Try

           Dim fName As String
           fName = imagepath
           If File.Exists(fName) Then
               Dim content As Byte() = ImageToStream(fName)
               'فحص الاتصال بقاعدة البيانات
               If SQL.conn.State = ConnectionState.Open Then
                   SQL.conn.Close()
               End If
               SQL.conn.Open()

               Dim cmd As New SqlCommand()
               cmd.CommandText = "insert into customers (cust_id,name,image,date) values(@cust_id,@name,@image,@date)"
               cmd.Parameters.AddWithValue("@cust_id", (id_number_txt.Text))
               cmd.Parameters.AddWithValue("@name", (name_txt.Text))
               cmd.Parameters.AddWithValue("@image", (content))
               cmd.Parameters.AddWithValue("@date", (datetoday))
               cmd.Connection = SQL.conn
               cmd.ExecuteNonQuery()
               SQL.conn.Close()
           Else
               MsgBox(fName & " الصورة المختارة ليست موجودة او غير صالحة  ", vbCritical, "حصل خطأ")
           End If

       Catch ex As SqlException
           MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
       Catch ex As Exception
           MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
       End Try
   End Sub
الرد }}}
تم الشكر بواسطة:


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  [VB.NET] حل مشكلة الاتصال بقواعد البيانات access loay775 2 158 25-02-24, 06:29 AM
آخر رد: loay775
  كيفية انشاء سكريبت لقاعدة بيانات من خلال الكود heem1986 1 193 20-02-24, 12:00 AM
آخر رد: Kamil
  سؤال عن عملية حذف سجل من قاعدة البيانات assuhimi 3 249 11-02-24, 08:43 PM
آخر رد: assuhimi
  [VB.NET] منع تكرار البيانات في عند الادخال مبرمج صغير 1 2 255 24-01-24, 05:18 PM
آخر رد: مبرمج صغير 1
  [VB.NET] مساعدة في استدعاء البيانات معينه من form الأول إلى form 2 بدون التعديل loay775 2 274 18-01-24, 05:04 PM
آخر رد: loay775
  [VB.NET] إعادة استدعاء البيانات من قاعدة بيانات اكسس والاهم الصورة مبرمج صغير 1 1 273 13-01-24, 01:17 PM
آخر رد: مبرمج صغير 1
  [VB.NET] كود حفظ الصور والبيانات مره وحده مبرمج صغير 1 2 251 12-01-24, 03:04 PM
آخر رد: مبرمج صغير 1
  [كود] اريد كود إرسال البيانات من الفيجوال بيسك إلىPDF issamsaidd 10 5,829 25-12-23, 06:30 PM
آخر رد: الحزين اليماني
  اضافة ادوات لفيجوال بيسك خالد20 10 1,976 18-12-23, 07:51 PM
آخر رد: ابراهيم عبدالباقى
  جملة الاتصال بقاعدة البيانات اكسس محمد خيري 4 362 12-12-23, 03:14 AM
آخر رد: محمد خيري

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم