Neal Stublen Populating a Database  SQLExpress should be installed with Visual Studio  The book provides a.sql file for populating.

Slides:



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

Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Chapter 14.3 LINQ to SQL Programming in Visual Basic 2010: The Very Beginner’s Guide by Jim McKeown Databases – Part 3.
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
C# Programming: From Problem Analysis to Program Design1 Working with Databases C# Programming: From Problem Analysis to Program Design 3 rd Edition 14.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Microsoft Visual Basic 2012 CHAPTER TEN Incorporating Databases with ADO.NET.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
From VS C# 2010 Programming, John Allwork 1 VS2010 C# Programming - DB intro 1 Topics – Database Relational - linked tables SQL ADO.NET objects Referencing.
Some Basic Database Terminology
Databases and LINQ Visual Basic 2010 How to Program 1.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
11 Updating a Database Table Textbook Chapter 14.
ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances.
Advanced Visual Basic th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
CHAPTER EIGHT Accessing Data Processing Databases.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 30 – Bookstore Application: Client Tier Examining.
.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.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Thirteen Working with Access Databases and LINQ.
CHAPTER EIGHT Accessing Data Processing Databases.
Neal Stublen Tonight’s Agenda  Interfaces  Generics  Code Organization  Databases  ADO.NET  Datasets  Q&A.
11 Using ADO.NET II Textbook Chapter Getting Started Last class we started a simple example of using ADO.NET operations to access the Addresses.
Neal Stublen Tonight’s Agenda  Database Errors  Parameterized queries  ToolStrip control  Master-detail relationships  Custom.
ADO.NET Part 2. Slide 2 Overview Slide 3 Introduction to the DataGridView Control It’s a two-dimensional grid containing rows and columns Its use in.
Using Adapter Wizard ISYS 512. Data Adapter Wizard – 2 nd Level of Using ADO.Net Configure Data Adapter and generating a dataset: –From the Data tab of.
ADO.NET Objects – Data Providers Dr. Ron Eaglin. Requirements Visual Studio 2005 Microsoft SQL Server 2000 or 2005 –Adventure Works Database Installed.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Databases with LINQ. LINQ to SQL LINQ to SQL uses LINQ syntax to query databases. LINQ to SQL classes are automatically generated by the IDE’s LINQ to.
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.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Mauricio Featherman, Ph.D. Washington St. University
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
1 Avoiding Hacker Attacks. 2 Objectives You will be able to Avoid certain hacker attacks and crashes due to bad inputs from users.
HNDIT Rapid Application Development
1 Database Programming with ADO.NET Kashef Mughal.
1 CS 3870/CS 5870: Note07 Prog 4. Master Pages Creating a master page based on another master page MainMasterPage –For all Progs and Tests Prog4MasterPage.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Understand Databound Controls Windows Development Fundamentals LESSON 4.2A.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
ASP.NET Programming with C# and SQL Server First Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Visual Basic 2010 How to Program
ADO.NET Framework.
© 2013, Mike Murach & Associates, Inc.
Brief description on how to navigate within this presentation (ppt)
CIS16 Application Programming with Visual Basic
How to work with bound controls and parameterized queries
Database Applications
Chapter 10 Accessing Database Files
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

Neal Stublen

Populating a Database  SQLExpress should be installed with Visual Studio  The book provides a.sql file for populating the MMABooks database in SQLExpress  Double-click the.bat file on the S: drive  We’ll need to repeat this process at the start of each class session

Confirm Database Access  Using Visual Studio to locate the new database as a Data Source View > Server Explorer Add Connection... Server name:.\SQLEXPRESS Database name: MMABooks Test Connection

Data Provider Errors  SqlException  OracleException  OdbcException  OleDbException  Number  Message  Source  Errors

Catch Provider Exception private void Form1_Load(object sender, EventArgs e) { try { this.customersTableAdapter.Fill(...); } catch (SqlException ex) { // report ex.Number, ex.Message }

ADO.NET Errors  DBConcurrencyException  DataException  ConstraintException  NoNullAllowedException  Message

Catch ADO.NET Exception try { this.customerBindingSource.EndEdit(); this.customersTableAdapterManager.UpdateAll(...); } catch (DBConcurrencyException ex) { // from UpdateAll() exception // report concurrency error this.customerTableAdapter.Fill(...); } catch (DataException ex) { // from EndEdit() exception // report ex.Message customerBindingsSource.CancelEdit(); } catch (SqlException ex) { // report ex.Number, ex.Message }

DataGridView Control Errors  Not an exception, but an event on the control  DataError  Exception  RowIndex  ColumnIndex

Catch DataGridView Errors private void gridView_DataError(...) { // report error in e.RowIndex and/or // e.ColumnIndex }

Dataset Designer  Command property on Fill, GetData  Opens Query Builder  Visually build SQL command  Preview Data to see query results

Designer.cs Queries  SQL queries are updated in the schema’s Designer.cs file  DeleteCommand, InsertCommand, UpdateCommand  SCOPE_IDENTITY() = ID generated from INSERT command = query parameter  UPDATE only updates a record matching original column values

Bound TextBox Controls  Formatting and Advanced Binding  Select TextBox  Open Properties Window  Expand DataBindings property  Select Advanced option, click “…”  Select new format type  Specify representation of null value

Bound ComboBox Controls  Populate a ComboBox with values from a column of a database table  SelectedItem is used to specify the value in a column of another database table

Code Practice  Select customer state using dropdown list ComboBox instead of TextBox  Create StatesDataSet in Data Source window  Add DataSet control for StatesDataSet and set DataSetName property  Add BindingSource control for DataSet and set DataSource/DataMember properties  Set State field to use ComboBox  Set ComboBox to use data bound controls  Clear ComboBox data bindings for Text property

Parameterized Queries  We can customize a DataSet by providing parameters to modify the query  Parameters can be introduced using the Query Builder

Code Practice  Create a customer search form  Populate a DataGridView based on the entry within a TextBox  Create CustomersDataSet as a Data Source  Open CustomersDataSet.xsd and modify Fill CommandText using Query Builder  Change Name Filter to  Drag Customers table onto a form  Update Fill to append ‘%’  ToolStrip is added to provide parameter  Examine Fill button’s Click event

What was that ToolStrip?  A tool strip can be docked around the main window  It contains other controls  Controls can be added through the Items collection  Items have events just like other controls  We can add a “Cancel” button to the navigation tool strip CancelEdit() on the customersBindingSource

Navigation Tool Strip  customersBindingSource.AddNew();  customersBindingSource.EndEdit();  customersBindingSource.CancelEdit();  customersBindingSource.RemoveCurrent();  A binding source keeps all bound controls in sync

DataViewGrid Control  Smart tag allows you to modify commonly used properties  Columns can be added, moved, or removed Remove ID columns Columns still exist in the DataSet  Column content can be formatted using DefaultCellStyle

Master-Detail Relationships  One-to-many relationship between tables  One customer has many invoices

Code Practice  View customer invoices based on the selection of a customer record  Populate DataGridView with invoice entries  Create Customers-Invoices DataSet  Customers uses Detail View  Drag Customers onto Form  Drag Customers.Invoices onto Form  Examine DataSource/DataMember on grid view and invoicesBindingSource

Why create our own?  Place data objects into a shared library  We’re not using a form  Separates database code from UI code

Using Our Own Connections SqlConnection cxn = new SqlConnection(); cxn.ConnectionString = "..."; cxn.Open();... cxn.Close(); Sample Connection String: Data Source=localhost\SqlExpress; Initial Catalog=MMABooks; Integrated Security=False; User ID=Be Careful; Password=Be Very, Very Careful;

Using Our Own Commands SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT * FROM Customers"; cmd.CommandType = CommandType.Text; cmd.Connection = cxn; SqlReader r = cmd.ExecuteReader();

Parameters in Commands  Add parameters to SQL statements SELECT * FROM Customers WHERE STATE = 'VA' SELECT * FROM Customers WHERE STATE is a SQL variable representing the state

Create the Parameters SqlParameter stateParam = new SqlParameter(); stateParam.ParameterName = stateParam.Value = some_local_variable; cmd.Parameters.Add(stateParam); value);

SQL Injection  Don’t let this happen… string cmd = "SELECT * FROM Customers WHERE State=" + value;

Executing Commands SqlDataReader r = cmd.ExecuteReader(); List customers = new List (); while (r.Read()) { Customer c = new Customer();... customers.Add(c); } r.Close(); cxn.Close();

Other Commands object result = cmd.ExecuteScalar(); // Cast result to expected type cmd.ExecuteNonQuery();

Examine Chapter 20 Code  MMABooksDB  CustomerDB GetCustomer – ExecuteReader, exceptions AddCustomer – current ID UpdateCustomer – concurrency, ExecuteNonQuery  StateDB  frmAddModifyCustomer

Disposable Objects  IDisposable interface  Single method: Dispose()  Releases unmanaged resources that may be held by an object  Such as a database connection!!

Using…  using keyword can be used to confine objects to a particular scope  using also ensures that Dispose() is called if the object implements IDisposable  using also calls Dispose if an exception is thrown

Disposable Connections using (SqlConnection cxn =...) { cxn.Open(); using (SqlCommand cmd =...) { cmd.Execute... }

Using Equivalence using (object obj = …) { } object obj = …; try { } finally { obj.Dispose(); }