Presentation is loading. Please wait.

Presentation is loading. Please wait.

Accessing Data with Microsoft Visual C# Applications.

Similar presentations


Presentation on theme: "Accessing Data with Microsoft Visual C# Applications."— Presentation transcript:

1 Accessing Data with Microsoft Visual C# Applications

2 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 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 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 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 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 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 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 GetCoName @CustID nchar(5),@CoName nchar(40) out as select @CoName = CompanyName from Customers where CustomerID = @CustID", myCn); PROCEDURE GetCoName @CustID nchar(5),@CoName nchar(40) out as select @CoName = CompanyName from Customers where CustomerID = @CustID", 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 = myCmd.Parameters.Add("@CustID", OleDbType.VarWChar, 5); 9 myParam = myCmd.Parameters.Add("@CoName", OleDbType.VarWChar, 40); 10 myParam.Direction = ParameterDirection.Output; 11 myCmd.Parameters["@CustID"].Value = "ABCD"; 12 myCmd.ExecuteNonQuery();

9 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 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 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 1 2 3 SQL Database

12 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 13 Scenario 5 Read XML Data  Demonstrates how to get XSD schema and XML data using the DataSet SqlConnection SqlDataAdapter DataSet 1 2 4 Fill GetXml() GetXmlSchema() 3 SQL Database

14 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 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 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 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 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 19 Scenario 8 Update Using DataSet  Use SqlCommand to update the data from a DataSet SqlConnection SqlDataAdapter DataSet Fill TablesRows 1 3 4 SQL Database Update InsertCommand UpdateCommand 2 Add Edit 5

20 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 (@RegionID, 'A new region')", myCn); 5 myParam = myDA.InsertCommand.Parameters.Add("@RegionID", SqlDbType.Int); 6 myParam.SourceColumn = "RegionID"; 7 myDA.UpdateCommand = new SqlCommand("Update Region Set RegionDescription = @RegionDescription WHERE RegionID = @RegionID", myCn); 8 myParam = myDA.UpdateCommand.Parameters.Add( "@RegionID“, SqlDbType.Int); 9 myParam.SourceVersion = DataRowVersion.Original; 10 myParam.SourceColumn = "RegionID";

21 21 Scenario 8 – con’t Update Using DataSet 11 myParam = myDA.UpdateCommand.Parameters.Add ("@RegionDescription", 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 22 Scenario 9 Handling Update Errors SqlDataAdapter2 (Table2) Rows SqlDataAdapter1 (Table1) Fill DataSet Fill Tables NewRow() Tables Edit Add Handle Errors 2 5 3 4 SqlCommandBuilder 1 Update 6

23 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 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 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 26 Scenario 10 Update Using Database Transactions SqlConnection SqlCommand Commit() SqlTransaction BeginTransaction() Rollback() 1 2 3 4

27 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 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 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 30 Scenario 12 Using ADO Recordset in ADO.NET SqlDataAdapter ADO Recordset DataSet Fill DB GetXml() 1 2 3

31 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 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 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 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 35 Try..Catch..Block try{ myCn.Open(); myCn.Open();} catch(Exception e) { Console.WriteLine(e.ToString()); Console.WriteLine(e.ToString());}finally{myCn.Close();}

36 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 37 Additional Resources  MSDN online: http://msdn.microsoft.com/voices/data.asp .NET SDK documentation

38 38


Download ppt "Accessing Data with Microsoft Visual C# Applications."

Similar presentations


Ads by Google