الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

الرجاء المساعدة بطريقة التنقل بين الصور المعروضه في PictureBox من قاعدة البيانات

بدأه jemmynet في 10 يناير 2012 · 3 رد · 938 مشاهدة · في ADO.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

بسم الله الرحمن الرحيم

الرجاء المساعدة بي طريقة التنقل بين الصور المخزنه في قاعدة البيانات المعروضه في Picture Box

صوره توضح الفورم

f07zF.jpg

اريد مثل هذا الكود الى يتنقل بي النصوص والارقام يكون للصور المخزنة بي قاعدة البيانات

   Private Sub Moving()

        Using ms As New MemoryStream(CType(ds.Tables("pice").Rows(0)("pice_img"), Byte()))
            Dim img As Image = Image.FromStream(ms)
            Me.PictureBox_view.Image = img


            Dim MyPosition As Integer = Me.BindingContext(ds, "pice").Position

            Me.pice_id_Lab.Text = ds.Tables("pice").Rows(MyPosition).Item("pice_id")

            Me.pice_name_lab.Text = ds.Tables("pice").Rows(MyPosition).Item("pice_name")

            Me.PictureBox_view.Image = img"  ' المشكلة هنا لااعرف كيف اعمل له اضافة مثل الحقول السابقة

            Me.all_pice_Lab.Text = ds.Tables("pice").Rows(MyPosition).Item("all_pice")

            Me.price_price_in_sa_Lab.Text = ds.Tables("pice").Rows(MyPosition).Item("pice_price_in_sa")


        End Using
    End Sub

 هذا كود الذهاب الى السجل التالي يبدل النصوص اما الصور لا تتغير

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click

        Me.BindingContext(ds, "pice").Position = (Me.BindingContext(ds, "pice").Count + 1)

        Moving()

    End Sub

المشروع بي المرفقات

او هنا

ORDER.rar

#2

انا لا اخزن الصور في قاعدة البيانات لكن اخزن مسار الصورة واستطيع التنقل

تزودوا فأن خيرا الزاد التقوى

اللهم انصر اخواننا المسلمين المجاهدين في كل مكان

#3

اخي الكريم جرب الاتي

قم بتعريف هذه المتغيرات في Declaration Area

Option Strict On
Imports System.Data.SqlClient
Imports System.Text
Imports System.IO

وبعدها في Calss Form Declaration اكتب التالي

 Protected Const SQL_CONNECTION_STRING As String = _
        "Data Source=JAMAL-PC\JAMALSQLSERVER;Initial Catalog=Northwind;User ID=me;Password=123"

       Protected Const CONNECTION_ERROR_MSG As String = _
        "To run this sample, you must have SQL " & _
        "or MSDE with the Northwind database installed.  For " & _
        "instructions on installing MSDE, view the ReadMe file."

    Protected da As SqlDataAdapter
    Protected cbd As SqlCommandBuilder
    Protected dsPictures As DataSet
    Protected didPreviouslyConnect As Boolean = False
    Protected connectionString As String = SQL_CONNECTION_STRING

وبعدها ضع خمسة Buttons على الفورم

1- Brows

2- Save

3- Delete

4- Display

5-CreatePictable

وبعدها ضع 2 Picture Box

و ListBox و OpenFileDialog وLable وغير اسمه إلى lblFilePath

وفي Brows أكتب التالي

 ' Handles the Browse button click event, allowing the user to find an image, 
    ' display it in a PictureBox control, and save the image to the database.
    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        ' Use an OpenFileDialog to enable the user to find an image to save to the 
        ' database. Provide a pipe-delimited set of pipe-delimited pairs of file 
        ' types that will appear in the dialog. Set the FilterIndex to the default 
        ' file type.
        With OpenFileDialog1
            .InitialDirectory = "C:\"
            .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
            .FilterIndex = 2
        End With

        ' When the user clicks the Open button (DialogResult.OK is the only option;
        ' there is not DialogResult.Open), display the image centered in the 
        ' PictureBox and display the full path of the image.
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            With PictureBox1
                .Image = Image.FromFile(OpenFileDialog1.FileName)
                .SizeMode = PictureBoxSizeMode.CenterImage
                .BorderStyle = BorderStyle.Fixed3D
            End With
            lblFilePath.Text = OpenFileDialog1.FileName
        End If
    End Sub

وفي Save أكتب التالي

' Handles the Save button click event, allowing the user to save the image
    ' viewed in the PictureBox to the database.
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        ' PREPARE DATA TO BE PASSED TO DATABASE:
        ' You only need to save the filename, not the entire path. Therefore, 
        ' Split the path, creating an array of strings. Make sure you pass in 
        ' the delimiter. Then reverse the array so that you can assign the 
        ' first string in the array to the SQL parameter.
        Dim arrFilename() As String = Split(lblFilePath.Text, "\")
        Array.Reverse(arrFilename)

        ' The SQL Server Image datatype is a binary datatype. Therefore, to save 
        ' it to the database you must convert the image to an array of bytes. You
        ' could use a FileStream object to open the image file and then read it to 
        ' the stream, but a MemoryStream with the Image.Save method is a bit easier.
        Dim ms As New MemoryStream
        PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
        Dim arrImage() As Byte = ms.GetBuffer

        ' Close the stream object to release the resource.
        ms.Close()

        ' Display a status message saying that we're attempting to connect.
        ' This only needs to be done the very first time a connection is
        ' attempted.  After we've determined that MSDE or SQL Server is
        ' installed, this message no longer needs to be displayed.
        Dim frmStatusMessage As New frmStatus
        If Not didPreviouslyConnect Then
            frmStatusMessage.Show("Connecting to SQL Server")
        End If

        ' Attempt to connect to the local SQL server instance, and a local
        ' MSDE installation (with Northwind).  
        Dim isConnecting As Boolean = True
        While isConnecting

            Try
                ' The SqlConnection class allows you to communicate with SQL Server.
                ' The constructor accepts a connection string as an argument.  This
                ' connection string uses Integrated Security, which means that you 
                ' must have a login in SQL Server, or be part of the Administrators
                ' group for this to work.
                Dim northwindConnection As New SqlConnection(connectionString)
                Dim strSQL As String = _
                    "INSERT INTO Picture (Filename, Picture)" & _
                    "VALUES (@Filename, @Picture)"

                ' A SqlCommand object is used to execute the SQL statement.
                Dim cmd As New SqlCommand(strSQL, northwindConnection)
                With cmd
                    ' Add parameters required by SQL statement. PictureID is an 
                    ' identity field (in Microsoft Access, an AutoNumber field), 
                    ' so you only need to pass values for the two remaining fields.
                    .Parameters.Add(New SqlParameter("@Filename", _
                        SqlDbType.NVarChar, 50)).Value = arrFilename(0)
                    .Parameters.Add(New SqlParameter("@Picture", _
                        SqlDbType.Image)).Value = arrImage
                End With

                ' Open the connection, execute the command, and close the 
                ' connection. It is more efficient to ExecuteNonQuery when data 
                ' is not being returned.
                northwindConnection.Open()
                cmd.ExecuteNonQuery()
                northwindConnection.Close()

                ' Data has been successfully submitted, so break out of the loop
                ' and close the status form.
                isConnecting = False
                didPreviouslyConnect = True
                frmStatusMessage.Close()
                MessageBox.Show(arrFilename(0) & " saved to the database.", _
                    "Image Save Status", MessageBoxButtons.OK, _
                    MessageBoxIcon.Information)

            Catch sqlExc As SqlException
                MessageBox.Show(sqlExc.ToString, "SQL Exception Error!", _
                    MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit While
            Catch exc As Exception
                If connectionString = SQL_CONNECTION_STRING Then
                    ' Couldn't connect to SQL Server.  Now try MSDE.
                    connectionString = MSDE_CONNECTION_STRING
                    frmStatusMessage.Show("Connecting to MSDE")
                Else
                    ' Unable to connect to SQL Server or MSDE
                    frmStatusMessage.Close()
                    MessageBox.Show(CONNECTION_ERROR_MSG, _
                            "Connection Failed!", MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
                    End
                End If
            End Try
        End While
    End Sub

وفي Display أكتب التالي

' Handles the Display button click event, allowing the user to display an image

' stored in the database.

Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

' When nothing is selected in the ListBox, the SelectedIndex = -1.

If lstPictures.SelectedIndex < 0 Then

MessageBox.Show("There are no images in the database to display.", _

"Empty Database!", MessageBoxButtons.OK, MessageBoxIcon.Information)

Else

' The SQL Server Image datatype is a binary datatype. Therefore, to

' generate an image from it you must first create a stream object

' containing the binary data. Then you can generate the image by

' calling Image.FromStream().

Dim arrPicture() As Byte = _

CType(dsPictures.Tables(0).Rows(lstPictures.SelectedIndex)("Picture"), _

Byte())

Dim ms As New MemoryStream(arrPicture)

With PictureBox2

.Image = Image.FromStream(ms)

.SizeMode = PictureBoxSizeMode.CenterImage

.BorderStyle = BorderStyle.Fixed3D

End With

lblFileName.Text = _

dsPictures.Tables(0).Rows(lstPictures.SelectedIndex)("FileName").ToString

' Close the stream object to release the resource.

ms.Close()

End If

End Sub

وفي Delete أكتب التالي

 ' Handles the Delete button click event, allowing the user to delete an image 
    ' stored in the database.
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        ' When nothing is selected in the ListBox, the SelectedIndex = -1.
        If lstPictures.SelectedIndex < 0 Then
            MessageBox.Show("There are no images in the database to delete.", _
                "Empty Database!", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            ' Use the SqlCommandBuilder to get a delete command and update the DataSet
            ' after you delete the row in the DataSet that corresponds with the picture
            ' the user has selected.
            dsPictures.Tables(0).Rows(lstPictures.SelectedIndex).Delete()
            da.UpdateCommand = cbd.GetDeleteCommand
            da.Update(dsPictures)

            ' Clear the image and filename
            lblFileName.Text = ""
            PictureBox2.Image = Nothing
        End If
    End Sub

في CreatePicTable

هو اختياري تستطيع ان تجرب على الجدول الذي عندك

هنا يقوم البرنامج بأضافة جدول اسمه Picture الى قاعدة البيانات به الحقول المطلوبة وما يهمنا هما الحقلين FileName ,Picture

 ' Handles the Create Table button click event, allowing the user to drop (if it exists)
    ' and create the Picture table in the Northwind database.
    Private Sub btnModifyDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifyDB.Click
        Dim strSQL As String = _
            "IF EXISTS (" & _
            "SELECT * " & _
            "FROM northwind.dbo.sysobjects " & _
            "WHERE NAME = 'Picture' " & _
            "AND TYPE = 'u')" & vbCrLf & _
            "BEGIN" & vbCrLf & _
            "DROP TABLE northwind.dbo.picture" & vbCrLf & _
            "END" & vbCrLf & _
            "CREATE TABLE Picture (" & _
            "PictureID Int IDENTITY(1,1) NOT NULL," & _
            "[FileName] Varchar(255) NOT NULL," & _
            "Picture Image NOT NULL," & _
            "CONSTRAINT [PK_Picture] PRIMARY KEY CLUSTERED" & _
            "(PictureID))"

        ' Display a status message saying that we're attempting to connect.
        ' This only needs to be done the very first time a connection is
        ' attempted.  After we've determined that MSDE or SQL Server is
        ' installed, this message no longer needs to be displayed.
        Dim frmStatusMessage As New frmStatus
        If Not didPreviouslyConnect Then
            frmStatusMessage.Show("Connecting to SQL Server")
        End If

        ' Attempt to connect to the local SQL server instance, and a local
        ' MSDE installation (with Northwind).  
        Dim isConnecting As Boolean = True
        While isConnecting
            Try
                ' The SqlConnection class allows you to communicate with SQL Server.
                ' The constructor accepts a connection string as an argument.  This
                ' connection string uses Integrated Security, which means that you 
                ' must have a login in SQL Server, or be part of the Administrators
                ' group for this to work.
                Dim northwindConnection As New SqlConnection(connectionString)

                ' A SqlCommand object is used to execute the SQL commands.
                Dim cmd As New SqlCommand(strSQL, northwindConnection)

                ' Open the connection, execute the command, and close the connection.
                ' It is more efficient to ExecuteNonQuery when data is not being 
                ' returned.
                northwindConnection.Open()
                cmd.ExecuteNonQuery()
                northwindConnection.Close()

                ' Data has been successfully submitted, so break out of the loop
                ' and close the status form.
                isConnecting = False
                didPreviouslyConnect = True
                frmStatusMessage.Close()
                MessageBox.Show("Table successfully created.", "Table Creation Status", _
                    MessageBoxButtons.OK, MessageBoxIcon.Information)

            Catch sqlExc As SqlException
                MessageBox.Show(sqlExc.ToString, "SQL Exception Error!", _
                    MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit While
            Catch exc As Exception
                If connectionString = SQL_CONNECTION_STRING Then
                    ' Couldn't connect to SQL Server.  Now try MSDE.
                    connectionString = MSDE_CONNECTION_STRING
                    frmStatusMessage.Show("Connecting to MSDE")
                Else
                    ' Unable to connect to SQL Server or MSDE
                    frmStatusMessage.Close()
                    MessageBox.Show(CONNECTION_ERROR_MSG, _
                            "Connection Failed!", MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
                    End
                End If
            End Try
        End While
        frmStatusMessage.Close()
    End Sub

اتمنى ان ينال اعجابك ويساعدك في ماتريد

تزودوا فأن خيرا الزاد التقوى

اللهم انصر اخواننا المسلمين المجاهدين في كل مكان

#4

السلام عليكم ورحمه الله وبركاته

اخي ممكن ترفق قاعدة البيانات؟

.:: Wish to become better and better ::.

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…