1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Accessing data with ADO.NET ppt

40 321 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 40
Dung lượng 828 KB

Nội dung

aspnet aspnet Accessing data with ADO.NET Accessing data with ADO.NET Hà Đồng Hưng Objectives • Introduction – ADO.NET and other technologies • Connected Objects – Objects List – Demonstrations – Using Disconnected Objects in VS.NET • Disconnected Objects – Objects List – Demonstrations – Using Disconnected objects in VS.NET Objectives • Introduction – ADO.NET and other technologies • Connected Objects – Objects List – Demonstrations – Using Disconnected Objects in VS.NET • Disconnected Objects – Objects List – Demonstrations – Using Disconnected objects in VS.NET What is ADO.NET ? • ADO.NET is a set of libraries included with the Microsoft .NET Framework that help you communicate with various data stores from .NET applications – connecting to a data source – submitting queries – processing results • Can also use ADO.NET as a hierarchical, disconnected data cache to work with data offline Why a new data access technology? • Visual Basic 3 - Data Access Objects (DAO) – designed to communicate with local file-based databases • Visual Basic 4 - Remote Data Objects (RDO) – a fast, lightweight data access layer designed to talk to larger server-based databases (SQL Server, Oracle…) • Visual Basic 5 and Visual Studio 97 - ODBCDirect – combine the power of RDO with the ease of use that DAO provides • Visual Basic 6 and Visual Studio 6 - ADO – a data access model we could use easily in server-side scripts (fewer lines of code, allow us to pass data structures from server to client and back) Drawbacks of older technologies • Problems: – Developers need to build more powerful applications • work with XML data • can pass ADO Recordset objects between different tiers in your application, but cannot combine the contents of multiple Recordset objects • … – Recent versions of ADO added more XML features, but ADO will never handle XML data as efficiently as ADO.NET) ADO.NET model • ADO.NET is designed to help developers build efficient multi- tiered database applications across intranets and the Internet ADO.NET model • The disconnected half of the ADO.NET object model do not communicate directly with the connected objects • A major change from previous Microsoft data access object models. – In ADO, Recordset object stores the results of your queries. – Call Open method to fetch the results of a query – Call Update method to submit changes stored within the Recordset to your database. • ADO.NET DataSet is comparable in functionality to the ADO Recordset. – However, DataSet does not communicate with your database. – In order to fetch data from your database into a DataSet, you pass the DataSet into the Fill method of a connected ADO.NET object—the DataAdapter. .NET Data Providers • SQL Client .NET Data Provider – communicate with SQL Server databases, version 7+ • OLE DB .NET Data Provider – communicate with various data stores through OLE DB providers • Each .NET data provider implements the same base classes – Base classes: Connection, Command, DataReader, Parameter, Transaction – their actual names depend on the provider Connection Strings • SQL Server – Provider=SQLOLEDB;Data Source=MyServer\MyInstance;  Initial Catalog=MyDatabase;User ID=MyUID;Password=MyPass; – Provider=SQLOLEDB;Data Source=MyServer; Initial Catalog=MyDb; Integrated Security=SSPI; – Provider=SQLOLEDB;Data Source=MyServer; Initial Catalog=MyDatabase; Trusted_Connection=Yes; • Oracle – Provider=MSDAORA;Data Source=MyDatabaseAlias; User ID=MyUID;Password=MyPass; • Access – Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\A\MyDb.mdb; – Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data\MyDb.mdb; – Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\A\MyDb.mdb; Jet OLEDB:Database Password=MyPass; [...]... • DataSet Object – contains a set of data – container for a number of DataTable objects – data stored in a DataSet object is disconnected from your database Any changes you make to the data are simply cached in each DataRow – allow you to write to and read from a XML file • DataTable Object – Is used to examine data through collections of rows and columns – can store the results of a query in a DataTable... the data – Use For…Next, While…End While Dim da As New OleDbDataAdapter(strSql, cn) Dim ds As New DataSet da.Fill(ds, “ABC") Dim tbl As DataTable = ds.Tables(“ABC”) Dim row As DataRow For Each row In tbl.Rows i=row(“MAKH”) s=row(“TenKH”) Next row Demonstrations Disconnected Objects • Create DataTable in DataSet Dim ds As New DataSet() Dim tbl As DataTable = ds.Tables.Add("Customers") Dim ds As New DataSet... Objects • Fetch the results of a query into a DataSet strSql = "SELECT MaKH, TENKH FROM KHACHHANG" Dim da As New OleDbDataAdapter(strSql, cn) Dim ds As New DataSet da.Fill(ds, “MAVATENCUAKH") • Get the data returned by DataAdapter Dim da As New OleDbDataAdapter(strSQL, cn) Dim ds As New DataSet() da.Fill(ds, “ABC") Dim tbl As DataTable = ds.Tables(“ABC”) Dim row As DataRow = tbl.Rows(0) Response.Write(“Mã... New DataTable("Customers") ds.Tables.Add(tbl) Demonstrations Disconnected Objects • Create a column in DataTable Dim ds As New DataSet() Dim tbl As DataTable = ds.Tables.Add("Customers") Dim col As DataColumn = tbl.Columns.Add("CustomerID")  Dim ds As New DataSet() Dim tbl As DataTable = ds.Tables.Add("Orders") Dim col As DataColumn = tbl.Columns.Add("OrderID", _ GetType(Integer))  • Use methods DataTable.PrimaryKey,... stores information about the structure of the column • Constraint Object – place constraints on the data stored locally within a DataTable object • DataRelation Object – Is used to indicate a relationship between different DataTable objects in your DataSet • DataView Object Objectives • Introduction – ADO.NET and other technologies • Connected Objects – Objects List – Demonstrations – Using Disconnected... contents of a specific table • DataReader Object (data read-only) – Is designed to help you retrieve and examine the rows returned by your query quickly • Transaction Object • Parameter Object Connected Objects • Data Adapter – act as a bridge between your database and the disconnected objects in the ADO.NET object model – Fill method fetch the results of a query into a DataSet or a DataTable – Update method... the results of a query in a DataTable through the DataAdapter object’s Fill method • DataRow Object – Use the object’s Rows collection – To examine the data stored in a specific column of a particular row, you use the Item property Disconnected Objects • DataColumn Object – corresponds to a column in your table – doesn’t contain the data stored in your DataTable Instead, it stores information about the... cmd.Parameters.Add(“@Param1”,”1”) cmd.Parameters.Add(“@Param2”,”2”) Dim rdr As OleDbDataReader = cmd.ExecuteReader() Tips Connected Objects • In the ADO.NET object model, you should close your DataReader objects as quickly as possible • Connection object having an open DataReader is considered blocked – If open a second DataReader before closing the first one, you’ll receive an exception “requires an... DataTable.PrimaryKey, DataTable.Constraint – To set PrimaryKey (consisting many properties), constraints… Demonstrations Disconnected Objects • Add new data Dim row As DataRow = ds.Tables("Customers").NewRow row("CustomerID") = "ALFKI" ds.Tables("Customers").Rows.Add(row) Dim aValues As Object() = {“123", "A", “B", "007"} ds.Tables("Customers").LoadDataRow(aValues, False) Demonstrations Disconnected Objects • Modify data. .. queries that select data Dim strSql As String strSql = "Select * from KHACHHANG" Dim cmd As New OleDb.OleDbCommand(strSql, cn) Dim rdr As OleDb.OleDbDataReader = cmd.ExecuteReader() While rdr.Read() i = rdr(“MaKH") s = rdr(“TenKH") End While rdr.Close() Demonstrations Connected Objects • Execute queries: Insert, Update, Delete (DML -Data Manipulation Language), Create, Alter, Drop… (DDL -Data Definition . aspnet aspnet Accessing data with ADO. NET Accessing data with ADO. NET Hà Đồng Hưng Objectives • Introduction – ADO. NET and other technologies. Recent versions of ADO added more XML features, but ADO will never handle XML data as efficiently as ADO. NET) ADO. NET model • ADO. NET is designed to help

Ngày đăng: 11/12/2013, 17:15

TỪ KHÓA LIÊN QUAN