03 3-create-db-application-ado-dot-net Create Database Application with using ADO.NET
Introduction to ADO.Net and Visual Studio Database Tools
-
Upload
nora-franco -
Category
Documents
-
view
108 -
download
1
description
Transcript of Introduction to ADO.Net and Visual Studio Database Tools
![Page 1: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/1.jpg)
Introduction to ADO.Net and Visual Studio Database Tools
ISYS 512
![Page 2: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/2.jpg)
Database Processing
• Querying database
• Updating database:– Insertion, deletion, modification
![Page 3: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/3.jpg)
Steps to Retrieve Data
• Establishes a connection to the database.
• Executes query commands against the database.– SQL Select commands
• Process the returned data.
![Page 4: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/4.jpg)
Steps to Update Data
• Establishes a connection to the database.
• Executes update commands against the database.– SQL Insert, Delete, Update commands
• Receive confirmation of completion.
![Page 5: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/5.jpg)
ADO.Net: Connected and Disconnected Data Processing
• The ADO.Net environment can be categorized into connected and disconnected environments. – A connected environment requires a constant
connection to transfer data between the client application and the data source.
– A disconnected environment retrieves data and performs modification without a constant connection to the data source.
![Page 6: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/6.jpg)
ADO.Net Classes
![Page 7: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/7.jpg)
ADO.Net Classes and Data Consumer
Ado.Net
Connection
Adapter
Command
DataReader
Dataset Data Consumer
WinForm
WebForm
![Page 8: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/8.jpg)
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.
![Page 9: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/9.jpg)
Data Providers
• ODBC Provider– Open Database Connectivity
• A driver manager• Used for relational databases
• OLE DB Provider– OLE DB interfaces provide applications with uniform access to
data stored in diverse information sources, or data stores. – MS Access (accdb)
• SQL Server Provider• Oracle Provider
• Etc.
![Page 10: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/10.jpg)
Using ODBC
• Windows 7: • Control Panel /Administrative Tools/DataSource(ODBC)
• Three types of data source names, DSN– User DSN: usable only by you and only on the machine
currently using.– System DSN: Any one using the machine can use.– File DSN: Can be copied and used by other computers
with the same driver installed.
• Note: For 32 bit software, you have to open the ODBC panel from C:\Windows\SysWOW64\odbcad32.exe
![Page 11: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/11.jpg)
Define an ODBC DSN for Access Database
• Download: SalesDB2011.accdb
• Open ODBC Window
• Define a System DSN to SalesDB2011 and name it: MySalesData
![Page 12: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/12.jpg)
SQL Server Express/SQL Server Express LocalDB
• SQL Server Express is available free from Microsoft, but it requires more computer resources.
• SQL Server Express LocalDB is a light-weight version of SQL Server Express that is already installed with VS 2012, and it does not bring heavy workload on development machines
![Page 13: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/13.jpg)
VS Database Tools
• View/Server Explorer:– For connection to ODBC data sources, OleDB to
MS Access, and SQL Server Express
• View/SQL Server Object Explorer– For connection to SQL Server LocalDB
![Page 14: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/14.jpg)
Connect to Access Database using OLE DB
• From Server Explorer, right-click Add Connection:– Microsoft Access Database File (OLE DB)
– Browse for database
– Test Connection
• Tools/Connect to database
![Page 15: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/15.jpg)
Connect to Access Database using ODBC• From Server Explorer:
– right-click Add Connection– Click Change– Microsoft Access ODBC Data source– Select DSN from the dropdown list– Test Connection
![Page 16: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/16.jpg)
To Start SQL Server Express LocalDB• View/SQL Server Object Explorer• Point to SQL Server and Right-click:
– Add SQL Server
• Connect to Server– Serve name: (localdb)\v11.0
– Connect
![Page 17: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/17.jpg)
Lab: Creating a LocalDB Database
• Database Name: MyDept
• Tables:– Department:
• DID, Dname, Office
– Employee• EID, Ename, Sex, Salary, HireDate, DID
![Page 18: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/18.jpg)
To Add a New Database
• Open the LocalDB node
• Right-click Databases– Add new database
• Enter database name– Example: MyDept
![Page 19: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/19.jpg)
To Add a New Table: Department• Open the Database node: Open the MyDept node
• Right-click Tables and select: Add New Table
• Change the table name to Department– CREATE TABLE [dbo].[Department]
• Click Update; then click Update Database
![Page 20: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/20.jpg)
To Add Records
• Open the Tables node
• Right-click the table name (Department) and select View Data
![Page 21: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/21.jpg)
To Modify Table Design
• Point and right-click the table name:– Select: View/Designer
• Change the CREATE TABLE command
• Or Change the design with the designer
![Page 22: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/22.jpg)
Add Employee Table
• Right-click Tables and select Add Table:– EID, Ename, Sex, Salary, HireDate, DID
![Page 23: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/23.jpg)
Working with SQL Server Express
• Starting SQL Server:– Control Panel/Administrative Tools
/Services/SQLServer(SQLExpress)
• SQL Server database file extension: mdf
• Default database folder: – A SQL Server file: testSQLServer.mdf – C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\
DATA\testSQLServer.mdf
![Page 24: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/24.jpg)
Creating SQL Server Database
• From Server Explorer, right click data connection and choose:
• Create new SQL Server Database
• Server name: – LocalServerName\SQLExpress– Ex: David-PC\SQLExpress
• Add new table: Right click Tables and choose Add New Table– Define table’s fields
• Add rows: Right click the table name and choose Show table data.
![Page 25: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/25.jpg)
Create a New SQL Server Database: EmployeeDB
• Add a new table: EmployeeTable– EID Char 10– Ename Char 30– Sex Char 1– Salary Numeric (10,2)– HireDate Date
• Enter data
![Page 26: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/26.jpg)
Three Ways to Create ADO.Net Objects
• 1. Automatically generated when creating data bound form.– Form wizard
• 2. Using Data Adapter Wizard• 3. Using code:
– Example:– string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\
SalesDB2007.accdb";– OleDbConnection objConn = new OleDbConnection(strConn);
![Page 27: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/27.jpg)
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 control’s Data Source and Data Member to bind the data.
![Page 28: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/28.jpg)
Automatically Generating Data Bound Form
• Creating a form with ADO.Net objects and data-bound controls to display and update information in a dataset.
• Step 1: Add a new data source:
• Step 2: Select controls for table fields: – Click the dropdown list next to the table’s name:
• Datagrid view
• Details
• Step 3: Drag the table to form.
![Page 29: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/29.jpg)
Add Data Source
• Data Source window:– View/Other windows/Data Source
• Add New Data Source– From Dataset
– Select connection
– Select tables
![Page 30: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/30.jpg)
Data Bound Form Examples
• Using the LocalDB database MyDept, show records of the Employee table:– with dataGridView– with detail view
![Page 31: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/31.jpg)
Items Added to the Form
• Table Adapter: click smart tag– Add query– Preview data
• Dataset:– Edit in dataset designer
• Binding Source: It is an object that keeps track of position (the current row) of a data source. – Preview data
• Code view: – Form load event:
• Adapter’s Fill command
![Page 32: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/32.jpg)
Other Data Form Demos
• DataGrid View
• Add /Modify/Delete records.
• Read only form: – Delete AddNew, Delete, Save buttons from
navigator bar.
![Page 33: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/33.jpg)
Hierarchical Forms:Department/Employees
• Parent table/Child table– Add parent table and child table to Data Source– Drag the parent table and the child table to the form.
Parent table uses detail view and child table uses dataGrid view
– Click Dataset object’s smart tag to choose Edit in Dataset Designer
– With the designer, right click the parent table and choose Add/Relation
– Change dataGrid’s DataSource property to the relation.
![Page 34: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/34.jpg)
Detail Form with Bound ListBox
• Example: Employee table form with EID listbox and displays selected employee information in textboxes.– Choose detail view for the Employee table.– Click the dropdown list next to the EID field and click
ListBox– Drag the Employee table to the form.– Bind the EID field to the BindingSource:
• Activate the Property window and click the listbox• Set the DataSOurce property to BindingSource• Set the Display Member property to EID
![Page 35: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/35.jpg)
Creating A Database Application Without Programming
• Creating a database application to display information and update database.
• A main form with buttons to open data forms:– DisplayInfo– Enter New– Modify– Exit
![Page 36: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/36.jpg)
Demo: Add Data Source from OleDB and ODBC Data source
![Page 37: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/37.jpg)
Data Adapter Wizard – 2nd Level of Using ADO.Net
• Configure Data Adapter and generating a dataset:– From the Data tab of the ToolBox, Drag
OledbDataAdapter to the form.– Use the Data Adapter Wizard to configure the Adapter.– Right Click the Adapter to preview data and create
dataset.
• Note: If OledbDataAdapter is not listed in Toolbox’s Data tab , right click ToolBox Data Tab and select Choose Item; then select OleDbDataAdapter from .Net Framework components.
![Page 38: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/38.jpg)
Creating a Form with Bound DataGridView
• Configure Adapter and generate dataset.• Bind DataGridView control to the table:
– Data Source property:• DataSet
– Data Member property• A table in the dataset
• In the Form Load event, use Adapter’s Fill method to load the dataset:
• OleDbDataAdapter1.Fill(DataSet11);
• Note 1: No navigation capability• Note 2: View adapter’s properties and see
the SQL commands.
![Page 39: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/39.jpg)
Creating a Form with Bound Textboxes
• Configure Adapter and generate dataset.• Select textbox’s Data Bindings property:
– Text: choose field from bindingSOurce object
• In the Form Load event, use Adapter’s Fill method to load the dataset:
• OleDbDataAdapter1.Fill(DataSet11)
• Note: No navigation capability
![Page 40: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/40.jpg)
Using BindingSource Object to Do the Binding and Navigation
• It is an object that keeps track of position (the current row) of a data source.
• Useful properties:– DataSource– DataMember– Position property: is the index of the current row. The index is a
0-based index, the first record has a position of 0.• Methods:
– MoveFirst, MoveLast, MoveNext, MovePrevious– AddNew– AllowEdit– EndEdit– Current– RemoveCurrent
![Page 41: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/41.jpg)
Bind Controls to BindingSource Object
• Configure Adapter and generate dataset.• Add BindingSource object to the form• Use BindingSource object’s property window to
set its DataSource and Data Member properties to the dataset object and table object.
• Set control’s DataBinding property to the BindingSource object.
![Page 42: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/42.jpg)
Use BindingSource Object’s MoveNext, MovePrevious Methods• Add a MoveNext button
– bindingSource1.MoveNext();
• Add a MovePrevious button:– bindingSource1.MovePrevious();
![Page 43: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/43.jpg)
Adding AddNew and Save Button
AddNew button: Use BindingSource AddNew Method:
private void button3_Click(object sender, EventArgs e) { bindingSource1.AddNew(); }
Save button: Use BindingSource EndEdit method and Adapter’s Update method:
private void button4_Click(object sender, EventArgs e) { bindingSource1.EndEdit(); oleDbDataAdapter1.Update(dataSet2.CUSTOMER); }
![Page 44: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/44.jpg)
Binding ListBox
• Example: Bind Customer Table’s CID field to a listbox.– Create a Adapter for Customer table , and
generate the dataset.– Add ListBox and set binding properties:
• Data Source: Customer table
• Display Member: Field to display in the listbox.
• Value Member: the actual values for items in the list box.
![Page 45: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/45.jpg)
Display Selected Record
• Bound textbox (same data source as the listbox):– If the Listbox and the textbox are bound to the same
BindingSource object, the textbox will automatically displays the record of the selected listbox item.
• Unbound textbox– To display the ValueMember
• textBox3.Text = listBox1.SelectedValue.ToString();
– To display other fields:• VB with late binding
– Textbox1.text = ListBox1.SelectedItem(“Cname”)
• C# does not support late binding:No!
![Page 46: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/46.jpg)
ListBox SelectedItem Property:(Works for VB)
• How to display the selected record in unbound textbox?
• After binding to a data source, this property return a DataRowView object.
• What is DataRowView? – Object Browser:
• System.Data– DataRowView: Item property is the default property
• 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”)
![Page 47: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/47.jpg)
Using Object Browser
• View/Object Browser• DataSet object model:• System.Data
– DataSet• Relations• Tables
– Rows– Columns
• Use Object Browser to study object’s properties, methods. Ex. System.Data.OleDbDataAdapter
![Page 48: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/48.jpg)
Unbound Textbox:Navigate from Dataset to Table; from Table to Row and Field
textBox3.Text = dataSet41.Tables["employee"].Rows[listBox1.SelectedIndex][1].ToString();textBox4.Text = dataSet41.Tables["employee"].Rows[listBox1.SelectedIndex]["Salary"].ToString();
![Page 49: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/49.jpg)
Working with SQL Server Data Adapter
• From the Data tab:– Choose: SQLDataAdapter– (If you don’t see it, right-click the Data tab and
select Choose Item. Then select it from the .Net components)
![Page 50: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/50.jpg)
DataSet and Data Adapter
• 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.– It is an object with SQL Select, Insert, Update and
Delete commands.
![Page 51: Introduction to ADO.Net and Visual Studio Database Tools](https://reader035.fdocuments.net/reader035/viewer/2022081420/56812e61550346895d940830/html5/thumbnails/51.jpg)
Creating Parent/Child Form Using DataAdapter Wizard
• Add two DataAdapters to the form: one for the parent table and one for the child table.
• Use the parent table’s DataAdapter to generate a dataset; then use the child table’s DataAdapter to generate the child table into the same dataset.
• Open dataset’s designer and right-click the parent table and choose Add/Relation to add a relation.
• Add and bind textboxes to the parent table; add dataGridView and bind its datasource property to the relation.
• Add navigation button’s using parent table’s BindingSource object.