Neal Stublen
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 us/library/8xx3tyca(v=vs.110).aspx us/library/8xx3tyca(v=vs.110).aspx
Think of it like this… class SqlConnectionPool { public SqlConnection Open(string cxnStr) { if (mPool.Contains(cxnString)) { return mPool[cxnString]; } // Create a new connection... }
And… class SqlConnectionPool { public void CheckIdle() { foreach (cxn in mPool) { if (cxn.IsIdle()) { cxn.ReallyClose(); mPool.Remove(cxn); }
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
Designer Walkthrough
Summary MenuStrip w/ defaults ToolStrip w/ defaults StatusStrip View Menu Toggles PerformClick() ToolStripContainer w/ docking ContextMenuStrip SplitContainer ErrorProvider
File System Static Classes System.IO namespace Directory CreateDirectory, Exists, Delete File Exists, Delete, Copy, Move Path Combine, GetDirectoryName, GetFileName, GetExtension, GetTempFileName DirectorySeparatorChar, VolumeSeparatorChar
File System Instance Classes DirectoryInfo EnumeratorDirectories(), EnumerateFiles() FileInfo Name, Length, Open(), OpenText()
File System Exceptions FileNotFoundException DirectoryNotFoundException EndOfStreamException IOException
Stream Classes FileStream StreamReader StreamWriter BinaryReader BinaryWriter
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
Review OpenFileDialog ImageList ListView, DetailsView
What’s XML? Structured data file Tags identify each data element Tags can have attributes and child tags Having Fun in Kansas City
XML Tags Elements are identified by start tags,, and end tags, Content can be placed between tags in the form of text or additional elements Elements can be described using a single tag, Comments are tags in the form,
Tag Attributes In addition to content, each tag can also contain zero, one, or more attributes instead of child elements:
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
XmlReader/XmlWriter System.XML is the namespace that contains XML classes Useful for exporting and importing data in a common format
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.Close();
Using XMLWriterSettings Define indentation XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = " ";
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…
XML Format Abeyatunge, Derek 1414 S. Dairy Ashford North Chili NY
Review MemoryStream XmlWriter SqlDataReader “inspection” System.Environment.GetFolderPath FileStream Debugging visualizers