PDA

View Full Version : ارسال اطلاعات از بانک درون textBox



dalaho
چهارشنبه 05 اسفند 1383, 10:45 صبح
با سلام من می خواهم اطلاعات یک فیلد را از sql server درون textbox
بریزم به نحوی که از BindingContext
استفاده شود
لطفا کمک کنید :wink: :wink: :wink: :wink:

C# Programmer
جمعه 07 اسفند 1383, 12:32 عصر
فکر کنم این مقاله کمکت کنه
هم برای comboBox و هم برای textBox




Populate data from database in a ComboBox
Introduction
This article is the second way (version) of "How to populate data in a ComboBox" - program. For doing that, I make use of 'TableMapping' and 'DataViewManager' that I'll explain later in the section: "Code and How it works".
The application allows you to select a StudentID in a ComboBox and displays the columns StudentID, Student Subject and Student Name from the table in the 3 TextBoxes.
It shows also how to get connected to a MS-Access database which you can also find in the project included (sudentDB.mdb). I chose MS-Access database because not many people have SQL-Server running.
Form:

Code and How it works
Before starting, a short explanation of what TableMapping and DataViewManager means. TableMapping is the process that controls how data adapters copy tables and columns of data from a physical data source to ADO.NET in-memory objects. When a data adapter reads data from a data source, it determines where to put the data in the corresponding DataSet table (or tables), using a table mapping. If you create a mapping in a DataAdapter, it allows you to establish a correspondence between columns in the data source and columns in a DataSet table. A DataAdapter contains a collection of DataTableMapping objects in its TableMappings property. You can pass the DataTableMapping name in place of the DataTable name to the Fill method of the DataAdapter.
The following example creates a DataTableMapping named "MyStudentMappings" for the table "studentTable".
dAdapter.TableMappings.Add("MyStudentMappings", "studentTable");
When you call the Fill method of the DataAdapter and do not specify a TableName or DataTableMapping name, the DataAdapter looks for a DataTableMapping called "Table". If you leave out that DataTableMapping, TableName of the DataTable will be "Table". This is the default DataTableMapping.
For example:
dAdapter.TableMappings.Add("Table", "studentTable");
If the SELECT command creates a result set with a default name of Table, then its contents go into a new or existing DataTable object called "studentTable". The DataTableMapping object describes a mapped relationship between a SQL-result set and a DataTable object in a DataSet.
The DataViewManager represents a view onto an entire DataSet and represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing, and navigation, whereas the DataView class acts as a view onto a single DataTable. A DataViewManager is an object that contains a collection of DataViews. The DataViewManager returned by the DefaultViewManager property allows you to create custom settings for each DataTable in the DataSet.
When you want to bind a control to more than one table of a DataSet, binding to a DataViewManager is the ideal choice. When there is only a single DataTable then use DataView. When binding a DataSet, .NET automatically uses the corresponding DataViewManager provided through the DataSet.DefaultViewManager property:
For example:
this.dviewmanager=dset.DefaultViewManager;
Here is the code for the method fnGetConnectedToDatabase():
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();
}
As soon as you select a StudentID from the ComboBox, the columns StudentID, StudentSubject and StudentName in the table "studentTable" are displayed in the 3 TextBoxes.
Conclusion
There are of course many ways to get data into a ComboBox control. I just tried to show a different way.
- Good coding!

dalaho
شنبه 08 اسفند 1383, 08:42 صبح
:flower: [/img]</span>[/code]

dalaho
چهارشنبه 12 اسفند 1383, 10:29 صبح
دوست عزیز من جواب نگرفتم :( :( :( :(

C# Programmer
پنج شنبه 13 اسفند 1383, 19:57 عصر
سلام

لطفا دقیقا مشکلت رو مطرح کن و اگر کدی نوشتی که جواب نداده اون رو اینجا بذار :wink:

dalaho
شنبه 15 اسفند 1383, 08:26 صبح
من می خواهم با زدن یک دکمه اطلاعات موجود در یک فیلد بانک درون یک
textBox
ریخته شود
و کد خاصی که قابل ذکر باشد هم ننوشته ام
please help me :flower:
:wink: :wink: :wink:

C# Programmer
شنبه 15 اسفند 1383, 10:09 صبح
سلام

برای این کار چند راه داری
ساده ترینش اینه که با استفاده از connection و dataAdapter به بانک وصل بشی و dataset رو Fill کنی و بعد textbox رو Bind کنی به dataset .
اگر به اون سایتی که برات فرستادم بری نمونه برنامه برای این کار داره
www.codeproject.com



oleDbDataAdapter1.Fill&#40;dataSet11&#41;;
textBox1.DataBindings.Add&#40;"Text", dataSet11.Tables&#91;0&#93;, "Name"&#41;;

dalaho
شنبه 15 اسفند 1383, 10:47 صبح
ممنون جواب گرفتم
لطف کردید :flower: :flower: :flower: :flower: :flower: :flower: :flower: :flower:

AspNet
یک شنبه 16 اسفند 1383, 14:18 عصر
حالا اگه ما چند تا textBox داشته باشیم و بخوایم اطلاعات رو بریزیم چطور؟
اگه bind کنیم همشون فقط با رکورد اول پر میشن

C# Programmer
یک شنبه 16 اسفند 1383, 21:01 عصر
معمولا چنین اتفاقی نمی افته یعنی ممنطقی نیست
ولی اگه بخواین چنین کاری بکنین می تونین از چند dataSet مختلف استفاده کنین و اونارو Bind کنین

AspNet
یک شنبه 16 اسفند 1383, 21:26 عصر
این راه که خیلی طولانی میشه و زیاد هم منطقی نیست :wink:
ولی همچین چیزی هم اتفاق میفته . مثلا زمانی که 10 تا سوال در پایگاه داده داشته باشیم و بخوایم اونا رو تو textBox نشون بدیم.

dalaho
دوشنبه 17 اسفند 1383, 08:25 صبح
حالا اگه ما چند تا textBox داشته باشیم و بخوایم اطلاعات رو بریزیم چطور؟
اگه bind کنیم همشون فقط با رکورد اول پر میشن

نظر شما راجع به این چیه؟
textBob1.Text=syste,.conver.tostring(ds.tables[0].Row[0]["فیلد مورد نظر"])
همین کار را برای بقیه انجام بده اما عدد Row را افزایش بده

C# Programmer
دوشنبه 17 اسفند 1383, 12:20 عصر
در این صورت باید رکورد ها رو دونه دونه و دستی بخونی
همون طور که جناب Dalaho گفتن :wink: :موفق:

AspNet
دوشنبه 17 اسفند 1383, 12:57 عصر
آیا راهی وجود ندارد که بخوایم با یه حلقه داده ها رو با یه خط کد بنویسیم؟

خواستم ببینم در سی شارپ همچین چیزی هست؟