13-01-15, 06:45 PM
الطريقة الاحسن والاسرع تعمل كلاس وموديول
في الكلاس تكتب
Imports System.Data
Imports System.Data.SqlClient
Imports System.Globalization
Public Class Class1
Public Shared sqlcon As New SqlConnection
'دالة بناء اوامر الحفظ والحذف والتعديل >>>
Public Function SQLExicute(ByVal SQLStatmenT As String) As Boolean
Try
Dim Cmd As New SqlClient.SqlCommand
Cmd.CommandText = SQLStatmenT
sqlcon.Open()
Cmd.Connection = sqlcon
Cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
Return False
MsgBox(ex.Message)
Finally
sqlcon.Close()
End Try
End Function
End Class
__________________________________________________________________________________________
في الموديول تكتب
Imports System.Data
Imports System.Data.SqlClient
Module Module1
'المتغيرات العامة
Public wessam As New Class1
End Module
___________________________________________________________________________________________
في زر الحذف تكتب
Private Sub btn_Delete_Click(sender As Object, e As EventArgs) Handles btn_Delete.Click
If txtname.Text.Trim = "" Then
MsgBox("لا يوجد بيانات للحذف من فضلك حدد البيان المراد حذفه", MsgBoxStyle.Critical, "مدرستي")
Else
Dim deleteok As String = MsgBox("هل ترغب في حذف البيانات بالفعل؟", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "تأكيد عملية حذف البيانات")
If deleteok = vbYes Then
Dim deleteyes As Boolean = WinClass.SQLExicute("Delete from NATION where NID='" & TxtID.Text & "'")
If deleteyes = True Then
MsgBox("تمت عملية الحذف بنجاح", MsgBoxStyle.Exclamation, "وسام")
txtname.Text = ""
txtname.Enabled = False
filldatagrid()
Else
MsgBox("خطأ في عملية الحذف", MsgBoxStyle.Critical, "مدرستي")
End If
End If
End If
End Sub
End Class
_____________________________________________________________________________________________
وهذة الدالة ستحتاجها تنظيم الداتا جريد وتعبئتها
Private Sub filldatagrid()
WinClass.filldataGrid(DataGridView1, "select * from NATION")
DataGridView1.Columns(0).HeaderText = "الرقم"
DataGridView1.Columns(0).Visible = False
DataGridView1.Columns(1).HeaderText = "الجنسية"
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End Sub
___________________________________________________________________________________________
هذة الدلة في حلة الضغط على معلومة مخزنة في الداتا جريد لعرضها علي التكيست لحذفها
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
ISUpdate = True
txtname.Enabled = True
TxtID.Text = DataGridView1(0, e.RowIndex).Value
txtname.Text = DataGridView1(1, e.RowIndex).Value
End Sub
___________________________________________________________________________________________
وهذا الحدث load
Private Sub national_Load(sender As Object, e As EventArgs) Handles Me.Load
txtname.Text = ""
filldatagrid()
End Sub
في الكلاس تكتب
Imports System.Data
Imports System.Data.SqlClient
Imports System.Globalization
Public Class Class1
Public Shared sqlcon As New SqlConnection
'دالة بناء اوامر الحفظ والحذف والتعديل >>>
Public Function SQLExicute(ByVal SQLStatmenT As String) As Boolean
Try
Dim Cmd As New SqlClient.SqlCommand
Cmd.CommandText = SQLStatmenT
sqlcon.Open()
Cmd.Connection = sqlcon
Cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
Return False
MsgBox(ex.Message)
Finally
sqlcon.Close()
End Try
End Function
End Class
__________________________________________________________________________________________
في الموديول تكتب
Imports System.Data
Imports System.Data.SqlClient
Module Module1
'المتغيرات العامة
Public wessam As New Class1
End Module
___________________________________________________________________________________________
في زر الحذف تكتب
Private Sub btn_Delete_Click(sender As Object, e As EventArgs) Handles btn_Delete.Click
If txtname.Text.Trim = "" Then
MsgBox("لا يوجد بيانات للحذف من فضلك حدد البيان المراد حذفه", MsgBoxStyle.Critical, "مدرستي")
Else
Dim deleteok As String = MsgBox("هل ترغب في حذف البيانات بالفعل؟", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "تأكيد عملية حذف البيانات")
If deleteok = vbYes Then
Dim deleteyes As Boolean = WinClass.SQLExicute("Delete from NATION where NID='" & TxtID.Text & "'")
If deleteyes = True Then
MsgBox("تمت عملية الحذف بنجاح", MsgBoxStyle.Exclamation, "وسام")
txtname.Text = ""
txtname.Enabled = False
filldatagrid()
Else
MsgBox("خطأ في عملية الحذف", MsgBoxStyle.Critical, "مدرستي")
End If
End If
End If
End Sub
End Class
_____________________________________________________________________________________________
وهذة الدالة ستحتاجها تنظيم الداتا جريد وتعبئتها
Private Sub filldatagrid()
WinClass.filldataGrid(DataGridView1, "select * from NATION")
DataGridView1.Columns(0).HeaderText = "الرقم"
DataGridView1.Columns(0).Visible = False
DataGridView1.Columns(1).HeaderText = "الجنسية"
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End Sub
___________________________________________________________________________________________
هذة الدلة في حلة الضغط على معلومة مخزنة في الداتا جريد لعرضها علي التكيست لحذفها
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
ISUpdate = True
txtname.Enabled = True
TxtID.Text = DataGridView1(0, e.RowIndex).Value
txtname.Text = DataGridView1(1, e.RowIndex).Value
End Sub
___________________________________________________________________________________________
وهذا الحدث load
Private Sub national_Load(sender As Object, e As EventArgs) Handles Me.Load
txtname.Text = ""
filldatagrid()
End Sub
