Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 27 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
27
Dung lượng
403,33 KB
Nội dung
1 VB.Net 2005 - Chapter6 1 Chapter6WorkingwithDatainaConnectedEnvironment VB.Net 2005 - Chapter6 2 Chapter6 Lesson 0: Connected & Disconnectd Environment in ADO.NET Lesson 1: Creating and Executing Command Objects Lesson 2: Workingwith Parameters in SQL Commands Note: Lesson 3: BLOBs and Lesson 4: Bulk Copy: For reading at home Lesson 5: Performing Transactions by Using the Transaction Object 2 VB.Net 2005 - Chapter6 3 Lesson 0: Connected & Disconnectd Environmentin ADO.NET 2 ways using with ADO.NET Connection Data Adapter Data Source DataSet Web Form Windows Form Connection Command Data Source DataReader Web Form Windows Form Disconnectd Connected VB.Net 2005 - Chapter6 4 Disconnected Data Access Single database server can support many users reduced server's resources Data using more flexible Data not 'tied' to a connection easy to pass between tiers or persist to file Highly suited to Web and n-tier Apps Expensive when open, close connections Retrieving large result sets can be very slow Places demand on client memory and CPU Advantages Disadvantages 3 VB.Net 2005 - Chapter6 5 System.Data Architecture DataReader Database Program DataProvider DataSet DataAdapter VB.Net 2005 - Chapter66 Details: System.Data Architecture Command Object Connection Object Program Provider DataSet DataReader DataAdapter Database Action SQL Maintain data Maintain data Get data Get data 4 VB.Net 2005 - Chapter6 7 Main Difference:DataSet-DataReader DataSet Data structure to store schema and dataina disconnected fashion Useful for editing data offline and later update to data source DataReader Like Phone connection. Doesn’t need to store datain memory Object to access dataina connected, forward-only, read- only fashion When performance is your chief concern, especially with large amounts of data, use a DataReader class VB.Net 2005 - Chapter6 8 Differences DataSet and DataReader 5 VB.Net 2005 - Chapter6 9 Lesson 1: Creating and Executing Command Objects 1.What Are Command Objects? 2.Creating and Configuring Command Objects 3.Creating SQL Commands (SQL Statements) with the Query Designer VB.Net 2005 - Chapter6 10 Lesson 1: Creating and Executing Command Objects 1.What Are Command Objects? 6 VB.Net 2005 - Chapter6 11 Lesson 1: Creating and Executing Command Objects 1.What Are Command Objects? To execute SQL statements,stored procedures Contain the necessary information to execute SQL statements VB.Net 2005 - Chapter6 12 Lesson 1: Creating and Executing Command Objects 1.What Are Command Objects? Depend on Data Providers 7 VB.Net 2005 - Chapter6 13 Lesson 1: Creating and Executing Command Objects 1.What Are Command Objects? Common properties (p.254) CommandText SQL string, table name, proc name CommandType Text Proc Table Connection Parameters VB.Net 2005 - Chapter6 14 Lesson 1: Creating and Executing Command Objects 1.What Are Command Objects? Common Command Object Methods (p.255) ExecuteNonQuery ExecuteReader ExecuteScalar 8 VB.Net 2005 - Chapter6 15 Lesson 1: Creating and Executing Command Objects 2.Creating and Configuring Command Objects Creating a Command Object That Executes a SQL Statement Creating a Command Object That Executes a Stored Procedure Creating a Command Object That Performs Catalog Operations Creating a Command Object That Returns a Single Value VB.Net 2005 - Chapter6 16 Lesson 1: Creating and Executing Command Objects 2.Creating and Configuring Command Objects Creating a Command Object That Executes a SQL Statement (p.256) 9 VB.Net 2005 - Chapter6 17 Lesson 1: Creating and Executing Command Objects 2.Creating and Configuring Command Objects Creating a Command Object That Executes a Stored Procedure (p.257) VB.Net 2005 - Chapter6 18 Lesson 1: Creating and Executing Command Objects 2.Creating and Configuring Command Objects Creating a Command Object That Performs Catalog Operations (p.257) 10 VB.Net 2005 - Chapter6 19 Lesson 1: Creating and Executing Command Objects 2.Creating and Configuring Command Objects Creating a Command Object That Returns a Single Value (p.258) VB.Net 2005 - Chapter6 20 Lesson 1: Creating and Executing Command Objects 3.Creating SQL Commands (SQL Statements) with the Query Designer Creating SQL Commands (SQL Statements) with the Query Designer Performing Database Operations Using Command Objects [...]... of Parameters 3.Creating Parameters 4.Adding Parameters to Command Objects VB.Net 2005 - Chapter6 32 16 Lesson 2: Workingwith Parameters in SQL Commands 1.What Is a Parameter and Why Should I Use Them? parameter can be thought of as a type of variable use to pass and return values between your application and a database Parameter data types are assigned using the types defined in the System .Data. SqlDbType... SQL Commands (SQL Statements) with the Query Designer Performing Database Operations Using Command Objects (p 260 ) VB.Net 2005 - Chapter6 22 11 How to receive DataReader Connectionn Connectionn Open Commandn Commandn Which data? DataReadern DataReadern Result 23 VB.Net 2005 - Chapter6 DataReader Class Datareader and MS Access Store the information obtained by the command In stateless stream type... Workingwith Parameters in SQL Commands 4.Adding Parameters to Command Objects Command objects have a Parameters property that represents a collection of parameters After you create a parameter, you must add it to the Parameters collection of the Command object VB.Net 2005 - Chapter6 36 18 Lesson 2: Workingwith Parameters in SQL Commands Lab: Lab: Workingwith Parameters page 275 VB.Net 2005 - Chapter. .. Create MA access database Table Student(ID,FirstName,Lastname,Phone) Using Client dynamic SQL to put data in Listview Using Client dynamic SQL to detail data to Textboxes Exit VB.Net 2005 - Chapter6 44 22 Ex Insert new record Exit Save VB.Net 2005 - Chapter6 45 Access Insert into sv(ma,lname,fname,phone) Values (?,?,?,?) Cmd.parameters.add(p1) Cmd.parameters.add(p2) Cmd.parameters.add(p3) Cmd.parameters.add(p4)... the garbage collector-> explicitly close Tie the connection ‘ life to DataReader CommandBehavior.CloseConnection in ExecuteReader VB.Net 2005 - Chapter6 28 14 Note about DataReader The first row of data is not available until you call the Read method Using with stored procedure uses a return or output parameter, must close DataReader before get parameter DataReader cannot be used for data binding System.DBNull.value... MessageBox.Show(drSinhVien.GetString(0)) Loop drSinhVien.Close() conSinhVien.Close() VB.Net 2005 - Chapter6 27 Note: DataReader ADO.NET does not provide all the server-side cursor Don’t keep DataReaders open longer than necessary For flexible updates & client-side manipulation… Use DataSets and DataAdapters Only one DataReader use at a time Tie to Connection=> cannot used other DataReader To reuse connection=>call DataReader.Close... designated in the Direction property of the parameter Witha parameter, we ou can set its Direction property to Input, Output, InputOutput, or ReturnValue VB.Net 2005 - Chapter6 34 17 Lesson 2: Working with Parameters in SQL Commands 3.Creating Parameters (p 274) Create parameters:instance of the Parameter class setting its name and data type Choose ParameterDirection VB.Net 2005 - Chapter6 35 Lesson 2: Working. .. enumeration pass parameter values to SQL statements when we want to change the criteria of your queries quickly VB.Net 2005 - Chapter6 33 Lesson 2: Working with Parameters in SQL Commands 2.Types of Parameters (p 274) Input parameter (default) Output parameter InputOutput parameter InputOutput parameters are used to both send and receive data when executing a command The type of parameter is designated... sdr.GetString(0) sb.Append(strProductName) sb.Append(sdr.GetDecimal(1).ToString()) sb.Append(vbTab) sb.Append(sdr.GetInt 16( 2).ToString()) End While VB.Net 2005 - Chapter6 30 15 Lesson 1: Creating and Executing Command Objects 3.Creating SQL Commands (SQL Statements) with the Query Designer Lab: P 265 VB.Net 2005 - Chapter6 31 Lesson 2: Working with Parameters in SQL Commands 1.What Is a Parameter and Why... Cmd.parameters.add(p4) Insert into sv(ma,lname,fname,phone) Values (@ma,@lname,@fname,@phone) insert into sv(ma,[last name],[first name],phone) values (?,?,?,?) VB.Net 2005 - Chapter6 46 23 Exer 2 Copy Exer 1 to new folder Change the way to get data Client Dynamic SQL -> Client Parameters SQL VB.Net 2005 - Chapter6 47 Exer 3 Copy Exer 2 to new folder Change the way to get data Client Parameters SQL ->server Parameters . 1 VB.Net 2005 - Chapter 6 1 Chapter 6 Working with Data in a Connected Environment VB.Net 2005 - Chapter 6 2 Chapter 6 Lesson 0: Connected & Disconnectd Environment. CPU Advantages Disadvantages 3 VB.Net 2005 - Chapter 6 5 System.Data Architecture DataReader Database Program DataProvider DataSet DataAdapter VB.Net 2005 - Chapter 6 6 Details: System.Data Architecture Command. with the Query Designer VB.Net 2005 - Chapter 6 10 Lesson 1: Creating and Executing Command Objects 1.What Are Command Objects? 6 VB.Net 2005 - Chapter 6 11 Lesson 1: Creating and Executing