
- •maranGraphics
- •CREDITS
- •ACKNOWLEDGMENTS
- •ABOUT THE AUTHORS
- •AUTHORS’ ACKNOWLEDGMENTS
- •TABLE OF CONTENTS
- •HOW TO USE THIS BOOK
- •INTRODUCTION TO C#
- •START VISUAL STUDIO .NET
- •OPEN A NEW C# PROJECT
- •OPEN A C# WEB PROJECT
- •SET JSCRIPT .NET AS THE DEFAULT SCRIPT LANGUAGE
- •EXPLORE THE CLASS VIEW WINDOW
- •VIEW THE CONTENTS WINDOW
- •GET HELP USING THE INDEX WINDOW
- •SEARCH FOR HELP
- •ADD COMPONENTS FROM THE TOOLBOX
- •ADD A TASK TO THE TASK LIST
- •CHANGE FORM PROPERTIES IN THE PROPERTIES WINDOW
- •ADD A CUSTOM TOOLBAR
- •DELETE A TOOLBAR
- •CHANGE THE VISUAL STUDIO ENVIRONMENT
- •MANAGE OPEN WINDOWS
- •OPEN A PROJECT
- •VIEW THE MAIN METHOD
- •COMBINE PROGRAM TYPES
- •ADD REFERENCE TYPES
- •ADD OPERATORS
- •INSERT ATTRIBUTES
- •ENTER CLASSES
- •ADD COMMENTS TO CODE
- •WRITE YOUR FIRST PROGRAM
- •ENTER XML DOCUMENTATION
- •ACCESS DOCUMENTATION
- •LOG A BUG REPORT
- •VIEW INFORMATION ABOUT C# BUILDING BLOCKS
- •PROGRAM CLASSES
- •ADD A CLASS
- •EMPLOY CLASS INHERITANCE
- •PROGRAM INSTANCE CONSTRUCTORS
- •INSERT DESTRUCTORS
- •PROGRAM STRUCTS
- •DISPLAY HEAP AND STACK INFORMATION
- •FIND TYPE INFORMATION
- •PROGRAM CONSTANT EXPRESSIONS
- •SPECIFY VALUE TYPES
- •PROGRAM NUMERIC TYPES
- •PROGRAM THE BOOLEAN TYPE
- •DECLARE REFERENCE TYPES
- •ENTER REFERENCE TYPE DECLARATIONS
- •CONVERT VALUE TYPES TO REFERENCE TYPES
- •PROGRAM POINTER TYPES
- •INSERT THE VOID TYPE
- •ADD INTERFACE PROPERTIES
- •ADD AN INTERFACE INDEX
- •VIEW INFORMATION ABOUT METHODS
- •ADD A METHOD
- •ADD STATIC METHODS
- •INCLUDE NON-STATIC METHODS
- •ENTER DELEGATES
- •PROGRAM EVENTS
- •ADD AN EVENT-HANDLING METHOD
- •VIEW INFORMATION ABOUT ARRAYS
- •ENTER SINGLE-DIMENSIONAL ARRAYS
- •ADD MULTIDIMENSIONAL ARRAYS
- •PROGRAM ARRAY-OF-ARRAYS
- •ITERATE THROUGH ARRAY ELEMENTS
- •SORT ARRAYS
- •SEARCH ARRAYS
- •IMPLEMENT A COLLECTIONS CLASS
- •PROGRAM STRUCTS
- •ADD AN INDEXER
- •INCLUDE ENUMERATIONS
- •CREATE STRING LITERALS AND VARIABLES
- •ASSIGN VALUES TO STRINGS
- •CONCATENATE STRINGS
- •COMPARE STRINGS
- •SEARCH FOR SUBSTRINGS
- •REPLACE CHARACTERS
- •EXTRACT SUBSTRINGS
- •CHANGE THE CHARACTER CASE
- •TRIM SPACES
- •REMOVE CHARACTERS
- •SPLIT A STRING
- •JOIN STRINGS
- •PAD STRINGS
- •VIEW INFORMATION ABOUT PROPERTIES
- •COMPARE PROPERTIES AND INDEXERS
- •PROGRAM PROPERTY ACCESSORS
- •DECLARE ABSTRACT PROPERTIES
- •INCLUDE PROPERTIES ON INTERFACES
- •VIEW INFORMATION ABOUT WINDOWS FORMS
- •ADD A WINDOWS FORM IN THE WINDOWS FORM DESIGNER
- •SET THE FORM TYPE
- •CHOOSE THE STARTUP WINDOWS FORM
- •CREATE A MODAL FORM
- •LAYOUT A FORM
- •SET A FORM LOCATION
- •CHANGE FORM PROPERTIES
- •CREATE A TRANSPARENT FORM
- •AN INTRODUCTION TO WEB FORMS AND CONTROLS
- •CREATE AN ASP.NET WEB SITE
- •CREATE A WEB FORM
- •ADD SERVER CONTROLS TO A WEB FORM
- •READ AND CHANGE PROPERTIES FROM OBJECTS ON A WEB FORM
- •USING SERVER-SIDE COMPONENTS ON WEB FORMS
- •INTRODUCING DATA ACCESS WITH ADO.NET
- •DISPLAY DATA WITH THE DATAGRID CONTROL
- •CONFIGURE THE DATAGRID CONTROL
- •INSERT DATA INTO A SQL DATABASE
- •UPDATE DATA FROM A SQL DATABASE
- •DELETE DATA FROM A SQL DATABASE
- •EXECUTE A STORED PROCEDURE IN A SQL DATABASE
- •READ XML FROM A FILE
- •SAVE XML TO A FILE
- •QUERY XML WITH XPATH
- •APPLY XSL TO XML
- •INTRODUCTION TO DISTRIBUTED APPLICATIONS
- •CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES
- •CREATE AN APPLICATION WITH SHARED ASSEMBLIES
- •VERSION A SHARED ASSEMBLY
- •CONFIGURE A CLIENT FOR A VERSIONED ASSEMBLY
- •CREATE A WEB SERVICE
- •USING A WEB SERVICE
- •INTRODUCTION TO EXCEPTION HANDLING
- •THROWING AN EXCEPTION
- •HANDLING EXCEPTIONS WITH THE CATCH BLOCK
- •USING THE FINALLY BLOCK
- •WRITE ERRORS TO THE APPLICATION LOG
- •BASIC EXAMPLES
- •WHAT’S ON THE CD-ROM
- •USING THE E-VERSION OF THIS BOOK
- •INDEX
- •Symbols & Numbers

C#
EXECUTE A STORED PROCEDURE IN A SQL DATABASE
ou can build secure, performance-driven applications Yby implementing stored procedures for accessing data.
Using stored procedures allows you to wrap your data access into precompiled procedures. These procedures can be secured, giving rights to only the users that need access.
If all your data access is put into stored procedures, you can remove direct access to your tables. Stored procedures give you known entry points to your data. If you keep read, update, and delete access enabled on your tables, you cannot protect your data from harmful modification, whether intentional or unintentional.
To implement stored procedures, first determine which provider you want to use, the SQLClient namespace or the OleDb namespace, depending on your database. No
matter which namespace you choose, you need a connection to the data source and a Command object to prepare, execute, and evaluate results of a stored procedure.
The key part of the Command object is collection of parameters. Parameters are used to pass in data that is needed to execute the SQL statements inside the stored procedure and to hold information that the program needs to inspect after the procedure has completed its execution. These output or return parameters may have records of data or just a single value that indicates the result of the execution.
EXECUTE A STORED PROCEDURE IN A SQL DATABASE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Á Save the file. |
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
||||
· Set the CommandType to |
||||||||||
|
‡ Add a SqlCommand |
StoredProcedure. |
||||||||
|
|
‚ Add a SqlParameter |
||||||||
variable initializing the |
|
|||||||||
|
||||||||||
variable for the ByRoyalty |
variable by setting the |
|||||||||
stored procedure and the |
properties, including the |
|||||||||
connection created. |
ParameterName, |
|||||||||
|
|
° Open the connection. |
SqlDbType, Direction, |
|||||||
|
|
and Value. |
||||||||
|
|
|
|

ACCESSING DATA WITH C# AND ADO.NET 12
You can shorthand the five lines that are required to prepare and set a parameter into a single line of code. In terms of code execution time, most likely both of these implementations would precompile down to the same Intermediate Language (IL). Which implementation to choose is a matter of style. The more verbose style is typically chosen because it is easier to troubleshoot.
The line of code for adding a parameter
cmdByRoyalty.Parameters.Add("@percentage",SqlDbT ype.Int, 15).Value=50;
can replace the following lines in the code used in the screenshots in this section
SqlParameter prmPercentage = new SqlParameter();
prmPercentage.ParameterName = "@percentageº;
prmPercentage.SqlDbType= SqlDbType.Int;
prmPercentage.Direction=
ParameterDirection.Input;
prmPercentage.Value=50;
cmdByRoyalty.Parameters.Add(prmPercentage);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
— Add a SqlDataReader |
|
¡ Close the database |
£ Click F5 to save, build, |
|||||||
|
|
||||||||||
variable and use the |
connection. |
and run the console |
|||||||||
ExecuteReader to run the |
|
™ Set a debug stop. |
application. |
||||||||
|
|
||||||||||
stored procedure. |
|
|
|
|
|
|
|
||||
|
|
± Output the contents of the |
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|||
SqlDataReader variable |
|
|
|
|
|
|
|
||||
using a while loop. |
|
|
|
|
|
|
|
■ A message appears showing the results of running the stored procedure.
243

C#
READ XML FROM A FILE
XML is a great lightweight storage of data for your applications. If you are using Microsoft SQL 2000, you can retrieve queries in the form of XML. You will
sometimes need to pull XML data from files.
To read XML files, you can use an implementation of the XMLReader class. The XMLReader class is an abstract base class that provides noncached, forward-only, read-only access. Because it is an abstract class, you need to use
one of the current implementations in the
System.XML namespace which are XMLTextReader, XMLValidatingReader, and XMLNodeReader classes.
Typically, you use the XMLTextReader if you need to access the XML as raw data. After you load the
XMLTextReader, you will iterate through XML data by using the Read method, sequentially retrieving the next record from the document. The Read method returns false if no more records exist. To process the XML data, each record has a node type that can be determined from the NodeType property. This NodeType property will help you determine how to process the node. The
XMLTextReader class will enforce the XML rules but does not provide data validation.
READ XML FROM A FILE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
⁄ Create a new console |
‹ Rename the namespace |
Á Add the Main function. |
|
|
· Use a while loop to |
||||||||||
|
|
||||||||||||||
application and open the |
to XMLSamples. |
‡ Create an |
|
move through the XML file. |
|||||||||||
Class1.cs file. |
› Rename the class name |
|
Note: You will need to copy |
||||||||||||
XmlTextReader variable |
|
||||||||||||||
¤ Add an alias to the |
|
||||||||||||||
to ReadXML. |
and initialize with null. |
|
photo_library.xml from the CD-ROM |
||||||||||||
System.IO and |
ˇ Save the file. |
° Create a new |
|
to the working directory. |
|||||||||||
System.Xml namespaces. |
|
|
|
|
|
||||||||||
|
XmlTextReader variable |
|
|
|
|
|
|||||||||
|
|
|
|
|
|
|
|
|
|
||||||
|
|
|
|
|
and initialize with the name |
|
|
|
|
|
|||||
|
|
|
|
|
of the XML file. |
|
|
|
|
|

USING THE XML FRAMEWORK CLASS 13
The following is an example that reads the XML with a StringReader and evaluates several node types. The output documents the nodes that are detected and writes out the node name, type, and value.
Example:
while (reader.Read() {
switch (reader.NodeType) {
case XmlNodeType.ProcessingInstruction:
OutputXML (reader, "ProcessingInstruction"); break; case XmlNodeType.DocumentType:
OutputXML (reader, "DocumentType"); break; case XmlNodeType.Comment:
OutputXML (reader, "Comment"); break; case XmlNodeType.Element:
OutputXML (reader, "Element"); while(reader.MoveToNextAttribute())
OutputXML (reader, "Attribute"); break;
case XmlNodeType.Text:
OutputXML (reader, "Text"); break; case XmlNodeType.Whitespace:
break;
}}
‚ Add a switch statement to check for element types.
— Create a case for an element type and write the XML to the console.
± Add a default case that does nothing.
¡ Set a debug stop.
™ Press F5 to save, build, and run the console application.
■ The contents of the XML file are displayed in
the console.
245