Accessing Data with Microsoft Visual C# Applications.

Slides:



Advertisements
Similar presentations
17. Data Access ADO.Net Architecture New Features of ADO.NET
Advertisements

ADO.Net Modelo para acesso a dados nas aplicação.Net Componentes: DataSet.Net Data providers: Connection Command DataReader DataAdapter.
ADO.NET: Working in Disconnected Environment Sergey Baidachni MCT, MCSD, MCDBA.
ADO vs ADO.NET ADOADO.NET Client/server coupledDisconnected collection of data from data server Uses RECORDSET object (contains one table) Uses DATASET.
Introduction to Database Processing with ADO.NET.
1 ADO.NET. 2.NET Framework Data Namespaces System.Data –Base set of classes and interfaces for ADO.NET System.Data.Common –Classes shared by the.NET Data.
ADO. NET. What is “ADO.Net”? ADO.Net is a new object model for dealing with databases in.Net. Although some of the concepts are similar to the classical.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
ADO.NET – part II August 2004 [ Marmagna Desai]. CONTENTS ADO vs ADO.NET ADO.NET – Managed providers Connecting to Database SqlConnection Selecting Database.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Chapter 61 Managing Data Sources Introduction to ASP.NET By Kathleen Kalata.
ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Overview of ADO.NET with the.NET Framework Scalable Development, Inc. Building systems today that perform tomorrow.
Developing Web Applications Using Microsoft ® Visual Studio ® 2008.
Accessing SQL Server and OLE DB from.NET Svetlin Nakov Telerik Corporation
CIS 375—Web App Dev II ASP.NET 10 Database 2. 2 Introduction to Server-Side Data Server-side data access is unique in that Web pages are basically ___________.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
Neal Stublen Populating a Database  SQLExpress should be installed with Visual Studio  The book provides a.sql file for populating.
Databases and Data Access  Introduction to ADO.NET  ADO.NET objects  ADP.NET namespaces  Differences between ADO and ADO.NET.
ADO.Net CS795. What is ADO.Net? Database language spoken by managed applications ADO.net database accesses go through modules: data providers –SQL Server.Net.
.NET Data Access and Manipulation ADO.NET. Overview What is ADO.NET? Disconnected vs. connected data access models ADO.NET Architecture ADO.NET Core Objects.
 It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data.
Objectives In this lesson, you will learn to: *Identify the need for ADO.NET *Identify the features of ADO.NET *Identify the components of the ADO.NET.
ASP.NET Rina Zviel-Girshin Lecture 5
Module 9: Accessing Relational Data Using Microsoft Visual Studio.NET.
Module 2: Connecting to Data Sources. Overview Choosing a.NET Data Provider Defining a Connection Managing a Connection Handling Connection Exceptions.
Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.
Module 7: Accessing Data by Using ADO.NET
Neal Stublen Tonight’s Agenda  Database Errors  Parameterized queries  ToolStrip control  Master-detail relationships  Custom.
Presented by Joseph J. Sarna Jr. JJS Systems, LLC
An Introduction to ADO.Net Marmagna Desai.NET Seminar, Fall-2003.
Module 3: Performing Connected Database Operations.
 It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data.
C# Programming in Depth Prof. Dr. Bertrand Meyer March 2007 – May 2007 Chair of Software Engineering Lecture 10: Database Lisa (Ling) Liu.
1 11/10/05CS360 Windows Programming ADO.NET. 2 11/10/05CS360 Windows Programming ADO.NET  Behind every great application is a database manager o Amazon.
Why ADO.NET Not your father’s Data Access.
DAT430 Extending Microsoft ® ADO.NET Building a Custom Data Factory API DAT430 Extending Microsoft ® ADO.NET Building a Custom Data Factory API Gert E.R.
Building Data-Centric Smart Client Applications Rajiv Sodhi Microsoft India.
ADO.Net CS795. What is ADO.Net? Database language spoken by managed applications ADO.net database accesses go through modules: data providers –SQL Server.Net.
1 11/15/05CS360 Windows Programming ADO.NET Continued.
HNDIT Rapid Application Development
DataSets Demystified. Who I am Shawn Wildermuth Senior Consultant with Magenic Technologies ( C# MVP INETA.
Module 4 Introduction ADO.NET.
1 Database Programming with ADO.NET Kashef Mughal.
DISTRIBUTED TRANSACTIONS IN.NET Presented By Divya Josyala.
Using ADO.Net to Build a Login System Dr. Ron Eaglin.
C# .NET Software Development
Active Data Objects Using.Net ADO.Net Farooq Ahmed Amna Umber Summayya Shehzad.
Module 2: Using ADO.NET to Access Data. Overview ADO.NET Architecture Creating an Application That Uses ADO.NET to Access Data Changing Database Records.
제 21 장 : ADO.NET 을 이용한 데이터 접근. ADO.NET - DataSet 사용하기.
Data Access. ADO.NET ADO.NET is the primary library for building database solutions within the.NET Framework. ADO.NET does not replace ADO. ADO and OLEDB.
.NET Data Access and Manipulation
ASP.NET Programming with C# and SQL Server First Edition
Introduction to ADO.NET
Introduction to Database Processing with ADO.NET
Introduction to Database Processing with ADO.NET
ADO.NET and Stored Procedures
ADO.NET Framework.
Active Data Objects Binding ASP.NET Controls to Data
ADO.Net and Stored Procedures
Tonga Institute of Higher Education
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
برنامه نویسی سیستم های شی گرا
Chapter 10 ADO.
PROG Advanced Web Apps 4/13/2019 Programming Data Pages Wendi Jollymore, ACES.
Active Data Objects Binding ASP.NET Controls to Data
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Introduction to Database Programming through ADO. NET
Presentation transcript:

Accessing Data with Microsoft Visual C# Applications

2 Agenda  Quick overview of Microsoft® Visual C#™ winform application  Data access with.NET data providers (3 scenarios): SQLClient data provider SQLClient data provider OleDb data provider OleDb data provider Using SQLDataAdapter Using SQLDataAdapter  Data population with ADO.NET (4 scenarios): Retrieve data with DataSet Retrieve data with DataSet Read XML data Read XML data Filter data in the DataSet Filter data in the DataSet Working with relations Working with relations

3 Agenda (2)  Updating Data in ADO.NET (3 scenarios): Update using DataSet Update using DataSet Handling update errors Handling update errors Update using database transactions Update using database transactions  Connection pooling (1 scenario)  Using ADO Recordset in ADO.NET (1 scenario)  Exception handling

4 Typical C# Windows Application 1 using System; 2 using System.Windows.Forms; 3 using System.Data; 4 using System.Data.SqlClient; 5 namespace YourAppName { 6public class Form1 : System.Windows.Forms.Form { 7private System.Windows.Forms.Button button1; 8private System.ComponentModel.Container components = null; 9public Form1()... 10protected override void Dispose( bool disposing )... Windows Form Designer generated code 11static void Main() { 12Application.Run(new Form1()); } 13private void button1_Click(object sender, System.EventArgs ev) { 14// Access your Data here }} }

5 Scenario 1 SQLClient Data Provider  Demonstrates the use of SQLClient data provider to insert a record into a table SqlConnection SqlCommand Insert Query 1 2 SQL Database

6 Scenario 1 – con’t SQLClient Data Provider 1 SqlConnection myCn = new SqlConnection("server=srv;uid=;pwd=;database=northwind"); 2 SqlCommand myCmd = new SqlCommand("INSERT INTO Customers(CustomerID, CompanyName) Values ('ABC','ABC Company')", myCn); 3 try { 4 myCn.Open(); 5 myCmd.ExecuteNonQuery(); // insert the record } 6 catch(Exception e) { 7 Console.Write("Couldn't insert record: " + e.ToString()); } 8 finally { 9 myCn.Close();// Close the connection explicitly }

7 Scenario 2 OleDb Data Provider  Demonstrates the use of OleDb data provider to execute a stored procedure that returns output parameters OleDbConnection OleDbCommand 1 create stored proc OleDbParameter (Out) 3 2 SQL Database

8 Scenario 2 – con’t OleDb Data Provider 1 OleDbConnection myCn = new OleDbConnection("Provider= SQLOLEDB; server=srv;uid=;pwd=;database=northwind“); 2 OleDbCommand procCmd = new OleDbCommand(“CREATE PROCEDURE nchar(40) out as = CompanyName from Customers where CustomerID myCn); PROCEDURE nchar(40) out as = CompanyName from Customers where CustomerID myCn); 3 myCn.Open(); 4 procCmd.ExecuteNonQuery(); 5 OleDbCommand myCmd = new OleDbCommand("GetCoName", myCn); 6 myCmd.CommandType = CommandType.StoredProcedure; 7 OleDbParameter myParam = null; 8 myParam = OleDbType.VarWChar, 5); 9 myParam = OleDbType.VarWChar, 40); 10 myParam.Direction = ParameterDirection.Output; 11 = "ABCD"; 12 myCmd.ExecuteNonQuery();

9 Scenario 3 Using SqlDataReader  Execute the CommandText against a SqlConnection and build the DataReader object SqlConnection SqlCommand ExecuteReader() SqlDataReader 1 2 SQL Database

10 Scenario 3 – con’t Using SqlDataReader 1 SqlDataReader myReader = null; 2 SqlConnection myCn = new SqlConnection(ConStr); 3 SqlCommand myCmd = new SqlCommand("select * from stores", myCn); try { try { 4 myCn.Open(); 5 myReader = myCmd.ExecuteReader (CommandBehavior.CloseConnection); 6 while (myReader.Read()) { 7Console.WriteLine(myReader[“StoreLocation"].ToString()); } } 9 catch(Exception e) { 10 Console.WriteLine(e.ToString()); } finally { finally { 11 if (myReader != null) 12 myReader.Close(); }

11 Scenario 4 Retrieve Data with DataSet  Demonstrates the use of DataAdapter and DataSet to get data from a SQL table SqlConnection SqlDataAdapter DataSet Fill TablesRows SQL Database

12 Scenario 4 – con’t Retrieve Data with DataSet 1 SqlConnection myCn = new SqlConnection(cnStr); 2 SqlDataAdapter myDA = new SqlDataAdapter(“Select * from Customers", myCn); try { try { 3 DataSet myDataSet = new DataSet(); 4 myDA.Fill(myDataSet, "myCustomers"); 5 foreach (DataRow myDataRow in myDataSet.Tables["myCustomers"].Rows) { 6Console.WriteLine(myDataRow["CustomerId"].ToString() + " " + myDataRow["CompanyName"].ToString()); } } catch (Exception e) { catch (Exception e) { 7Console.WriteLine(e.Message.ToString()); }

13 Scenario 5 Read XML Data  Demonstrates how to get XSD schema and XML data using the DataSet SqlConnection SqlDataAdapter DataSet Fill GetXml() GetXmlSchema() 3 SQL Database

14 Scenario 5 – con’t Read XML Data 1 try{ 2 SqlConnection myCn = new SqlConnection(cnStr); 3 SqlDataAdapter myDA1 = new SqlDataAdapter("select * from customers where CustomerID='ABC'", myCn); 4 DataSet myDataSet = new DataSet(); 5 myDA1.Fill(myDataSet,"Customers"); 6 String strXMLData = myDataSet.GetXml(); 7 String strXMLSchema = myDataSet.GetXmlSchema(); 8 Console.WriteLine("Schema"); 9 Console.WriteLine(strXMLSchema); 10 Console.WriteLine(""); 11 Console.WriteLine("Data"); 12 Console.WriteLine(strXMLData); } 13 catch(Exception e) { 14Console.WriteLine(e.ToString()); }

15 Scenario 6 Filter Data in the DataSet  Demonstrates how to use DataView to filter data in the DataSet SqlConnection SqlDataAdapter DataSet 1 2 Fill RowFilter 3 DataView SQL Database

16 Scenario 6 – con’t Filter Data in DataSet 1 SqlConnection myCn = new SqlConnection(cnStr); 2 SqlDataAdapter myDA = new SqlDataAdapter("select * from customers", myCn); try { try { 3 DataSet myDataSet = new DataSet(); 4 myDA.Fill(myDataSet,"Customers"); 5 DataView myDV = new DataView(myDataSet.Tables["Customers"]); 6 myDV.Sort = "CustomerID"; 7 myDV.RowFilter = "City='London'"; 8 for (int i = 0; i < myDV.Count; i++){ 9Console.Write(myDV[i]["CustomerID"].ToString() + " - " + myDV[i]["CompanyName"].ToString() + "\n"); } } catch(Exception e){ catch(Exception e){ Console.WriteLine(e.ToString()); }

17 Scenario 7 Working with Relations  Demonstrates how to add relations between tables in the DataSet and iterate over the data hierarchically SqlConnection DataAdapter1 (Table1) DataSet Fill DataAdapter2 (Table2) Fill Add Relations

18 Scenario 7 – con’t Working With Relations 1 SqlDataAdapter myDA1 = new SqlDataAdapter("select * from customers",myCn); 2 SqlDataAdapter myDA2 = new SqlDataAdapter("select * from Orders",myCn); 3 DataSet myDataSet = new DataSet(); 4 myDA1.Fill(myDataSet,"Customers"); 5 myDA2.Fill(myDataSet,"Orders"); 6 myDataSet.Relations.Add("CustOrder", myDataSet.Tables[“Customers"].Columns["CustomerId"], myDataSet.Tables["Orders"].Columns["CustomerId"]); 7 foreach (DataRow myParentRow in myDataSet.Tables["Customers"].Rows) { 8 Console.WriteLine("Customer: " + myParentRow["ContactName"].ToString()); 9 foreach (DataRow myChildRow in myParentRow.GetChildRows(myDataSet.Relations["CustOrders"])) myParentRow.GetChildRows(myDataSet.Relations["CustOrders"])) { 10 Console.WriteLine("Order #" + myChildRow["OrderId"].ToString()); } }

19 Scenario 8 Update Using DataSet  Use SqlCommand to update the data from a DataSet SqlConnection SqlDataAdapter DataSet Fill TablesRows SQL Database Update InsertCommand UpdateCommand 2 Add Edit 5

20 Scenario 8 – con’t Update Using DataSet 1 SqlConnection myCn = new SqlConnection(cnStr); 2 SqlDataAdapter myDA = new SqlDataAdapter("Select * from Region", myCn); 3 SqlParameter myParam = null; 4 myDA.InsertCommand = new SqlCommand("Insert into Region (RegionID, RegionDescription) VALUES 'A new region')", myCn); 5 myParam = SqlDbType.Int); 6 myParam.SourceColumn = "RegionID"; 7 myDA.UpdateCommand = new SqlCommand("Update Region Set RegionDescription WHERE RegionID myCn); 8 myParam = myDA.UpdateCommand.Parameters.Add( SqlDbType.Int); 9 myParam.SourceVersion = DataRowVersion.Original; 10 myParam.SourceColumn = "RegionID";

21 Scenario 8 – con’t Update Using DataSet 11 myParam = myDA.UpdateCommand.Parameters.Add SqlDbType.NChar, 50); 12 myParam.SourceVersion = DataRowVersion.Current; 13 myParam.SourceColumn = "RegionDescription"; 14 DataSet myDataSet = new DataSet(); 15 myDA.MissingSchemaAction = MissingSchemaAction.AddWithKey; 16 myDA.Fill(myDataSet, "Region"); 17 DataRow myDataRow = myDataSet.Tables["Region"].Rows.Find(2); 18 myDataRow[1] = “This region description is modified now"; 19 DataRow myDataRow2 = myDataSet.Tables["Region"].NewRow(); 20 myDataRow2[0] = 901; 21 myDataRow2[1] = "A new region"; 22 myDataSet.Tables["Region"].Rows.Add(myDataRow2); 23 myDA.Update(myDataSet, "Region");

22 Scenario 9 Handling Update Errors SqlDataAdapter2 (Table2) Rows SqlDataAdapter1 (Table1) Fill DataSet Fill Tables NewRow() Tables Edit Add Handle Errors SqlCommandBuilder 1 Update 6

23 Scenario 9 – con’t Handling Update Errors 1 DataSet myDataSet = new DataSet(); 2 DataRow myDataRow; 3 DataSet newDataSet; 4 DataRow[] rowsInError; 5 SqlConnection myCn = new SqlConnection(cnStr); 6 SqlDataAdapter myDACust = new SqlDataAdapter("Select * From Customers", myCn); 7 SqlDataAdapter myDAOrd = new SqlDataAdapter("Select * From Orders", myCn); 8 SqlCommandBuilder myCmdBlder = new SqlCommandBuilder(myDACust);

24 Scenario 9 – con’t Handling Update Errors 9 myDACust.MissingSchemaAction = MissingSchemaAction.AddWithKey; 10 myDAOrd.MissingSchemaAction = MissingSchemaAction.AddWithKey; 11 myDACust.Fill(myDataSet,"Customers"); 12 myDAOrd.Fill(myDataSet,"Orders"); 13 myDataSet.Relations.Add("CustOrders",myDataSet.Tables [“Customers"].Columns["CustomerId"],myDataSet.Tables["Orde rs"].Columns["CustomerId"]); 14 myDataSet.Tables["Customers"].Rows[0]["ContactName"]= "Peaches"; 15 myDataRow = myDataSet.Tables["Customers"].NewRow(); 16 myDataRow["CustomerId"] ="NewID"; 17 myDataRow["ContactName"] = "New Name"; 18 myDataRow["CompanyName"] = "New Company Name"; 19 myDataSet.Tables["Customers"].Rows.Add(myDataRow);

25 Scenario 9 – con’t Handling Update Errors 26 if (myDataSet.HasChanges(DataRowState.Modified | DataRowState.Added)) { 27 newDataSet = myDataSet.GetChanges(DataRowState.Modified | DataRowState.Added); 28 if(!newDataSet.HasErrors) { 29newDataSet.Merge(myDataSet); 30myDACust.Update(myDataSet, "Customers"); } 31 else { 32 foreach(DataTable newTable in newDataSet.Tables) { 33 if(newTable.HasErrors) { 34 rowsInError = newTable.GetErrors(); 35 for(int i = 0; i < rowsInError.Length; i++) { 36foreach(DataColumn newCol in newTable.Columns) { 37 Console.WriteLine(newCol.ColumnName + " " + rowsInError[i].GetColumnError(newCol)); rowsInError[i].GetColumnError(newCol));} 38rowsInError[i].ClearErrors(); } } } } }

26 Scenario 10 Update Using Database Transactions SqlConnection SqlCommand Commit() SqlTransaction BeginTransaction() Rollback()

27 Scenario 10 – con’t Update Using Database Transactions 1 SqlConnection myCn = new SqlConnection(cnStr); 2 SqlCommand myCmd = new SqlCommand(); 3 SqlTransaction myTrans; 4 myCn.Open(); 5 myCmd.Connection = myCn; 6 myTrans = myCn.BeginTransaction(); 7 myCmd.Transaction = myTrans; try { try { 8 myCmd.CommandText = "Insert into Region (Region, Description) VALUES (500, ‘Western')"; 9 myCmd.ExecuteNonQuery(); 10 myCmd.CommandText = "Insert into Region (Region, Description) VALUES (501, 'Eastern')"; 11 myCmd.ExecuteNonQuery(); 12 myTrans.Commit(); } catch(Exception e) { catch(Exception e) { 13myTrans.Rollback(); Console.WriteLine(e.ToString()); }

28 Scenario 11 Connection Pooling  Minimum and maximum number of connections in the pool  Use connections in the pool  Create new connections in the pool  Return connections to the pool  Create new connections in a different pool

29 Scenario 11 – con’t Connection Pooling 1 String cnStr1, cnStr2; 2 cnStr1 = "server=server;uid=uid;pwd=***;database=northwind; pooling=true;connection lifetime=5;min pool size=1;max pool size=50"; 3 SqlConnection myCn1 = new SqlConnection(cnStr1); 4 SqlConnection myCn2 = new SqlConnection(cnStr1); 5 SqlConnection myCn3 = new SqlConnection(cnStr1); 6 myCn1.Open(); 7 myCn2.Open(); 8 myCn1.Close(); 9 myCn2.Close(); 10 myCn1.Open(); 11 myCn2.Open(); 12 myCn3.Open(); 13 myCn1.Close(); 14 myCn2.Close(); 15 myCn3.Close(); 16 SqlConnection myCn4 = new SqlConnection(cnStr2); 17 myCn4.Open(); 18 myCn4.Close();

30 Scenario 12 Using ADO Recordset in ADO.NET SqlDataAdapter ADO Recordset DataSet Fill DB GetXml() 1 2 3

31 Scenario 12 Using ADO Recordset in ADO.NET via COM InterOp Layer 1 ADODB.Recordset rsObj = new ADODB.Recordset(); 2 String cnstr = “server=server;uid=id;pwd=***; database=Northwind; provider=sqloledb"; 3 rsObj.Open("Customers",cnstr, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockOptimistic,2); 4 DataSet myDataSet = new DataSet(); 5 OleDbDataAdapter adapter = new OleDbDataAdapter(); 6 adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; 7 int count = adapter.Fill(myDataSet, rsObj, "ADODB.RecordSet"); 8 Console.WriteLine(myDataSet.GetXml());

32 Scenario 12 Using ADO Recordset in ADO.NET via Reflection 1 Type rsType = Type.GetTypeFromProgID("ADODB.RecordSet"); 2 object rsObj = Activator.CreateInstance(rsType); 3 String cnstr = "server=srv;uid=myID;pwd=myPwd;database= Northwind;provider=sqloledb"; Northwind;provider=sqloledb"; 4 object[] values = new object[] {“Customers", cnstr, 0, 1, 2}; 5 rsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, rsObj, values); 6 DataSet myDataSet = new DataSet(); 7 OleDbDataAdapter adapter = new OleDbDataAdapter(); 8 adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; 9 int count = adapter.Fill(myDataSet, rsObj, "ADODB.RecordSet"); 10 Console.WriteLine(myDataSet.GetXml());

33 Exceptions  Error or unexpected behavior  Come from: An executing program An executing program The run time environment The run time environment  Passed up the stack: either handled by application or program terminates  Derived from the Exception class defined in the system namespace

34 ADO.NET Exceptions  Defined in System.Data Single exception class for each.NET data provider namespace: Single exception class for each.NET data provider namespace: SqlException SqlException OleDbException OleDbException OdbcException OdbcException Most System.Data exception classes do not add any additional properties or methods Most System.Data exception classes do not add any additional properties or methods SqlException and OleDbException both support the traditional ADO-style Errors collection SqlException and OleDbException both support the traditional ADO-style Errors collection

35 Try..Catch..Block try{ myCn.Open(); myCn.Open();} catch(Exception e) { Console.WriteLine(e.ToString()); Console.WriteLine(e.ToString());}finally{myCn.Close();}

36 Try..Catch..Block (2)  Always order catch blocks/exceptions from most specific to least specific That is, don’t put Exception before a SqlException catch block That is, don’t put Exception before a SqlException catch block  Throw exceptions only in exceptional cases Do not use exceptions for normal or expected errors or for normal flow of control Do not use exceptions for normal or expected errors or for normal flow of control

37 Additional Resources  MSDN online: .NET SDK documentation

38