Documente Academic
Documente Profesional
Documente Cultură
.Net Applications
ADO.Net
OLE DB Provider
OLE DB Data Source
Using ODBC
Windows 2000/2003:
Control Panel /Administrative Tools/DataSource(ODBC)
Server Explorer
Data connections:
Right click and Add Connection Tables, Views
ADO.NET Objects
.NET Applications
Data Set
Adapter Data Reader
Command Object
Connection Object
Database
ADO.NET Objects
Connection Object: Represent a connection to the database. Command Object: The command object allows us to execute a SQL statement or a stored procedure. DataReader: It is a read-only and forward-only pointer into a table to retrieve records. DataSet Object: A DataSet object can hold several tables and relationships between tables. DataAdapter: This the object used to pass data between the database and the dataset.
Using code:
Example:
dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) objConn.open()
Dataset:
View Schema: Dataset/XML
Data Binding
Connect a control or property to one or more data elements. Simple binding: Use simple binding to display a field value in controls that show Data Bindings in the property window, such as text box or label. Complex binding: Use complex binding to bind more than one field to controls such as DataGrid and list box. Use the controls Data Source and Data Member to bind the data.
Me.BindingContext(DataSet21, "customer").Position += 1
MoveLast:
Me.BindingContext(DataSet21, "customer").Position = Me.BindingContext(DataSet21, "customer").Count -1
How to MovePrevious and MoveFirst? Note: The Position property takes care of the end of file automatically.
CurrencyManager
Dim custCurrMgr As CurrencyManager Dim ordCurrMgr As CurrencyManager In a procedure:
ordCurrMgr = Me.BindingContext(Ds31, "orders") custCurrMgr = Me.BindingContext(Ds31, customer") custCurrMgr.Position += 1 ordCurrMgr.Position += 1
Binding DataGrid
From Server Explorer, drag the table from a database connection (or from Data tab, drag a oleDbAdapter) onto the form. Create dataset. Drag DataGrid and set the DataSource and Data Member property. Use adapters Fill method to load the dataset.
Binding ListBox
Example: Bind Customer Tables CID field to a listbox.
Create a Adapter to retrieve CID (and Cname) fields , and generate the dataset. Add ListBox and set binding properties:
Data Source Display Member Value Member: the actual values for items in the list box. To display the selected items value in a text box, do:
Textbox1.text = ListBox1.SelectedValue
Unbound textbox
To retrieve a column from a DataRowView object (use 0-based index to identity a column):
ListBox1.SelectedItem.Item(1) Or: ListBox1.SelectedItem(1) Or: ListBox1.SelectedItem(Cname)
Collection Structure
Properties:
Count Item(index), 0-based index
Methods:
Clear, Add, Insert, Remove, etc.
TextBox4.Text = drFound.Item("cname")
Or drFound.Item(1)
OleDBCommannd
Methods: ExecuteReader, ExecuteNonQuery Properties: Connection, CommandType, CommandText, Parameters
OleDBDataAdapter
Methods: Fill Properties: SelectCommand, InsertCommand, DeleteCommand, UpdateCommand.
Another Way to Bind Listbox and Display Selected Record Create an adapter to retrieve Customer records and create a dataset. Bind the CID field to the listbox. Use the Find method of Tables Rows collection to find the record. Display the found record in unbound text boxes.
Code Example
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim drFound As DataRow drFound = DataSet41.CUSTOMER.Rows.Find(ListBox1.SelectedValue)
Assume SelectedValue is CID
TextBox1.Text = drFound.Item("cname")
TextBox2.Text = drFound.Item("rating")
End Sub Note: We can get the search value from other controls such as InputBox and Textbox.
Generate the dataset with the parameter. Create and bind textboxes to the dataset. In the listboxs click event, assign the selected value to the parameter and fill the dataset:
DataSet11.Clear() OleDbDataAdapter2.SelectCommand.Parameters("cid").Value = ListBox1.SelectedValue OleDbDataAdapter2.Fill(DataSet11)
Add an Update button that use adapters update method to send changes back to the data source:
OledbDataAdapter1.Update(Dataset11)