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

جدولين بينها علاقة أب بابن ( رأس بأطراف ) كيف يمكن إتمام حذف الصفوف المرتبطة

بدأه m00j في 9 ديسمبر 2012 · 3 رد · 806 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

هل هناك من طريقة سهلة لإتمام حذف الصفوف في جدول الابن عند حذف صف من الجدول الأب ، وجدت أكثر من طريقة ولكن لم افهمها بشكل كامل :) والمعذرة لأني مبتدئ في فحول بيسك وآمل شرحها لعلي افهم أو اجد لديكم طريقة أسهل وشكرا

هذه طريقة اعتقد أنها تنفع مع تفعيل خاصية تتالي الحذف

والشرح مفهوم ما عدى الكود التالي

Imports System.Data.SqlServerCe

Public Class SQLCEIDHelper
    ''' <summary>
    ''' Retrieves the primary key auto-number identity values from SQL CE
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub SetPrimaryKey(ByVal trans As SqlCeTransaction, _
                                    ByVal e As SqlCeRowUpdatedEventArgs)

        ' If this is an INSERT operation...
        If e.Status = UpdateStatus.Continue AndAlso _
           e.StatementType = StatementType.Insert Then
            Dim pk = e.Row.Table.PrimaryKey
            ' and a primary key PK column exists...
            If pk IsNot Nothing AndAlso pk.Count = 1 Then
                'Set up the post-update query to fetch new @@Identity
                Dim cmdGetIdentity As New SqlCeCommand("SELECT @@IDENTITY", _
                                                       CType(trans.Connection, SqlCeConnection), _
                                                       trans)

                'Execute the command and set the result identity value to the PK
                e.Row(pk(0)) = CInt(cmdGetIdentity.ExecuteScalar)
                e.Row.AcceptChanges()

            End If
        End If
    End Sub
End Class

Namespace MyDatabaseDataSetTableAdapters
    Partial Public Class CategoriesTableAdapter
        Private Sub _adapter_RowUpdated(ByVal sender As Object, _
                                        ByVal e As SqlCeRowUpdatedEventArgs) _
                                        Handles _adapter.RowUpdated

            SQLCEIDHelper.SetPrimaryKey(Me.Transaction, e)
        End Sub
    End Class

    Partial Public Class ProductsTableAdapter
        Private Sub _adapter_RowUpdated(ByVal sender As Object, _
                                        ByVal e As SqlCeRowUpdatedEventArgs) _
                                        Handles _adapter.RowUpdated

            SQLCEIDHelper.SetPrimaryKey(Me.Transaction, e)
        End Sub
    End Class
End Namespace

Partial Class MyDatabaseDataSet
    Partial Class CategoriesDataTable
        Private Sub CategoriesDataTable_TableNewRow(ByVal sender As Object, _
                                            ByVal e As System.Data.DataTableNewRowEventArgs) _
                                            Handles Me.TableNewRow
            'Set defaults so that constraints don't fail when EndEdit is called
            Dim cat = CType(e.Row, CategoriesRow)
            cat.CategoryName = "[new]"
        End Sub
    End Class

    Partial Class ProductsDataTable
        Private Sub ProductsDataTable_TableNewRow(ByVal sender As Object, _
                                             ByVal e As System.Data.DataTableNewRowEventArgs) _
                                             Handles Me.TableNewRow
            'Set defaults so that constraints don't fail when EndEdit is called
            Dim product = CType(e.Row, ProductsRow)
            product.ProductName = "[new]"
        End Sub
    End Class
End Class
][/codImports System.Data.SqlServerCe

Public Class SQLCEIDHelper
    ''' <summary>
    ''' Retrieves the primary key auto-number identity values from SQL CE
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub SetPrimaryKey(ByVal trans As SqlCeTransaction, _
                                    ByVal e As SqlCeRowUpdatedEventArgs)

        ' If this is an INSERT operation...
        If e.Status = UpdateStatus.Continue AndAlso _
           e.StatementType = StatementType.Insert Then
            Dim pk = e.Row.Table.PrimaryKey
            ' and a primary key PK column exists...
            If pk IsNot Nothing AndAlso pk.Count = 1 Then
                'Set up the post-update query to fetch new @@Identity
                Dim cmdGetIdentity As New SqlCeCommand("SELECT @@IDENTITY", _
                                                       CType(trans.Connection, SqlCeConnection), _
                                                       trans)

                'Execute the command and set the result identity value to the PK
                e.Row(pk(0)) = CInt(cmdGetIdentity.ExecuteScalar)
                e.Row.AcceptChanges()

            End If
        End If
    End Sub
End Class

Namespace MyDatabaseDataSetTableAdapters
    Partial Public Class CategoriesTableAdapter
        Private Sub _adapter_RowUpdated(ByVal sender As Object, _
                                        ByVal e As SqlCeRowUpdatedEventArgs) _
                                        Handles _adapter.RowUpdated

            SQLCEIDHelper.SetPrimaryKey(Me.Transaction, e)
        End Sub
    End Class

    Partial Public Class ProductsTableAdapter
        Private Sub _adapter_RowUpdated(ByVal sender As Object, _
                                        ByVal e As SqlCeRowUpdatedEventArgs) _
                                        Handles _adapter.RowUpdated

            SQLCEIDHelper.SetPrimaryKey(Me.Transaction, e)
        End Sub
    End Class
End Namespace

Partial Class MyDatabaseDataSet
    Partial Class CategoriesDataTable
        Private Sub CategoriesDataTable_TableNewRow(ByVal sender As Object, _
                                            ByVal e As System.Data.DataTableNewRowEventArgs) _
                                            Handles Me.TableNewRow
            'Set defaults so that constraints don't fail when EndEdit is called
            Dim cat = CType(e.Row, CategoriesRow)
            cat.CategoryName = "[new]"
        End Sub
    End Class

    Partial Class ProductsDataTable
        Private Sub ProductsDataTable_TableNewRow(ByVal sender As Object, _
                                             ByVal e As System.Data.DataTableNewRowEventArgs) _
                                             Handles Me.TableNewRow
            'Set defaults so that constraints don't fail when EndEdit is called
            Dim product = CType(e.Row, ProductsRow)
            product.ProductName = "[new]"
        End Sub
    End Class
End Class

الحل الثاني ذكره مشرف في المنتدى بس ما فهمته أبدا على هذا الرابط

آمل شرح فكرة remove at

وشكرا لكم

#2

داخل قاعدة البيانات علاقات بين الاجداول يمكن انتجعلاها عند حذف الاب يحذف الابناء

#3

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

أعتقد أنك تقصد حذف السجل الأساسى وكل السجلات الفرعية المرتبطة به فى الجدول الفرعى

لو كان كذلك...

هناك طريقة سهلة وبسيطة وحتى لا تحتاج لربط الجدولين بنظام Cascading

الفكرة تتلخص فى...

حذف السجل الذى مفتاحه الأساسى يساوى قيمة معينة

ثم حذف كل السجلات من الجدول الفرعى والتى مفتاحها الفرعى يساوى نفس القيمة

على سبيل المثال

عندنا جدولان الفواتير وتفاصيلها

فربما تريد حذف الفاتورة رقم 5 وكل عناصرها من جدول التفاصيل

كل ما عليك هو كتابة أمرين Delete فى CommandText الخاصة بـ SQLCommand كالآتى...

Dim cmd as New SQLCommand

cmd.CommandText ="delete from Orders where OrderID =5; delete from OrderDetails where OrderID = 5"

لاحظ أن الأمرين مفصولان بفاصلة منقوطة ; وبذلك فإنك تستطيع تنفيذ عدة أوامر مرة واحدة

ملحوظة:

إذا كان الجدولان مرتبطين فيجب الحذف من الجدول الفرعى أولا

أدعو الله أن يكون هذا مفيدا

تم تعديل هذه المشاركة بواسطة ema2000_t في 10 ديسمبر 2012 في 00:26

4

أدعو الله أن يجعل أعمالنا خالصة لوجهه الكريم --- وأن ينفعنا بما علمنا

Dr Sayed Tohamy

Programming Instructor

مدرب برمجة

---------------

EmaSoft2000

-------------------

#4

أخ كريم شكرًا على مرورك ولكن قاعدة البيانات من نوع Sdf لا يتمم عملية الحذف حتى وإن عملت cascade بين الجداول المرتبطة ،، وبالتالي ظهرت حلول منها الحل المذكور في الأعلى ولكنه معقد قليلا ،،

الأخ Ema2000_t بالفعل هذا ما اقصد وإجابتك أفادتني كثيرا ،، وجزاك الله خير ،، أشكرك كثيراً و سأجرب الفكرة

تم تعديل هذه المشاركة بواسطة m00j في 10 ديسمبر 2012 في 05:31

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

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

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

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

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