- •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#
UPDATE DATA FROM A SQL DATABASE
For .NET applications, you can use the System.Data namespace to update data in SQL databases. Using the System.Data namespace puts a layer of abstraction
between your code and the data store’s API (Application Programming Interface).
One way of updating data is by executing SQL update statements. SQL update statements are typically built from information the user provides. The current data that is in the SQL database is retrieved and displayed to the user. The user changes the values that need to be updated and then submits the information for updating.
A basic update statement contains the destination table, sets expressions, and includes an optional conditional
statement. The dynamic portions are the set expressions and the conditional statement. The set expression specifies which columns to update. The conditional statement determines which rows in the table need to be updated. Also, the conditional statement is typically based on the primary key(s) of the table.
The steps of running an update statement are very similar to running an insert statement, requiring a Connection object and a Command object. Within the Command object you set the CommandText property equal to the update statement. At this point, you can execute the update statement using ExecuteNonQuery. See page 240 for further details on ExecuteNonQuery.
UPDATE DATA FROM A SQL DATABASE
|
|
|
|
|
|
|
|
namespace |
Á Save the file. |
||
|
|
‡ Add a SqlConnection |
|
|
|
||
class name |
variable and initialize the |
||
. |
variable with a connection |
||
function. |
string to your database. |
||
|
|
|
° Add a string variable for the update command and initialize the string with a SQL statement that will update a row to your table.
ACCESSING DATA WITH C# AND ADO.NET 12
This console application does the same work as the code displayed in the screenshots of this section, but wraps it inside a database transaction.
TYPE THIS:
SqlTransaction txPubs = cnPubs.BeginTransaction(); cmdTitles.Transaction = txPubs; cmdTitles.ExecuteNonQuery(); Console.WriteLine("You ran the following:"); Console.WriteLine(sUpdateCmd); Console.Write("Commit change? [Y/N] ");
char cCommitResponse = (char) Console.Read(); if (cCommitResponse == char.Parse("Y"))
txPubs.Commit(); else
txPubs.Rollback();
RESULT:
C:\>csc UpdateData_ai.cs
C:\> UpdateData_ai.exe
You ran the following SQL Statement:
UPDATE authors SET zip = 30004 WHERE au_id = '172-32-1176'
Do you want to commit the change? [Y/N] Y
C:\>
· Add a variable with the and the
‚ Open execute
the connection
C#
DELETE DATA FROM A SQL DATABASE
For .NET applications, you can use the System.Data namespace for deleting data from SQL databases. If you learn how to delete data with the System.Data
namespace for your current database, you can reuse this knowledge to delete data on your next database choice.
Running a SQL delete statement is a simple way to remove rows from a table in a SQL database. Similar to inserting data, you can dynamically build out a SQL delete statement based on user input to the application.
A basic delete statement contains the requested table and a condition statement that indicates which row or rows in that table need to be deleted. The dynamic portion of
the delete statement typically is in the condition statement, but in some cases the table may be dynamically determined.
The basic process of running a SQL delete statement is very similar to running an insert statement. You need a Connection object and a Command object. Within the Command object, set the CommandText property equal to the delete statement. At this point, you can execute the delete statement with the ExecuteNonQuery method. The ExecuteNonQuery method runs SQL statements that do not need to return data, but instead return only the rows affected.
DELETE DATA FROM A SQL DATABASE
|
|
|
|
|
|
|
|
namespace |
Á Save the file. |
||
|
|
‡ Add a SqlConnection |
|
|
|
||
class name |
variable and initialize the |
||
|
variable with a connection |
||
Main function. |
string to your database. |
||
|
|
|
° Add a string variable for the delete command and initialize the string with a SQL statement that will delete a row from your table.
ACCESSING DATA WITH C# AND ADO.NET 12
If you want to clean up data or add data from the command-line, you can use the isql command line utility. This command line below does the same work as the code used to delete data from a SQL database in the task for this section.
Example:
isql -Usa -P -dpubs -Q"delete from titles where title_id='BU2222'"
The following is a key for the switches used in the above statement.
/*
where
-U = Database User -P = User’s password -d = Database Catalog
-Q = "query" and exit */
· Add a SqlCommand variable and use the string with the delete command and the connection created.
‚ Open a connection, execute the query, and close the connection.
3601-X fg1219_20.eps
|
— Add a message to the |
¡ Click F5 to save, build, |
||
|
||||
|
console about the SQL |
and run the console |
||
|
statement being executed. |
application. |
||
|
|
± Set a debug stop. |
|
■ The message about the |
|
|
|
||
|
|
|
SQL statement appears. |
241