ado.net objects – data providers
DESCRIPTION
ADO.NET Objects – Data Providers. Dr. Ron Eaglin. Requirements. Visual Studio 2005 Microsoft SQL Server 2000 or 2005 Adventure Works Database Installed Database available on local machine. Agenda. Working with the Database Connection Object ConnectionString property - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/1.jpg)
ADO.NET Objects – Data Providers
Dr. Ron Eaglin
![Page 2: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/2.jpg)
Requirements
• Visual Studio 2005
• Microsoft SQL Server 2000 or 2005– Adventure Works Database Installed– Database available on local machine
![Page 3: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/3.jpg)
Agenda
• Working with the Database Connection Object– ConnectionString property– Visual Creation of Connection– Code Creation of Connection
– Runtime Connections
![Page 4: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/4.jpg)
Database ConnectionsCreate a new project
Windows application
Name project:AdventureWorksDemo1
![Page 5: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/5.jpg)
Using Toolbox
• Right click on the data icon in the toolbox
• Select “Choose items …”
• This will bring up a dialog allowing you to select all items displayed from the list of available tools.
![Page 6: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/6.jpg)
Data “items”Select SQLConnection fromList of available tools
The SQLConection objectWill now be in your Toolbox
![Page 7: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/7.jpg)
Adding SQLConnection to FormDrag and dropSQLConnectionObject ontoForm.
![Page 8: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/8.jpg)
SQL Connection ObjectSelect the SQLConnection object in the form and look at the Properties dialog box. Select the ConnectionString propertyAnd select <New Connection>
![Page 9: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/9.jpg)
Add Connection
Select your server
Select AdventureWorks database
Test the connection
![Page 10: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/10.jpg)
Add Connection
• ConnectionString property will fill with the text that allows the connection.
• ConnectionString has form
Keyword=value;keyword=value;keyword=value
Example:
Data Source=WHITEWATER;Initial Catalog=AdventureWorks;Integrated Security=True
![Page 11: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/11.jpg)
Adding Connection String with Code Add button to form and
Change text of button
Double click button for Code window
![Page 12: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/12.jpg)
Button Code windowEnter code here
![Page 13: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/13.jpg)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Create a new connection object of type SQLConnection Dim RuntimeConnection As New Data.SqlClient.SqlConnection ' Set the connection string RuntimeConnection.ConnectionString = "Data Source=WHITEWATER;Initial Catalog=AdventureWorks;Integrated Security=True"
Try RuntimeConnection.Open() MsgBox("Connection with connection string " + RuntimeConnection.ConnectionString + " opened successfully", MsgBoxStyle.Information) Catch ex As Exception MsgBox("Connection with connection string " + RuntimeConnection.ConnectionString + " failed to open", MsgBoxStyle.Information) End Try
RuntimeConnection.Close()
End Sub
![Page 14: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/14.jpg)
Test Application
![Page 15: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/15.jpg)
Add code to test SQLConnection1 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Try SqlConnection1.Open() MsgBox("Connection with connection string " + SqlConnection1.ConnectionString + " opened successfully", MsgBoxStyle.Information) Catch ex As Exception MsgBox("Connection with connection string " + SqlConnection1.ConnectionString + " failed to open", MsgBoxStyle.Information) End Try
SqlConnection1.Close() End Sub
![Page 16: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/16.jpg)
Test SQLConnection1
![Page 17: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/17.jpg)
SQLConnection Properties
• ConnectionString
• ConnectionTimeout
• Database
• DataSource
• ServerVersion
• State
![Page 18: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/18.jpg)
Other types of Connections
• SQLConnection is specific to SQl Server only
• ODBCConnection – any ODBC database
• OLEDBConnection – Any OLE DB datasource
• OracleConnection
![Page 19: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/19.jpg)
Agenda
• Creating and using a DataCommand object
• Issuing Database commands using the DataCommand object.
![Page 20: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/20.jpg)
DataCommand object
• DataCommand is a simple ADO.NET wrapper for a SQL Statement.
• Using DataCommand from design and from run-time.
![Page 21: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/21.jpg)
Design Window SQLCommand
• If SQLCommand is not in your toolbox, right click and add it to your toolbox using the Choose Items…
• Drag and drop the SQLCommand on to your form.
![Page 22: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/22.jpg)
SQLCommand addedSQLCommand object inform
![Page 23: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/23.jpg)
SQLCommand
• Properties of the SQLCommand– Connection– CommandType
• Text• StoredProcedure• TableDirect
– Parameters
![Page 24: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/24.jpg)
SQLCommand1 Properties
• Set Connection = SQLConnection1
• Set CommandType = Text
• Click on … dialog on CommandText Property
![Page 25: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/25.jpg)
Clicking on boxWill bring up queryBuilder.
![Page 26: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/26.jpg)
Once the query is built – it is possible to use the previewData link in the properties window.
![Page 27: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/27.jpg)
Runtime SQLCommand
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim rtSQLCommand As New Data.SqlClient.SqlCommand
rtSQLCommand.Connection = SqlConnection1rtSQLCommand.CommandType = CommandType.TextrtSQLCommand.CommandText = "SELECT Person.Address.* FROM Person.Address"
End Sub
![Page 28: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/28.jpg)
SQLCommand
• Note that running the code to create the runtime SQLCommand – does not actually do anything.
• We must execute the Command and also have a place for the results.
![Page 29: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/29.jpg)
DataReader Object
• A DataReader is a lightweight object meant to hold the results of a SQLCommand
• DataReaders can be treated as a cursor to go through the DB values.
![Page 30: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/30.jpg)
DataReader
SqlConnection1.Open() Dim rtDataReader1 As Data.SqlClient.SqlDataReader rtDataReader1 = rtSQLCommand.ExecuteReader
If rtDataReader1.HasRows = True Then MsgBox("The reader has found rows", MsgBoxStyle.Information) End IfSqlConnection1.Close()
Add this code to the previous code
![Page 31: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/31.jpg)
![Page 32: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/32.jpg)
Binding the Data ReaderAdd ListboxTo Form
![Page 33: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/33.jpg)
Add code
While rtDataReader1.Read ListBox1.Items.Add(rtDataReader1.GetValue(1)) End While
Add code before closing the connection, after executing the reader.
![Page 34: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/34.jpg)
Results of Code
Data from first field of DB Query is added to the list.
![Page 35: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/35.jpg)
DataReader Methods
• Open code window
• Put cursor on the code
Dim rtDataReader1 As Data.SqlClient.SqlDataReader
Hit F1
Put cursor here
![Page 36: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/36.jpg)
Help on DataReaderCode examples of using Data Reader
More code
![Page 37: ADO.NET Objects – Data Providers](https://reader031.vdocuments.net/reader031/viewer/2022013112/56812bc5550346895d9009d9/html5/thumbnails/37.jpg)
Review
• Working with Database Connection
• Creating code to use database
• Using Command Objects
• Using the Data Reader Object
• Displaying Database Results
• Getting More Information on Objects