Stored Procedures Dr. Ralph D. Westfall May, 2009.

Slides:



Advertisements
Similar presentations
Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Advertisements

Stored procedures and views You can see definitions for stored procedures and views in the demo databases but you can’t change them. For views, expand.
11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Visual Basic.NET Data Entry Professor Ralph Westfall April 2011.
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.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
8/6/ ITE 370: SQL Stored Procedures. 8/6/ Stored Procedures A stored procedure is A stored procedure is a collection of SQL statements saved.
® IBM Software Group © 2006 IBM Corporation The Eclipse Data Perspective and Database Explorer This section describes how to use the Eclipse Data Perspective,
Apply Sub Procedures/Methods and User Defined Functions
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
CIS 451: ASP.NET Debugging and Server-Side Includes Dr. Ralph D. Westfall February, 2009.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
CIS 338: Creating ActiveX Controls Dr. Ralph D. Westfall March, 2003.
ASP.NET Web Application Professor Ralph Westfall May 2011.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
CIS 451: Using ASP.NET Objects with SQL Dr. Ralph D. Westfall February, 2009.
Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
1/36 Database Programming with Visual Basic.Net and MS Access IKE Lab. Yunho Song Database Management and Analysis.
CIS 375—Web App Dev II ASP.NET 10 Database 2. 2 Introduction to Server-Side Data Server-side data access is unique in that Web pages are basically ___________.
CIS 451: ASP.NET & Databases Dr. Ralph D. Westfall February, 2009.
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.
CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009.
CIS 338: Using Queries in Access as a RecordSource Dr. Ralph D. Westfall May, 2011.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
ASP.NET Rina Zviel-Girshin Lecture 5
ASP.NET OPTIMIZATION. Why Optimize? $$$ Whether you build applications for customers or not, enhanced applications save money.
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.
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.
CIS 338: VB.NET & Databases Dr. Ralph D. Westfall May, 2011.
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.
Gold – Crystal Reports Introductory Course Cortex User Group Meeting New Orleans – 2011.
Normalizing Database Files Professor Ralph Westfall May, 2011.
1 Database Programming with ADO.NET Kashef Mughal.
Using ADO.Net to Build a Login System Dr. Ron Eaglin.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Session 8: Data Management (with Stored Procedures)
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
SSMS SQL Server Management System. SQL Server Microsoft SQL Server is a Relational Database Management System (RDBMS) Relational Database Management System.
CIS 338: VB.NET Printing Dr. Ralph D. Westfall May, 2011.
Creating SQL Database file And Displaying a Database Table in a DataGridView.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 11 Creating Web Applications and Writing Data to a Database.
DBMS Programs MS SQL Server & MySQL
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
Visual Basic Fundamental Concepts
ASP.NET Programming with C# and SQL Server First Edition
Creating Data Base & Sql Data Source
Visual Basic 2010 How to Program
Stored Procedures Dr. Ralph D. Westfall May, 2011.
VB 2010 Pertemuan 10.
Lecture 6 VB.Net SQL Server.
Listing 9.1 ShowLocalConnection.aspx
ISC440: Web Programming 2 Server-side Scripting PHP 3
Visual Basic..
CIS16 Application Programming with Visual Basic
Computer Science Projects Database Theory / Prototypes
Creating Data Base & Sql Data Source
PROG Advanced Web Apps 4/13/2019 Programming Data Pages Wendi Jollymore, ACES.
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Presentation transcript:

Stored Procedures Dr. Ralph D. Westfall May, 2009

Getting Database Data when using a database, there are two places where a detailed request for data can be located inside a program that is separate from the database inside the database itself a stored procedure is a previously created query or program in a database

SQL Server Stored Procedures precompiled in SQL Server so they run faster can be reused to avoid recoding make code simpler like subprocedures do better security can give users access to data from stored procedures rather than to whole tables

Stored Procedures code to manipulate database (retrieve data, add, change, delete) stored in database like subroutine, can use in multiple programs (but not with Access database) less storage: once instead of multiple copies easier to update than multiple copies stored queries run faster inside database makes it easier to migrate applications to other platforms or scale up to larger volumes

Stored Procedures with VB create or open a Visual Basic project Data>Add new Data Source to get an existing SQL Server database and select the tables that you will use View>Server Explorer expand the database you want, right-click Stored Procedures>Add New Stored Procedure

Configure Stored Procedure change name in top line us/library/aa258259(SQL.80).aspx us/library/aa258259(SQL.80).aspx r/a/storedprocedure.htm r/a/storedprocedure.htm

Create Stored Procedure right-click Stored Procedures>New Stored Procedure

Stored Procedures with VB start SQL Server Management Studio Express attach or create a database file right-click database name>New Query paste in a sample query (e.g., from link above) and modify it to match fields in this database) run it to see results

Creating Stored Procedures start SQL Server Management Studio expand a database>expand Programmability>right-click Stored Procedures>New Stored Procedure click Query in top menu>Specify Values for Template Parameters replace values for Datatypes and Default Values

Complete Stored Procedure replace SELECT statement with SQL code for the procedure parameters usually are in WHERE clause WHERE [field] e.g., WHERE AGE use Query>Parse to verify syntax click !Execute button to compile it File>Save (rename it)>Save

Verify Stored Procedure click Refresh icon in Object Explorer expand [database name]> Programmability>Stored Procedures to verify that it's there

Test Stored Procedure click New Query and type the following: USE "database name"; 'within quotes GO EXECUTE [stored proc = = [value2]; GO ignore warnings click !Execute button and verify results

Sample Code uses Nations.mdf database from Classy ProjectClassy stored procedure is named dbo.GetByPopGolds SELECT * from nations2 where Pop and Gold

Add Procedure to Code create a VB Project with a ListBox, two Textboxes and a Button double click the Button to create a Sub and add code on following pages

Declarations Dim sqc As SqlCommand Dim da As SqlDataAdapter Dim ds As DataSet Dim con As SqlConnection Dim dr As DataRow Dim output As String Dim pads() As Integer = {4, 22, 3, 3, 3, 7, 15} Dim padDirection() As String = {"L", "R", "L", "L", "L", "L", "R"}

Connection Code ds = New DataSet con = New SqlConnection con.ConnectionString ="server=.\SQLEXPRESS;" _ & AttachDbFilename=[path]\Nations.mdf;" _ & "Integrated Security=True;"

Getting Data sqc = New SqlCommand sqc.CommandText = "Exec [dbo].[GetByPopGolds] " _ & TextBox1.Text & ", " & TextBox2.Text sqc.Connection = con da = New SqlDataAdapter da.SelectCommand = sqc da.Fill(ds) ListBox1.Sorted = True

Load Outputs For Each dr In ds.Tables(0).Rows output = "" For i As Integer = 0 To 6 If padDirection(i) = "L" Then output += CStr(dr(i)).PadLeft(pads(i)) & " " Else output += CStr(dr(i)).PadRight(pads(i)) End If Next ListBox1.Items.Add(output) Next

Using Optional Parameters Optional Parameters in SQL Stored Procedures Optional Parameters in SQL Stored Procedures create a stored procedure based on the code at the above web page right click the stored procedure name> Execute Stored Procedure>either check Pass Null Value or input the desired criterion (don't do both!), OK and review output

Dynamic SQL include SQL code in parameters rather than just values potential security risks (SQL injection) potential to create more flexible stored procedures more options

Microsoft Access can create stored procedures in VB code ccess_sp.aspx ccess_sp.aspx

Creating a Stored Procedure download sample.mdb into the project foldersample.mdb create a query using design view can also use query from form or report see Help on saving SQL statements as queries use Save As to save query within the database with the name you give it

Additional Activity try to modify code to run the Query that you created in the database