تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
ماهي طريقة الاتصال بقاعدة البيانات الموجودة في موقع الانترنت
#5
.....

بعد الاطلاع على هذا الموضوع وموضوع آخر لك (ماهي المشكلة في مشروعي . ربط قاعدة بيانات mysql)




منقول مع تعديل بسيط ليناسب طلبك


كود :
Imports MySql.Data.MySqlClient

Public Class Form1

    Dim con As New MySqlConnection(" Server = db4free.net;  Port = 3306;  Database = vbnet; " _
                                 & " User id = vbnet;  Password = 10241024;                 ")


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' إعدادات خصائص بعض الأدوات يمكن الإستغناء عن هذه الأسطر بتعيينها من خلال نافذة الخصائص
        Me.btnInsert.Text = "إضافة"
        Me.btnUpdate.Text = "تحديث"
        Me.btnDelete.Text = "حذف"
        Me.btnInsert.Enabled = False
        Me.btnUpdate.Enabled = False
        Me.btnDelete.Enabled = False
        Me.txtPID.Enabled = False
        '---
        Me.DataGridView1.AllowUserToAddRows = False
        Me.DataGridView1.AllowUserToDeleteRows = False
        Me.DataGridView1.ReadOnly = True
        Me.DataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
        Me.DataGridView1.MultiSelect = False
        '------------------------
        ' إستعجال ظهور النافذة
        Me.Show()
        Application.DoEvents()
        '------------------------
        ' إستدعاء تعبئة البيانات
        If DataGridView1Fill() Then
            Me.btnInsert.Enabled = True
        End If
    End Sub



    ' إضافة
    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click

        If Me.btnInsert.Text = "جديد" Then
            ' إستدعاء الوضع الإفتراضي للأدوات
            Reset()
            Exit Sub
        End If

        If (Me.txtName.Text.Trim = String.Empty) Or (Not IsNumeric(Me.txtPrice.Text)) Then
            MsgBox("تأكد من البيانات")
            Exit Sub
        End If

        Try
            Dim sql As String = " INSERT INTO `products` ( `name`, `price`, `description`, `expiredate` ) " & _
                                "                 Values ( @name,  @price,  @description,  @expiredate  ) "
            Using cm As New MySqlCommand(sql, con)
                cm.Parameters.AddWithValue("@name", Me.txtName.Text.Trim)
                cm.Parameters.AddWithValue("@price", Me.txtPrice.Text.Trim)
                cm.Parameters.AddWithValue("@description", Me.txtDescription.Text.Trim)
                'cm.Parameters.AddWithValue("@expiredate", Me.DateTimePicker1.Value.Date) '' إذا كان الحقل تاريخ فقط بدون وقت
                cm.Parameters.AddWithValue("@expiredate", Me.DateTimePicker1.Value) '' هنا التاريخ مع الوقت

                con.Open()
                cm.ExecuteNonQuery()
                con.Close()

            End Using

            ' إستدعاء الوضع الإفتراضي للأدوات
            Reset()

            MsgBox("تمت الإضافة بنجاح")

            DataGridView1Fill()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub



    ' تحديث
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

        If MsgBox("هل أنت متأكد من البيانات", MsgBoxStyle.MsgBoxRight + MsgBoxStyle.MsgBoxRtlReading + MsgBoxStyle.YesNo + MsgBoxStyle.Question) <> MsgBoxResult.Yes Then
            Exit Sub
        End If

        If (Val(Me.txtPrice.Text) < 0) _
              Or (Me.txtName.Text.Trim = String.Empty) _
              Or (Not IsNumeric(Me.txtPrice.Text)) _
              Then
            MsgBox("تأكد من البيانات")
            Exit Sub
        End If

        Try
            Dim sql As String = " UPDATE `products`                 " & _
                                " SET `name`  = @name               " & _
                                "   , `price` = @price              " & _
                                "   , `description` = @description  " & _
                                "   , `expiredate`  = @expiredate   " & _
                                " WHERE `pid` = @pid                "

            Using cm As New MySqlCommand(sql, con)
                cm.Parameters.AddWithValue("@name", Me.txtName.Text.Trim)
                cm.Parameters.AddWithValue("@price", Me.txtPrice.Text.Trim)
                cm.Parameters.AddWithValue("@description", Me.txtDescription.Text.Trim)
                cm.Parameters.AddWithValue("@expiredate", Me.DateTimePicker1.Value)
                cm.Parameters.AddWithValue("@pid", Val(Me.txtPID.Text))

                con.Open()
                cm.ExecuteNonQuery()
                con.Close()

            End Using

            ' إستدعاء الوضع الإفتراضي للأدوات
            Reset()

            MsgBox("تم التحديث بنجاح")

            DataGridView1Fill()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub



    ' حذف
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If MsgBox("هل أنت متأكد من حذف البيانات", MsgBoxStyle.MsgBoxRight + MsgBoxStyle.MsgBoxRtlReading + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2 + MsgBoxStyle.Exclamation) <> MsgBoxResult.Yes Then
            If MsgBox("هل أنت فعلاً متأكد من حذف البيانات", MsgBoxStyle.MsgBoxRight + MsgBoxStyle.MsgBoxRtlReading + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2 + MsgBoxStyle.Exclamation) <> MsgBoxResult.Yes Then
                Exit Sub
            End If
        End If

        Try
            Dim sql As String = " Delete From `products`    " & _
                                " WHERE `pid` = @pid        "
            Using cm As New MySqlCommand(sql, con)
                cm.Parameters.AddWithValue("@pid", Val(Me.txtPID.Text))

                con.Open()
                cm.ExecuteNonQuery()
                con.Close()

            End Using

            ' إستدعاء الوضع الإفتراضي للأدوات
            Reset()

            MsgBox("تم الحذف بنجاح")

            DataGridView1Fill()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub



    ' تعبئة أداة الجدول بالبيانات
    Public Function DataGridView1Fill() As Boolean
        Try
            Using da As New MySqlDataAdapter("Select * from `products`", con)
                Using dt As New DataTable
                    da.Fill(dt)
                    DataGridView1.DataSource = dt
                    If DataGridView1.Rows.Count > 0 Then
                        DataGridView1.Columns(0).HeaderText = "ID"
                        DataGridView1.Columns(0).Width = 50
                        DataGridView1.Columns(1).HeaderText = "الإسم"
                        DataGridView1.Columns(1).Width = 150
                        DataGridView1.Columns(2).HeaderText = "السعر"
                        DataGridView1.Columns(2).Width = 70
                        DataGridView1.Columns(3).HeaderText = "الوصف"
                        DataGridView1.Columns(3).Width = 200
                        DataGridView1.Columns(4).HeaderText = "تاريخ الصلاحية"
                        DataGridView1.Columns(4).Width = 110
                    End If
                End Using
            End Using

            Return True

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function



    ' الاختيار بالنقر المزدوج

    ' عند النقر المزدوج رأس السطر
    Private Sub DataGridView1_RowHeaderMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseDoubleClick
        RowSelect(e.RowIndex)
    End Sub
    ' عند النقر المزدوج على أي خلية في سطر
    Private Sub DataGridView1_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
        RowSelect(e.RowIndex)
    End Sub
    ' قراءة بيانات السطر الذي تم اختياره بالنقر المزدوج
    Private Sub RowSelect(ByVal rowIndex As Integer)
        Me.txtPID.Text = DataGridView1.Item(0, rowIndex).Value
        Me.txtName.Text = DataGridView1.Item(1, rowIndex).Value
        Me.txtPrice.Text = DataGridView1.Item(2, rowIndex).Value
        Me.txtDescription.Text = DataGridView1.Item(3, rowIndex).Value
        Me.DateTimePicker1.Text = DataGridView1.Item(4, rowIndex).Value
        Me.btnInsert.Text = "جديد"
        Me.btnUpdate.Enabled = True
        Me.btnDelete.Enabled = True
        Me.DataGridView1.Enabled = False
    End Sub



    ' إعادة الأدوات للوضع الإفتراضي
    Private Sub Reset()

        Me.txtPID.Text = ""
        Me.txtName.Text = ""
        Me.txtPrice.Text = ""
        Me.txtDescription.Text = ""
        Me.DateTimePicker1.Text = Now

        Me.btnInsert.Text = "إضافة"
        Me.btnUpdate.Enabled = False
        Me.btnDelete.Enabled = False

        Me.DataGridView1.Enabled = True

    End Sub


End Class

.....


الملفات المرفقة
.rar   WindowsApplication1.rar (الحجم : 160.5 ك ب / التحميلات : 117)
الرد }}}
تم الشكر بواسطة: Amir_Alzubidy


الردود في هذا الموضوع
RE: ماهي طريقة الاتصال بقاعدة البيانات الموجودة في موقع الانترنت - بواسطة vbnet - 28-08-14, 01:05 PM

المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  استفسارات عن قواعد البيانات مبتدئ بوحمد 0 1,009 06-11-24, 10:15 AM
آخر رد: بوحمد
  [C#.NET] مشكلة في مجلد قاعدة البيانات kamel1978 3 691 06-10-24, 06:26 PM
آخر رد: Taha Okla
  ما هو كود معرفة نوع البيانات من اكسس؟ justforit 6 1,054 23-05-24, 08:22 AM
آخر رد: مصمم هاوي
  [سؤال] حول مشكلة توافق نوع البيانات عند الاستعلام في اكسل justforit 4 697 23-05-24, 05:35 AM
آخر رد: justforit
  ماهي الجداول التي احتاجها لنظام رواتب متكامل Adel27213 0 638 03-11-23, 09:41 PM
آخر رد: Adel27213
  مشكلة عند الاتصال ب sql server 2014 bassant 1 1,331 27-08-23, 12:26 PM
آخر رد: رضوان الجماعي
  [سؤال] عدم إظهار الصور مع النص فى Listview من قاعدة البيانات alims 2 2,035 27-08-23, 12:22 PM
آخر رد: رضوان الجماعي
  [SQL] استفسار عن طريقة تحزم قواعد بيانات MSSQL مع البرنامج VB.NET2019 salemq 2 868 18-05-23, 03:02 AM
آخر رد: سعود
  استفسار عن تخزين الوقت في قاعدة البيانات strongriseman 2 1,571 22-04-22, 07:27 PM
آخر رد: strongriseman
  [سؤال] مشكلة فى عرض الصورة من ادخل قاعدة البيانات alims 2 1,712 17-11-21, 10:09 AM
آخر رد: سعود

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


يقوم بقرائة الموضوع: