07-05-14, 04:48 PM
مثال بسيط
مربع نص textbox1
زر امر button1
شبيكة بيانات datagridview باسم dgv1
وهذا الكلاس
مربع نص textbox1
زر امر button1
شبيكة بيانات datagridview باسم dgv1
وهذا الكلاس
كود :
Imports System.Data.OleDb
Public Class Form1
Dim str As String = "provider=microsoft.ace.oledb.12.0;data source=|datadirectory|\students.accdb"
Dim con As New OleDbConnection(str)
Dim ds As New DataSet
Private Sub dgds(ByVal d As DataSet)
If dgv1.InvokeRequired Then
dgv1.Invoke(New Action(Of DataSet)(AddressOf dgds), d)
Else
dgv1.DataSource = d
End If
End Sub
Private Sub dgv(ByVal d As String)
If dgv1.InvokeRequired Then
dgv1.Invoke(New Action(Of String)(AddressOf dgv), d)
Else
dgv1.DataMember = d
End If
End Sub
Private Sub gd()
dgds(Nothing)
dgv(Nothing)
ds.Clear()
Dim da As New OleDbDataAdapter("select id_number from s1", con)
da.Fill(ds, "s1")
dgds(ds)
dgv("s1")
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
begin()
End Sub
Private Sub begin()
Dim ath As New Threading.Thread(AddressOf gd)
If ath.IsAlive = False Then
ath.Start()
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If isfound(TextBox1.Text) Then
MsgBox("الرقم محجوز", MsgBoxStyle.Critical, "")
Exit Sub
End If
If addnew(TextBox1.Text) Then
MsgBox("تم الحفظ", MsgBoxStyle.Information, "")
Else
MsgBox("لم يتم الحفظ لوجود خطا", MsgBoxStyle.Critical, "")
End If
End Sub
Private Function isfound(ByVal txt As String) As Boolean
Dim cm As New OleDbCommand("select count(tid) from s1 where id_number=@id_number", con)
cm.Parameters.AddWithValue("@id_number", txt)
If con.State = ConnectionState.Closed Then
con.Open()
End If
If cm.ExecuteScalar > 0 Then
Return True
Else
Return False
End If
End Function
Private Function addnew(ByVal txt As String) As Boolean
Dim cm As New OleDbCommand("insert into s1(id_number) values(@id_number)", con)
cm.Parameters.AddWithValue("@id_number", txt)
If con.State = ConnectionState.Closed Then
con.Open()
End If
Try
cm.ExecuteNonQuery()
begin()
Return True
Catch ex As Exception
Return False
End Try
End Function
End Classاللهم إني أعوذ بك من غلبة الدين وغلبة العدو، اللهم إني أعوذ بك من جهد البلاء ومن درك الشقاء ومن سوء القضاء ومن شماتة الأعداء
اللهم اغفر لي خطيئتي وجهلي، وإسرافي في أمري وما أنت أعلم به مني، اللهم اغفر لي ما قدمت وما أخرت، وما أسررت وما أعلنت وما أنت أعلم به مني، أنت المقدم وأنت المؤخر وأنت على كل شيء قدير

