مقدمة:
في هذا المقال سنعرض بإذن الله كيفية تعبئة الـ ComboBox من قاعدة البيانات وسنستخدم طريقة
TableMapping and 'DataViewManager' مع استخدام ADO.NET للربط مع قاعدة البيانات
سنعرض StudentID في ComboBox ونعرض جميع بيانات هذا الطالب الموجودة في هذا الحقل في 3 TextBoxes كما في الشكل التالي:

كيف يعمل الكود؟
في البداية يجب أن نوضح ما هي TableMapping و 'DataViewManager'
TableMapping عبارة عن طريقة لتسهيل تحكم الـ DataAdapter بالبيانات عن طريق نسخ البيانات ( الجداول والحقول) من ملف قاعدة البيانات و ADO OBJECT-MEMORY ( Dataset ) فعندما تقرأ الـ DataAdapter البيانات من ملف قاعدة البيانات فهي تحدد أي جدول (جداول) في الـdataset سيتم تعبئتها بهذه البيانات عن طريق TableMapping, فأنت بإنشاء TableMapping في الـ DataAdapter تستطيع أن تتحكم في العلاقة بين الحقول الموجودة في ملف البيانات والـ Dataset. DataAdapter تحتوي علي مجموعة من DataTableMapping في خاصية TableMappings , وتستطيع إرسال اسم الـ DataTableMapping بدل اسم الجدول في آمر Fill الموجود في DataAdapter.
المثال التالي يوضح كيف تنشئ DataTableMapping وسنسميه “MyStudentMappings” للجدول " studentTable”
dAdapter.TableMappings.Add("MyStudentMappings", "studentTable");والآن سنتعرف علي DataViewManager حيث أنها توضح كيفية عرض البيانات الموجودة في الـ Dataset وتوضح كيفية التحكم في البيانات من ناحية الترتيب , البحث والتعديل ... الخ , أما بالنسبة لـ DataView Class تعبر عن جدول واحد فقط في الـ Dataset و DataViewManager يتكون من عدة DataView
واليك الكود الذي يشرح كيفية تعبئة الـ ComboBox
private void fnGetConnectedToDatabase()
{
//Connection string
string conStr="Provider=Microsoft.Jet.OLEDB.4.0;" +
“ Data Source=..\\..\\studentDB.mdb ”;
try
{
//Instantiate OleDbConnection and open
//the connection to the database
myConn = new OleDbConnection(conStr);
myConn.Open
}
catch(OleDbException ex) {
//get the error message if connection failed
MessageBox.Show("Error in connection ..."+ex.Message );
}
string sqlStr ="SELECT * FROM studentTable;";
//Instantiate a DataAdapter by passing the sqlStr and myConn.
//now data is in raw form
dAdapter = new OleDbDataAdapter(sqlStr,myConn);
//Instantiate a DataSet
dset = new DataSet();
//Gets a collection that provides the master mapping
// between a source table and a DataTable
dAdapter.TableMappings.Add("Table", "studentTable “ );
//A data adapter object utilizes the Fill method
//to populate a DataSet or a DataTable object with
//data retrieved by a SELECT command.
dAdapter.Fill(dset);
//When binding a DataSet, .NET automatically uses the corresponding
//DataViewManager provided through the DataSet.DefaultViewManager property
this.dviewmanager=dset.DefaultViewManager;
this.comboBox1.DataSource=this.dviewmanager;
//display "studentTable.StudentID" in the ComboBox
this.comboBox1.DisplayMember="studentTable.StudentID “;
//DataBinding for the TextBox controls
this.textBox1.DataBindings.Add("Text “ ,
this.dviewmanager,"studentTable.StudentID” );
this.textBox2.DataBindings.Add("Text “ ,
this.dviewmanager, "studentTable.StudentSubject");
this.textBox3.DataBindings.Add("Text “ ,
this.dviewmanager, "studentTable.StudentName” );
// Close the connection to the database.
this.myConn.Close();
}منقول للأفادة من جروب الشبكة الاسلامية للكمبيوتر
