Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

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

Соседние файлы в папке c#