1 / 26

C#: Introduction for Developers

Neal Stublen nstublen@jccc.edu. C#: Introduction for Developers. Open/Close Connections. ADO.NET uses “connection pooling” to optimize opening and closing connections to the database

tamarr
Download Presentation

C#: Introduction for Developers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Neal Stublen nstublen@jccc.edu C#: Introduction for Developers

  2. Open/Close Connections • ADO.NET uses “connection pooling” to optimize opening and closing connections to the database • cxn.Open() and cxn.Close() are using connections from the connection pool that share the same connection string • ADO.NET manages the actual connection to the database • http://msdn.microsoft.com/en-us/library/8xx3tyca(v=vs.110).aspx

  3. Think of it like this… class SqlConnectionPool { public SqlConnection Open(string cxnStr) { if (mPool.Contains(cxnString)) { return mPool[cxnString]; } // Create a new connection ... } }

  4. And… class SqlConnectionPool { public void CheckIdle() { foreach (cxn in mPool) { if (cxn.IsIdle()) { cxn.ReallyClose(); mPool.Remove(cxn); } } } }

  5. DataSets in Class Libraries • Create a DataSet in the class libraries • Select “Referenced DataSets” when adding a DataSet control to a form • Add a BindingSource • Add form controls and bind them to the BindingSource

  6. Menus, Tool BARS, and STATUS Bars

  7. Designer Walkthrough

  8. Summary • MenuStrip w/ defaults • ToolStrip w/ defaults • StatusStrip • View Menu Toggles • PerformClick() • ToolStripContainer w/ docking • ContextMenuStrip • SplitContainer • ErrorProvider

  9. Files and Data Streams

  10. File System Static Classes • System.IO namespace • Directory • CreateDirectory, Exists, Delete • File • Exists, Delete, Copy, Move • Path • Combine, GetDirectoryName, GetFileName, GetExtension, GetTempFileName • DirectorySeparatorChar, VolumeSeparatorChar

  11. File System Instance Classes • DirectoryInfo • EnumeratorDirectories(), EnumerateFiles() • FileInfo • Name, Length, Open(), OpenText()

  12. File System Exceptions • FileNotFoundException • DirectoryNotFoundException • EndOfStreamException • IOException

  13. Stream Classes • FileStream • StreamReader • StreamWriter • BinaryReader • BinaryWriter

  14. Code Practice • Browse for a text file • Place the filename in a TextBox • Read each line from the file and insert into a ListView • Use two columns in the ListView • Line number • Content

  15. Review • OpenFileDialog • ImageList • ListView, DetailsView

  16. XML Files

  17. What’s XML? • Structured data file • Tags identify each data element • Tags can have attributes and child tags • <Books> • <Book Code=“BK0001”> • <Name>Having Fun in Kansas City</Name> • <Price>19.95</Price> • </Book> • </Books>

  18. XML Tags • Elements are identified by start tags, <tag_name>, and end tags, </tag_name> • Content can be placed between tags in the form of text or additional elements • Elements can be described using a single tag, <tag_name /> • Comments are tags in the form, <!-– my comment -->

  19. Tag Attributes • In addition to content, each tag can also contain zero, one, or more attributes instead of child elements: <Book ISBN=“978-1-890774-59-2”> </Book> <Book> <ISBN>978-1-890774-59-2</ISBN> </Book>

  20. Working with XML Files • Any text editor can be used to create XML files • Visual Studio helps create and edit XML files • Creates XML declaration • Color coded tags • Automatic indentation and closing tags • Expanding and collapsing tags

  21. XmlReader/XmlWriter • System.XML is the namespace that contains XML classes • Useful for exporting and importing data in a common format

  22. Writing XML Files XmlWriter w = XmlWriter.Create(path, settings); w.WriteStartDocument(); // A start tag w.WriteStartElement(root_name); // A nested start tag w.WriteStartElement(parent_name); // An attribute on the parent_name tag w.WriteAttributeString(name, value); // A complete element w.WriteElementString(child_name, value); // End tags for parent_name and root w.WriteEndElement(); w.WriteEndElement(); w.Close();

  23. Using XMLWriterSettings • Define indentation XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = " ";

  24. Code Practice • Create a new project called CustomerExport • Export the rows from the Customers table to Customers.xml • Consider how you would use SqlConnection, SqlCommand, and SqlReader • Save the XML file on the Desktop as…

  25. XML Format <Customers> <Customer id="157"> <Name>Abeyatunge, Derek</Name> <Address>1414 S. Dairy Ashford</Address> <City>North Chili</City> <State>NY</State> <ZipCode>14514 </ZipCode> </Customer> ... </Customers>

  26. Review • MemoryStream • XmlWriter • SqlDataReader “inspection” • System.Environment.GetFolderPath • FileStream • Debugging visualizers

More Related