Net classes creating a dataset in code dataset create

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: taset.DataSetName = “BookAuthors”; DataTable authors = new DataTable(“Author”); DataTable books = new DataTable(“Book”); ADO.NET Classes Creating a DataSet in Code DataSet Define columns Define keys DataColumn id = authors.Columns.Add("ID", typeof(Int32)); id.AutoIncrement = true; authors.PrimaryKey = new DataColumn {id}; DataColumn name = new authors.Columns.Add("Name",typeof(String)); DataColumn isbn = books.Columns.Add("ISBN", typeof(String)); books.PrimaryKey = new DataColumn {isbn}; DataColumn title = books.Columns.Add("Title", typeof(String)); DataColumn authid = books.Columns.Add(“AuthID”,typeof(Int32)); DataColumn foreignkey = new DataColumn {authid}; 27 5/29/2013 5/29/2013 ADO.NET Classes Creating a DataSet in Code DataSet Add the tables to the DataSet dataset.Tables.Add (authors); dataset.Tables.Add (books); ADO.NET Classes Creating a DataSet in Code DataSet Add data and save the DataSet DataRow shkspr = authors.NewRow(); shkspr["Name"] = "William Shakespeare"; authors.Rows.Add(shkspr); DataRow row = books.NewRow(); row["AuthID"] = shkspr["ID"]; "1000row["ISBN"] = "1000-XYZ"; row["Title"] = "MacBeth"; books.Rows.Add(row); dataset.AcceptChanges(); 28 5/29/2013 5/29/2013 ADO.NET Classes Typed DataSets DataSet Typed DataSet Derived from base DataSet class Uses XML schema to generate new class Tables, columns, etc. compiled into new class ds.Customers.FirstName Untyped DataSet (*.dbml) No built-in schema Tables, columns, etc. exposed only as collections ds.Tables[“Customers”].Rows[0][“FirstName”] ADO.NET Classes Errors and Exceptions Error class Contains information on an error or warning returned by data source Created and managed by Errors class Errors class Contains all errors generated by an adapter Created by Exception class Exception class Created whenever an unhandled error occurs Always contains at least one Error instance 29 5/29/2013 5/29/2013 ADO.NET Classes Errors and Exceptions Example try { DataTable myTable = new DataTable(); myTable.Columns.Add(“myCol”); myTable.Columns.Add(“myCol”); //whoops! } catch (DataException myException) { "\ Console.WriteLine ("Message: " + myException.Message + "\n" + "\ "Source: " + myException.Source + "\n" + "\ “Stack Trace: " + myException.StackTrace + "\n"); } Installing SQL Server 2012 Express Management Studio Download and install: SQL Server 2012 Express Management Studio Express Create LocalDB instance: Sqllocaldb.exe create “SqlExpress” Start the LocalDB instance Sqllocaldb.exe start “SqlExpress” Use Management Studio to create new database (choose the folder on the file system) Add the connection to (localdb)\SqlExpress to the server explorer in Visual Studio and start using the database from Visual Studio (e.g., Data-Binding, etc.) 30 5/29/2013 5/29/2013 Connection Strings Use to prepare connection strings. 31...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online