29-09-13, 12:37 PM
السلام عليكم شباب
اليوم اسوف اشرح الاكود الدخلية للمكتبة alysbisydata و كفية تم التصميم هذى المكتبة و هى اهداء لجميع الاعضاء منتدى فيجوال بيسك لكل العرب
فى الاول هذى المكتبة تعمل مع قواعد البيانات من نوع اكسس و سيكوال و يمكن اضافة الاوركل
تم استخدام حلقات التكرار فى انشاء المكتبة For , For Each
و يكوم فى الربط شرح هذى الحلقات
هنا
و تم استخدام الجملة الشرطية IF
و يكوم فى الربط شرح الجملة الشرطية IF
هنا
و ثالث شى مستخدام فى المكتبة هى Try
و كل المطلوب منها كشف الاخطاء
الشرح عن طريق الكود
نقوم بعمل دلة
قومت بعمل متغير اسمو Control من نوع Form و ده علشان اقدر اعرف اسم الفورم لى هعمل عليها
و strcon لجلب اسم نص الاصتلا بقاعدة البيانات
و Table لجلب اسم الجدوال المراد التعامل
اكسس
سيكوال
و بعد ذلك اضافة متيغير جديد من نوع OleDbConnection لجلب نص الاتصال بقاعدة البيانات و الربط مع المكتبة الخاصة بالاتصال سؤا كان اكسس او سيكوال
و بعد ذلك نكشف حالة الاتصال بقاعدة البيانات فى حالة مفتوح نقوم باغلاق الاتصال بقاعدة البيانات
اكسس
سيكوال
تم استخدام try
و بعد ذلك تم فتح الاتصال بقاعدة البيانات con.Open
و بعد كده تم عمل استعلام عن الجدوال و بالطبع هنا قومت بجلب اسم المتغير لى حامل اسم الجدال من اعلى و بقاى الكود معروف للجميع و بعد ذلك استخدام For لجلب الكنترول
و هنا قومت بحلب الكنتروال الموجود على الفروم عن طريق اسم الفروم من المتغير Control و بعد كده دورت على كل الكنترول لى اسمو TextBox
و بعد ذلك قوم بعمل المتغير s و هو يساوى اسم المتغير Control لجلب اسم الكنترول
و بعد كده قومت بعمل متغير تانى علشان جيب البيانات من المتغير نفسو و هو المتغير w
و بعد ذلك قوم بعمل اضافة فى قاعدة البيانات عن طريق اسم الكنترول لى هو يساوى اسم الحقل فى قاعدة البيانات و هو المتغير s و بعد ذلك اضافة البيانات فى الحقل عن طريق المتغير w
و بعد ذلك انهاء القاعدة الشرطية End If
و السؤال هنا ليه حط الحلقة التكرار For بعد Try
هقولك ليه بس اوعى تقول حد
دلوقتى فى حالة ان فى كنترول من نوع TextBox اسمو مش موجود فى الجدوال هنا هيحصل مشكلة و سوف يتم تحويلك الى Catch ex As Exception الطبيعى هنا ان مش احط كود تانى لحل المشكلة و لكن اسيبو فارغ علشان يذهب الى Next لى بدوره هترجعو تانى علشان يجيب باقى الكنتروال TextBox بس كده سهله
و بعد ذلك يتم اضافة البيانات فى الجدوال و لعم تحديث و ثم اغلاق الاتصال بقاعدة البيانات
و اظهر الرسائل تفيد ان تم العملية بنجاح و فى حالة و جود خطاء فى كتب اسماء اسر الكنتروال TextBox بنفس اسم الحقوال قى الجدوال يتم اظهر هذى الرسائل
و اليكم الكود كمل يعم مع قاعدة بيانات اكسس و اترك لكم تغيرمن اكسس الى سيكوال او اروكل
شكرا ليكم
اليوم اسوف اشرح الاكود الدخلية للمكتبة alysbisydata و كفية تم التصميم هذى المكتبة و هى اهداء لجميع الاعضاء منتدى فيجوال بيسك لكل العرب
فى الاول هذى المكتبة تعمل مع قواعد البيانات من نوع اكسس و سيكوال و يمكن اضافة الاوركل
تم استخدام حلقات التكرار فى انشاء المكتبة For , For Each
و يكوم فى الربط شرح هذى الحلقات
هنا
و تم استخدام الجملة الشرطية IF
و يكوم فى الربط شرح الجملة الشرطية IF
هنا
و ثالث شى مستخدام فى المكتبة هى Try
و كل المطلوب منها كشف الاخطاء
الشرح عن طريق الكود
نقوم بعمل دلة
كود :
Public Sub addtodata(ByVal Control As System.Windows.Forms.Form, ByVal strcon As String, ByVal Table As String)
و strcon لجلب اسم نص الاصتلا بقاعدة البيانات
و Table لجلب اسم الجدوال المراد التعامل
اكسس
كود :
Dim con As New System.Data.OleDb.OleDbConnection(strcon)
كود :
Dim con As New System.Data.SqlClient.SqlConnection(strcon)
كود :
If con.State = ConnectionState.Open Then con.Close()
اكسس
كود :
Try
con.Open()
Dim da As New System.Data.OleDb.OleDbDataAdapter("select * from " + Table, con)
Dim ds As New DataSet
Dim cm As New System.Data.OleDb.OleDbCommandBuilder(da)
da.Fill(ds, Table).ToString()
Dim dr As DataRow = ds.Tables(Table).Rows(0)
For i As Integer = 0 To Control.Controls.Count - 1
كود :
Try
con.Open()
Dim da As New System.Data.SqlClient.SqlDataAdapter("select * from " + Table, con)
Dim ds As New DataSet
Dim cm As New System.Data.SqlClient.SqlCommandBuilder(da)
da.Fill(ds, Table).ToString()
Dim dr As DataRow = ds.Tables(Table).Rows(0)
For i As Integer = 0 To Control.Controls.Count - 1
و بعد ذلك تم فتح الاتصال بقاعدة البيانات con.Open
و بعد كده تم عمل استعلام عن الجدوال و بالطبع هنا قومت بجلب اسم المتغير لى حامل اسم الجدال من اعلى و بقاى الكود معروف للجميع و بعد ذلك استخدام For لجلب الكنترول
كود :
If TypeOf (Control.Controls(i)) Is System.Windows.Forms.TextBox Then
Dim s As String = Control.Controls(i).Name
Dim w As Object = (Control.Controls(i).Text)
dr.Item(s) = w
End If
Catch ex As Exception
End Try
Next
و بعد ذلك قوم بعمل المتغير s و هو يساوى اسم المتغير Control لجلب اسم الكنترول
و بعد كده قومت بعمل متغير تانى علشان جيب البيانات من المتغير نفسو و هو المتغير w
و بعد ذلك قوم بعمل اضافة فى قاعدة البيانات عن طريق اسم الكنترول لى هو يساوى اسم الحقل فى قاعدة البيانات و هو المتغير s و بعد ذلك اضافة البيانات فى الحقل عن طريق المتغير w
و بعد ذلك انهاء القاعدة الشرطية End If
و السؤال هنا ليه حط الحلقة التكرار For بعد Try
هقولك ليه بس اوعى تقول حد
دلوقتى فى حالة ان فى كنترول من نوع TextBox اسمو مش موجود فى الجدوال هنا هيحصل مشكلة و سوف يتم تحويلك الى Catch ex As Exception الطبيعى هنا ان مش احط كود تانى لحل المشكلة و لكن اسيبو فارغ علشان يذهب الى Next لى بدوره هترجعو تانى علشان يجيب باقى الكنتروال TextBox بس كده سهله
كود :
ds.Tables(Table).Rows.Add(dr)
da.Update(ds, Table)
con.Close()
MsgBox("Added successfully", MsgBoxStyle.Information, "Email:mrsv_42@hotmail.com")
Catch ex As Exception
MsgBox("Please make sure the text of the contact and the name of the database table and the names of the tools correctly", MsgBoxStyle.Exclamation, "Email:mrsv_42@hotmail.com")
End Try
و اظهر الرسائل تفيد ان تم العملية بنجاح و فى حالة و جود خطاء فى كتب اسماء اسر الكنتروال TextBox بنفس اسم الحقوال قى الجدوال يتم اظهر هذى الرسائل
و اليكم الكود كمل يعم مع قاعدة بيانات اكسس و اترك لكم تغيرمن اكسس الى سيكوال او اروكل
كود :
Public Sub addtodata(ByVal Control As System.Windows.Forms.Form, ByVal strcon As String, ByVal Table As String)
Dim con As New System.Data.OleDb.OleDbConnection(strcon)
If con.State = ConnectionState.Open Then con.Close()
Try
con.Open()
Dim da As New System.Data.OleDb.OleDbDataAdapter("select * from " + Table, con)
Dim ds As New DataSet
Dim cm As New System.Data.OleDb.OleDbCommandBuilder(da)
da.Fill(ds, Table).ToString()
Dim dr As DataRow = ds.Tables(Table).Rows(0)
For i As Integer = 0 To Control.Controls.Count - 1
Try
If TypeOf (Control.Controls(i)) Is System.Windows.Forms.TextBox Or TypeOf (Control.Controls(i)) Is System.Windows.Forms.DateTimePicker Or TypeOf (Control.Controls(i)) Is System.Windows.Forms.RichTextBox Or TypeOf (Control.Controls(i)) Is System.Windows.Forms.MaskedTextBox Or TypeOf (Control.Controls(i)) Is System.Windows.Forms.NumericUpDown Or TypeOf (Control.Controls(i)) Is System.Windows.Forms.Label Or TypeOf (Control.Controls(i)) Is System.Windows.Forms.LinkLabel Or TypeOf (Control.Controls(i)) Is System.Windows.Forms.DomainUpDown Then
Dim s As String = Control.Controls(i).Name
Dim w As Object = (Control.Controls(i).Text)
dr.Item(s) = w
End If
Catch ex As Exception
End Try
Next
For Each chk In Control.Controls.OfType(Of System.Windows.Forms.CheckBox)()
Try
Dim ssq As String = chk.Name
Dim wwq As Object = chk.Checked
dr.Item(ssq) = wwq
Catch ex As Exception
End Try
Next
For Each chyk In Control.Controls.OfType(Of System.Windows.Forms.RadioButton)()
Try
Dim ssp As String = chyk.Name
Dim wwp As Object = chyk.Checked
dr.Item(ssp) = wwp
Catch ex As Exception
End Try
Next
For Each chkr In Control.Controls.OfType(Of System.Windows.Forms.PictureBox)()
Try
If chkr.Image Is Nothing Then
Else
Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo("~\alysbisyimg")
If dir.Exists = True Then
System.IO.File.Copy(chkr.ImageLocation, "~\alysbisyimg\" + IO.Path.GetFileName(chkr.ImageLocation))
Else
System.IO.Directory.CreateDirectory("~\alysbisyimg")
System.IO.File.Copy(chkr.ImageLocation, "~\alysbisyimg\" + IO.Path.GetFileName(chkr.ImageLocation))
End If
Dim ssb As String = chkr.Name
Dim wwb As Object = "~/alysbisyimg/" + (IO.Path.GetFileName(chkr.ImageLocation))
dr.Item(ssb) = wwb
End If
Catch ex As Exception
End Try
Next
For Each chhk In Control.Controls.OfType(Of System.Windows.Forms.ComboBox)()
Try
If chhk.SelectedItem Is Nothing Then
Else
Dim sss As String = chhk.Name
Dim www As String = chhk.SelectedItem.ToString
dr.Item(sss) = www
End If
Catch ex As Exception
End Try
Next
For Each chhkk In Control.Controls.OfType(Of System.Windows.Forms.ListBox)()
Try
If chhkk.SelectedItem Is Nothing Then
Else
Dim sssg As String = chhkk.Name
Dim wwwg As String = chhkk.SelectedItem.ToString
dr.Item(sssg) = wwwg
End If
Catch ex As Exception
End Try
Next
For Each ctrl In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl2 In ctrl.Controls.OfType(Of System.Windows.Forms.TextBox)()
Try
Dim sqwe As String = ctrl2.Name
Dim wqwe As Object = ctrl2.Text
dr.Item(sqwe) = wqwe
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
For Each ctr3 In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl4 In ctr3.Controls.OfType(Of System.Windows.Forms.DateTimePicker)()
Try
Dim wsqwe As String = ctrl4.Name
Dim wwqwe As Object = ctrl4.Text
dr.Item(wsqwe) = wwqwe
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
For Each ctr5 In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl6 In ctr5.Controls.OfType(Of System.Windows.Forms.RichTextBox)()
Try
Dim sqwte As String = ctrl6.Name
Dim wqwte As Object = ctrl6.Text
dr.Item(sqwte) = wqwte
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
For Each ctr7 In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl8 In ctr7.Controls.OfType(Of System.Windows.Forms.MaskedTextBox)()
Try
Dim sqweyy As String = ctrl8.Name
Dim wqweyy As Object = ctrl8.Text
dr.Item(sqweyy) = wqweyy
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
For Each ctr9 In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl10 In ctr9.Controls.OfType(Of System.Windows.Forms.NumericUpDown)()
Try
Dim sqweoo As String = ctrl10.Name
Dim wqweoo As Object = ctrl10.Text
dr.Item(sqweoo) = wqweoo
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
For Each ctrl11 In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl12 In ctrl11.Controls.OfType(Of System.Windows.Forms.Label)()
Try
Dim sqwell As String = ctrl12.Name
Dim wqwell As Object = ctrl12.Text
dr.Item(sqwell) = wqwell
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
For Each ctrl3 In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl113 In ctrl3.Controls.OfType(Of System.Windows.Forms.LinkLabel)()
Try
Dim sqwegg As String = ctrl113.Name
Dim wqwegg As Object = ctrl113.Text
dr.Item(sqwegg) = wqwegg
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
For Each ctrl1114 In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl221 In ctrl1114.Controls.OfType(Of System.Windows.Forms.DomainUpDown)()
Try
Dim sqwerrr As String = ctrl221.Name
Dim wqwerrr As Object = ctrl221.Text
dr.Item(sqwerrr) = wqwerrr
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
For Each ctr1000 In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl2000 In ctr1000.Controls.OfType(Of System.Windows.Forms.CheckBox)()
Try
Dim ssq As String = ctrl2000.Name
Dim wwq As Object = ctrl2000.Checked
dr.Item(ssq) = wwq
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
For Each ctrl In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl552 In ctrl.Controls.OfType(Of System.Windows.Forms.RadioButton)()
Try
Dim ssq As String = ctrl552.Name
Dim wwq As Object = ctrl552.Checked
dr.Item(ssq) = wwq
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
For Each ctrl7686 In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl45657652 In ctrl7686.Controls.OfType(Of System.Windows.Forms.PictureBox)()
Try
If ctrl45657652.Image Is Nothing Then
Else
Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo("~\alysbisyimg")
If dir.Exists = True Then
System.IO.File.Copy(ctrl45657652.ImageLocation, "~\alysbisyimg\" + IO.Path.GetFileName(ctrl45657652.ImageLocation))
Else
System.IO.Directory.CreateDirectory("~\alysbisyimg")
System.IO.File.Copy(ctrl45657652.ImageLocation, "~\alysbisyimg\" + IO.Path.GetFileName(ctrl45657652.ImageLocation))
End If
Dim ssb As String = ctrl45657652.Name
Dim wwb As Object = "~/alysbisyimg/" + (IO.Path.GetFileName(ctrl45657652.ImageLocation))
dr.Item(ssb) = wwb
End If
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
For Each ctr8797l In Control.Controls.OfType(Of System.Windows.Forms.GroupBox)()
Try
For Each ctrl4532 In ctr8797l.Controls.OfType(Of System.Windows.Forms.ListBox)()
Try
If ctrl4532.SelectedItem Is Nothing Then
Else
Dim sssg As String = ctrl4532.Name
Dim wwwg As String = ctrl4532.SelectedItem.ToString
dr.Item(sssg) = wwwg
End If
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
ds.Tables(Table).Rows.Add(dr)
da.Update(ds, Table)
con.Close()
MsgBox("Added successfully", MsgBoxStyle.Information, "Email:mrsv_42@hotmail.com")
Catch ex As Exception
MsgBox("Please make sure the text of the contact and the name of the database table and the names of the tools correctly", MsgBoxStyle.Exclamation, "Email:mrsv_42@hotmail.com")
End Try
End Sub
قال تعالى : يَامَعْشَرَ الْجِنِّ وَالإِنسِ إِنْ اسْتَطَعْتُمْ أَنْ تَنفُذُوا مِنْ أَقْطَارِ السَّمَاوَاتِ وَالأَرْضِ فَانفُذُوا لا تَنفُذُونَ إِلاّ بِسُلْطَانٍ