net data access
Post on 08-Feb-2016
56 Views
Preview:
DESCRIPTION
TRANSCRIPT
.NET Data Access
台南市教育局網路中心在職教師校務行政系統程式開發進階班行政網路組asp@mail.tn.edu.tw
Design Goals for ADO.NET
• Leverage Current ADO Knowledge • Support the N-Tier Programming Model • Integrate XML Support
Why ADO.NET?ADO vs. ADO.NET
• ADO– Connected access– Physical data model– RecordSet based data– RecordSet is ‘one’ table
• > 1 table requires a database JOIN
• Data is “flattened”: lose relationships
– COM/COM+ data types– Data via COM marshalling– Blocked by firewalls
(DCOM, binary)
• ADO.NET– Disconnected access– Logical data model– The DataSet based data– DataSet is multi-table
• > 1 table or source does not require a JOIN
• Relationships remain: navigation is relational
– XML schema Data types – No data type conversions required
– XML is plaintext: ‘Firewall friendly’
DataSetDataSet
ADO.NET Architecture()
System.DataSystem.Data
System.Data.OleDbSystem.Data.OleDb System.Data.SQLTypesSystem.Data.SQLTypesSystem.Data.SqlClientSystem.Data.SqlClient
.NET Data Providers.NET Data Providers
ADO.NET Architecture(2)• XML and ADO.NET• ADO.NET Components
.NET Data Provider
• Retrieve data from a data source and reconcile changes made to it with the data source
• The link between data source and application
• Two Providers out of the box:– .NET SQL Data Provider: SqlClient– .NET OLE DB Data Provider: OleDb
ADO.NET Data ProviderCore Classes
ConnectionConnection
CommandCommand
Data ReaderData Reader
Data AdapterData Adapter
System.Data.<Provider>System.Data.<Provider>Connect to data sourceConnect to data source
Execute Command Execute Command against data sourceagainst data source
Reads a forward-only read-Reads a forward-only read-only stream of data from a only stream of data from a
data sourcedata source
Populates a DataSet and Populates a DataSet and resolves updates with data resolves updates with data
sourcesource
Connection Class• Represent a unique session with a data source• Create, open, close a connection to a data source• Functionality and methods to perform transactions• Connection example:
String conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +String conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=NWIND_RW.MDB"; "Data Source=NWIND_RW.MDB";OleDbConnection aConn = new OleDbConnection(conStr);OleDbConnection aConn = new OleDbConnection(conStr);aConn.Open();aConn.Open();
// Execute Queries using OleDbCommand Class
aConn.Close();aConn.Close();
Connection
• OleDbConnection Provider=MSDAORA; Data Source=ORACLE8i7; User ID=OLEDB; Password=OLEDB
Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\bin\LocalAccess40.mdb;
Provider=SQLOLEDB;Data Source=MySQLServer;Integrated Security=SSPI; • SqlConnection user id=sa;password=aU98rrx2;initial catalog=northwind;data source=mySQLServer;Connect Timeout=30
Command Class• Represents a query to execute on the data
source• Methods of interest:
– ExecuteNonQuery: Executes a SQL statement that doesn’t return any rows
– ExecuteReader: Returns a DataReader– ExecuteScalar: Executes the query and returns
the first column of the first row
String SqlStr = “INSERT INTO Customers (CustId,FName,Lname) ;” +String SqlStr = “INSERT INTO Customers (CustId,FName,Lname) ;” +““Values (‘Matt’,’Stephen’)”;Values (‘Matt’,’Stephen’)”;
SqlCommand myCommand = new SqlCommand(SqlStrSqlStr , myConnection);myCommand.Connection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close();
DataAdapter Class • Bridge between the DataSet and the data store• Means to modify the DataSet and data source• Properties of Interest: DeleteCommand, InsertCommand,
SelectCommand, UpdateCommand• TableMappings: Maps source and DataTable• Inherits methods from DbDataAdapter class
– public virtual int Fill(DataSet,”Tablename”); – public virtual int update(DataSet,”Tablename”);
Data Store DataSetDataAdapter
DataReader• Forward-only data access• “Lightweight” programming model
– Less overhead than using Command• Instantiated by the Command class Execute method• Ties up the Command until it is finished reading• Methods to retrieve data:
– By index and optionally column type: GetString, GetInt32, and so on
– Read: Advances reader to next record
DataReader Sample
// Code for creating the Connection “Conn” not shownString myQuery = “SELECT * FROM Customers”;String myQuery = “SELECT * FROM Customers”;Conn.Open();Conn.Open();OleDbCommand myCmd = new OleDbCommand( myQuery, Conn );OleDbCommand myCmd = new OleDbCommand( myQuery, Conn );// Declare the DataReader...OleDbDataReader myDataReader;OleDbDataReader myDataReader;
// Instantiate the DataReader with ExecuteReader(...) ...myDataReadermyDataReader = = myCmd.ExecuteReader();myCmd.ExecuteReader();
// Always call Read before accessing data. while(myDataReader.Read()) while(myDataReader.Read()) {{
Console.WriteLine(myDataReader.GetString(0));Console.WriteLine(myDataReader.GetString(0));}}// Always Close the reader and the connection when donemyDataReader.Close();myDataReader.Close();Conn.Close();Conn.Close();
System.Data Principal Classes
DataSetDataSet
DataTableDataTable
DataRowDataRow
DataColumnDataColumn
DataRelationDataRelation
DataViewDataView
Contains the ‘main’ classes of Contains the ‘main’ classes of ADO.NETADO.NET
In-memory cache of databaseIn-memory cache of database
In-memory cache of database tableIn-memory cache of database table
Used to manipulate a row in a Used to manipulate a row in a DataTableDataTable
Used to define the columns in a Used to define the columns in a DataTableDataTable
Used to relate two Used to relate two DataTablesDataTables to each to each otherother
Used to create a view on a Used to create a view on a DataSetDataSet
System.DataSystem.Data
DataSet Tables
DataTable
System.Data: DataSet Overview
Relations
DataRelation
DataRelation
DataRow(s)
DataColumn
Constraint(s)
DataTable
DataTable
DataView
DataView
System.Data: DataSet• An in-memory cache of data from a data source via
DataAdapter• XML used to read and write data and schema• Common way to represent and manipulate data
– Universal data container– Not just for use with databases
• Logical or physical representation of data, depending on: – The query/result set– Whether DataTables and Relations exist
• Designed to be disconnected from the data source– Connect, execute query, disconnect
System.Data: DataTable• May be mapped to a physical table in the data
source• Can be related to one another through
DataRelations• Optimistic concurrency / locking• Properties of Interest:
– Columns: Returns DataColumnCollection of DataColumns
– Rows: Returns DataRow objects as a DataRowCollection
– Constraints: Returns the table’s ConstraintCollection
System.Data: DataSet & DataTable• Create a DataTable and add it to a DataSet
DataSet ds = new DataSet();DataSet ds = new DataSet();
// Create DataTable object: “Customers”.DataTable dt = new DataTable( “Customers” );DataTable dt = new DataTable( “Customers” );
// Create and add columns to the table// 1. Explicitly create and Add a DataColumnDataColumn dc = new DataColumn( “CustID”, typeof(Int16) );DataColumn dc = new DataColumn( “CustID”, typeof(Int16) );dt.Columns.Add( dc );dt.Columns.Add( dc );
// 2. Implicitly Create and Add columns (DataColumn).dt.Columns.Add( “First_Name”, typeof(String) );dt.Columns.Add( “First_Name”, typeof(String) );dt.Columns.Add( “Last_Name”, typeof(String) );dt.Columns.Add( “Last_Name”, typeof(String) );
// Add the DataTable object to the DataSetds.Tables.Add( dt );ds.Tables.Add( dt );
如何存取資料庫• ADO .NET 資料存取模型
– 資料庫連線 (Connection)• SqlConnection 與 OleDbConnection
– 執行 SQL 語法 (Commands)• SqlCommand 與 OleDbCommand
– ExecuteNonQuery ( 不傳回資料列 )– ExecuteScalar ( 傳回單一列資料 )– ExecuteReader ( 傳回多列資料 )
– 取得資料• DataReader• DataAdapter 與 DataSet
• 使用資料庫連線的步驟– OleDbConnection conn = new OleDbConnection() ;
– conn.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0; User ID=Admin; Password=123;
Data Source=C:\Northwind.mdb" ;
– conn.Open() ;
– conn.Close() ;
如何使用資料庫連線– 建立資料庫連線物件– 設定連線字串
– 開啟資料庫連線– :
– 關閉資料庫連線
• SqlConnection– conn.ConnectionString =
"User ID=sa; Password=; Server=localhost;Database=Northwind"
;
• OleDbConnection– conn.ConnectionString =
"Provider=SQLOLEDB; User ID=sa; Password=;
Data Source=localhost; Initial Catalog=Northwind" ;
Connection
– 只能夠連接 Microsoft SQL Server– 效能最佳化– 需引用 System.Data.SqlClient
– 可以透過 OLE DB 連接不同的資料庫– 應用範圍最廣– 需引用 System.Data.OleDb– 一樣可以連接 Microsoft SQL Server
Command • SqlCommand 與 OleDbCommand
• 支援多種 SQL 命令– DCL : GRANT, REVOKE, DENY
– DDL : CREATE, ALTER, DROP
– DML : SELECT, INSERT, DELETE, UPDATE
– 其它 : Stored Procedure, CURSOR, …
• 允許加入參數 (Parameters)
• 執行非查詢式 SQL 語法– SqlCommand cmd = new
SqlCommand("update… where OrderID=@ID", conn) ;
– SqlParameter param = new SqlParameter("@ID", typeof(int)) ;
– param.Value = 12 ;
– cmd.Parameters.Add(param) ;
– int rowEffects = cmd.ExecuteNonQuery() ;
執行 SQL 語法– 建立一個 Command 物件
– 建立參數物件– 設定參數值– 將參數加入 Command 物件– 執行 SQL 語法
• 執行查詢式 ( 單筆記錄 ) SQL 語法– string sql = "select cid from… where OrderID=@ID" ;
– SqlCommand cmd = new SqlCommand(sql, conn) ;
– SqlParameter param = new SqlParameter("@ID", typeof(int)) ;
– param.Value = 12 ;
– cmd.Parameters.Add(param) ;
– int myCID = (int) cmd.ExecuteScalar() ;
如何執行 SQL 語法– 建立一個 Command 物件– 建立參數物件– 設定參數值– 將參數加入 Command 物件– 執行 Command 並取得結果
• 執行查詢式 ( 多筆記錄 ) SQL 語法– string sql = "select PName, Qty from MyOrders" ;
– SqlCommand cmd = new SqlCommand(sql, conn) ;
– SqlDataReader dr = cmd.ExecuteReader() ;
– while ( dr.Read() ){
string pname = dr.GetString(0) ;int qty = dr.GetInt32(1) ;Console.WriteLine("{0}, {1}", pname, qty) ;
}
如何處理多筆記錄– 建立一個 Command 物件– 執行 Command 並取得 DataReader
– 讀取下一筆記錄 ( 如果 true 表示有資料 ){
取得第一個欄位取得第二個欄位
}
• 執行 Stored Procedure– SqlCommand cmd = new SqlCommand() ;
– cmd.Connection = conn ;
– cmd.ComandText = "DeleteOrder" ;
– cmd.CommandType = CommandType.StoredProcedure ;
– cmd.Parameters.Add("@ID", orderID) ;
– int rowEffects = cmd.ExecuteNonQuery() ;
如何執行 Stored Procedure
– 建立一個 Command 物件– 設定使用連線– 設定命令名稱– 設定 CommandType
– 建立參數– 執行 Stored Procedure
Data Access Layer Calling stored procedures
Dim sqlconn As SqlConnection = New SqlConnection(connectionstring)aDim sqlcomm As SqlCommand = New SqlCommand()sqlcomm.CommandType = CommandType.StoredProceduresqlcomm.CommandText = "DemoProc“
‘Open Connectionsqlconn.Open()sqlcomm.Connection = sqlconn
'Create parameterssqlcomm.Parameters.Add(New SqlParameter("@input1", SqlDbType.Int))sqlcomm.Parameters.Add(New SqlParameter("@input2", SqlDbType.Int))sqlcomm.Parameters.Add(New SqlParameter("@output1",
SqlDbType.Int)).Direction = ParameterDirection.Output
‘Set Parameter Valuessqlcomm.Parameters.Item("@input1").Value = 123sqlcomm.Parameters.Item("@input2").Value = 456sqlcomm.ExecuteNonQuery()
‘Access OutputResponse.write (sqlComm.Parameters.Item(“@output1”).Value.ToString())
WebForms 控制項資料繫結<%@ Import Namespace="System.Data.SqlClient" %><html><head><script language="C#" runat=server>
public void Page_Load(Object sender, EventArgs args) {
// 建立資料庫連線,並取得 DataReaderSqlConnection cnn = new SqlConnection("server=localhost;uid=sa;");conn.Open();SqlCommand cmd = new SqlCommand("select * from customers", conn);SqlDataReader dr = cmd.ExecuteReader();
// 將資料來源繫結到控制項dgCustomers.DataSource = dr;dgCustomers.DataBind(); }
</script></head><body><asp:DataGrid id="dgCustomers" runat="server"/>
</body></html>
自訂 Data Provider• ADO .NET 共有那些 Data Provider
– SQL Data Provider– OLE DB Data Provider– ODBC Data Provider ( 外掛 )– Oracle Data Provider ( 未來 )
SelectCommandSelectCommand
InsertCommandInsertCommand
UpdateCommandUpdateCommand
DeleteCommandDeleteCommand
DataAdapter 與 DataSet• DataSet 與資料庫之間透過 DataAdapter 進行存取動作
DataAdapterDataAdapter
DataSetDataSet資資料料庫庫 Update Update
Fill Fill
• 透過 DataAdapter 取得 DataSet 資料– SqlCommand cmd = new SqlCommand() ;
cmd.Connection = conn ;cmd.ComandText = "select * from Customers" ;
– SqlDataAdapter da = new SqlDataAdapter() ;
– da.SelectCommand = cmd;
– DataSet ds = new DataSet("Customers") ;
– da.Fill(ds) ;
如何取得 DataSet
– 建立一個 Command用來執行 SELECT 語法
– 建立 DataAdapter 物件– 設定 SelectCommand
– 建立一個 DataSet 物件– 使用 DataAdapter 物件的 Fill 方法
填滿 DataSet
– SqlDataAdapter da = new SqlDataAdapter("select * from Customers",
conn) ;
DataSet 的結構• DataSet 可以包含多個表格 (Table) ,組成 Tables 集合
DataSetDataSet
TablesTables
Tables[0]Tables[0]Tables[1]Tables[1]Tables[2]Tables[2]Tables[3]Tables[3]
::::::::::
DataTableDataTable
RowsRows
Rows[0]Rows[0]Rows[1]Rows[1]Rows[2]Rows[2]
::::::::
DataRowDataRow
• 使用 Tables[ 表格號碼 ] 或 Tables[" 表格名稱 "] 來引用 Tables 中的任何一個表格。(Tables.Count 可以取得表格數目 )
• 每一個 DataSet 中的表格都是一個 DataTable 物件• DataTable 可以包含多筆資料列 (Row) ,組成 Rows 集合• 使用 Rows[ 列號碼 ] 來引用 Tables 中的任何一個表格。(Rows.Count 可以取得資料總筆數 )
• Rows 集合中的每一筆資料記錄都是一個 DataRow 物件
AB(04)2222-2222男王小明血型電話性別姓名
3210
DataRowDataRow rowrow = = dsds..TablesTables[0].[0].RowsRows[[33];];stringstring name = name = rowrow[[00];];stringstring phone = phone = rowrow[["" 電話電話 ""];];
• 透過欄位號碼或欄位名稱來存取 DataRow 的資料• 每一個 DataRow ( 每一筆記錄 ) 中包含了多個欄位
• 使用 foreach 迴圈瀏覽 DataSet 資料foreach(DataRow cust in ds.Tables[0].Rows){
Console.WriteLine("\n 客戶 : {0}", cust["Name"]);foreach(DataRow ord in cust.GetChildRows("odr")){
Console.Write(" 訂單編號 : {0}", ord["id"]);Console.Write("\t 數量 :{1}\n", ord["qty"]);
}}
• 使用 for 迴圈瀏覽 DataSet 資料for(int c=0;c < ds.Tables[0].Columns.Count; c++) { DataColumn dc = ds.Tables[0].Columns[c] ;
Console.Write("{0}\t", dc.ColumnName) ;}for(int r = 0;r < ds.Tables[0].Rows.Count;r++) {
string cName = ds.Tables[0].Rows[r][" 客戶名稱 "] ;string cAddress = ds.Tables[0].Rows[r][" 地址 "] ;Console.WriteLine("\n{0}\t{1}", cName, cAddress) ;
}
如何瀏覽 DataSet 中的資料
Data, XML, And .NETXML Recap
• XML declaration• Document element• Elements• Attributes• Text content• Namespaces
<?xml version="1.0"?><?xml version="1.0"?><employees xmlns="urn:…"><employees xmlns="urn:…">
<employee id="123"><employee id="123"> <name>Philips</name><name>Philips</name> <salary>145000</salary><salary>145000</salary> </employee></employee> … …</employees></employees>
.NET XML Classes Namespaces and Classes
XmlReaderXmlReader
System.Xml namespaceSystem.Xml namespace
XmlWriterXmlWriter
XmlNavigatorXmlNavigator
XmlDocumentXmlDocument
XmlElementXmlElement
XmlAttributeXmlAttribute
etc.etc.
System.Xml.XPathSystem.Xml.XPath• Plus…
System.Xml.XslSystem.Xml.Xsl
XPath engineXPath engine
XSLT transformsXSLT transforms
.NET XML Classes Reading XML Data
• Use XmlReader class– Fast, forwards-only, read-only cursor– “Pull” model (not the “push” model of SAX)
• XmlReader is an abstract class– Several concrete
subclasses
XmlReaderXmlReader{ Abstract class }{ Abstract class }
XmlTextReaderXmlTextReaderRead a text-based Read a text-based
streamstream
XmlNodeReaderXmlNodeReaderRead an in-memory Read an in-memory
DOM treeDOM tree
.NET XML Classes Using XmlReader
• Example
• Capabilities– Pull nodes you want, skip those you don't– Get detailed node information– Get typed data
XmlReader reader;XmlReader reader;reader = new XmlTextReader("MyFile.xml");reader = new XmlTextReader("MyFile.xml");while(reader.Read())while(reader.Read()){ { …process nodes… } }
.NET XML Classes Writing XML Data
• Use XmlWriter
• Alternatively…– Create content using XmlDocument– Serialise data using XmlWriter
XmlWriterXmlWriter
XmlTextWriterXmlTextWriter XmlNodeWriterXmlNodeWriter
XmlWriter writer = new XmlTextWriter(…);XmlWriter writer = new XmlTextWriter(…);writer.WriteStartDocument();writer.WriteStartDocument();writer.WriteStartElement("name", "Mike");writer.WriteStartElement("name", "Mike");
.NET XML Classes Using XPath
• XPath maps an XML document to a tree of nodes
• Use XPath expressions to identify, select, and manipulate nodes in an XML hierarchy– Use location paths
to select nodes– XPath operators and functions
.NET XML Classes XML .NET Support for XPath
• XmlNavigator class provides XPath support
• Evaluate XPath expressions
• Select nodes using XPath
XmlNavigator nav;XmlNavigator nav;nav = new DocumentNavigator(document);nav = new DocumentNavigator(document);
nav.Evaluate("count(//employee)");nav.Evaluate("count(//employee)");
nav.Select("//employee[salary > 75000]");nav.Select("//employee[salary > 75000]");
.NET XML Classes Using XSLT
XML XML documentdocument
XSLT XSLT style sheetstyle sheet
++ XSLT XSLT processorprocessor
HTML document HTML document (XML-conformant)(XML-conformant)
XML XML documentdocument
XSLT XSLT style sheetstyle sheet
++ XSLT XSLT processorprocessor
XML document XML document (different grammar)(different grammar)
.NET XML Classes XML .NET Support for XSLT
• XslTransform class represents an XSLT style sheet
// Load style sheetXslTransform xsl = new XslTransform();XslTransform xsl = new XslTransform();xsl.Load("MyStylesheet.xsl");xsl.Load("MyStylesheet.xsl");
// Perform transformationxsl.Transform(aNavigator, xsl.Transform(aNavigator, null, null, anXmlWriter); anXmlWriter);
SyncSync
XML and ADO.NET Unified Architecture
DataReaderDataReader
SqlData-SqlData-ReaderReader
OleDbData-OleDbData-ReaderReader
Controls,Controls,Designers,Designers,
Code-gen, etc.Code-gen, etc.
DataSetDataSet
XmlReaderXmlReader
XmlText-XmlText-ReaderReader
XmlNode-XmlNode-ReaderReader
XSL/T, X-Path,XSL/T, X-Path,Validation, etc.Validation, etc.
XmlData-XmlData-DocumentDocument
DataAdapterDataAdapter
SqlData-SqlData-AdapterAdapter
OleDbData-OleDbData-AdapterAdapter
top related