VB2008从入门到精通(PDF格式英文版)-第109部分
按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
Dim cmd As IDbmand = _
New Sqlmand(〃INSERT INTO draws (draw_date; first_number; second_number; 〃 _
& 〃third_number; fourth_number; 〃 _
& 〃fifth_number; sixth_number; bonus) 〃 _
& 〃VALUES (@pDrawDate; @pFirstNumber; @pSecondNumber; 〃 _
& 〃@pThirdNumber; @pFourthNumber; @pFifthNumber; @pSixthNumber; @pBonus)〃)
cmd。Connection = connection
Dim paramDate As IDbDataParameter = New SqlParameter()
paramDate。ParameterName = 〃@pDrawDate〃
paramDate。DbType = System。Data。DbType。DateTime
paramDate。Size = 8
paramDate。SourceColumn = 〃draw_date〃
paramDate。Value = DateTime。Now
cmd。Parameters。Add(paramDate)
Dim param As IDbDataParameter = New SqlParameter()
param。ParameterName = 〃@pFirstNumber〃
param。DbType = System。Data。DbType。Int32
param。Size = 4
param。SourceColumn = 〃first_number〃
param。Value = 1
cmd。Parameters。Add(param)
。 。 。
param = New SqlParameter()
param。ParameterName = 〃@pBonus〃
param。DbType = System。Data。DbType。Int32
param。Size = 4
param。SourceColumn = 〃bonus〃
param。Value = 1
cmd。Parameters。Add(param)
Dim retval As Integer = cmd。ExecuteNonQuery()
Console。WriteLine(〃retval (〃 & retval & 〃)〃)
connection。Close()
To execute a mand; you need to instantiate a class that implements the IDbmand
interface; where the constructor parameter is the SQL statement that you want to execute。 The
SQL statement contains SQL parameters。 How a SQL parameter is defined often depends on
the ADO implementation。 The mand is associated with the database connection using
the cmd。Connection property。
Each parameter; regardless of the database driver used; is of type IDataParameter or
IDbDataParameter (which implements IDataParameter)。 The type associated with IDbDataParameter
is SqlParameter in this case; because we are using SQL Server。
…………………………………………………………Page 406……………………………………………………………
384 CH AP T E R 1 4 ■ L E A R N I N G A B OU T R E L A TI O N AL DA TA B AS E D AT A
The properties of IDbDataParameter are as follows:
o ParameterName: Specifies the name of the parameter and must be prefixed with an at sign
(@) character。
o DbType: Specifies the type of the parameter。
o Size: Specifies the size of the parameter (in bytes)。
o SourceColumn: Specifies in which column the parameter will be stored。
o Value: Specifies the value for the parameter。
■Note As a general rule of thumb; stick to using and manipulating the standard ADO interfaces; rather
than the types specific to the ADO driver。
The example shows code that you would write when you want to explicitly define the
attributes of every parameter。 This was to allow you to see what is happening when parameters
are converted and stored in a table。 A simpler notation is as follows:
Dim connection As IDbConnection = _
New SqlConnection(DatabaseConsoleEx。My。Settings。Default。lotteryConnectionString)
connection。Open()
Dim cmd As IDbmand = New Sqlmand(〃INSERT INTO draws (draw_date; 〃 _
& 〃first_number; second_number; third_number; fourth_number; 〃 _
& 〃fifth_number; sixth_number; bonus) VALUES (@pDrawDate; 〃 _
& 〃@pFirstNumber; @pSecondNumber; @pThirdNumber;〃 _
& 〃@pFourthNumber;@pFifthNumber;@pSixthNumber;@pBonus)〃)
cmd。Connection = connection
cmd。Parameters。Add(New SqlParameter(〃@pDrawDate〃; DateTime。Now))
cmd。Parameters。Add(New SqlParameter(〃@pFirstNumber〃; 10))
cmd。Parameters。Add(New SqlParameter(〃@pSecondNumber〃; 1))
cmd。Parameters。Add(New SqlParameter(〃@pThirdNumber〃; 30))
cmd。Parameters。Add(New SqlParameter(〃@pFourthNumber〃; 19))
cmd。Parameters。Add(New SqlParameter(〃@pFifthNumber〃; 2))
cmd。Parameters。Add(New SqlParameter(〃@pSixthNumber〃; 21))
cmd。Parameters。Add(new SqlParameter(〃@pBonus〃; 5))
Once you have defined the parameters and assigned them with a value; the SQL query can
be executed。 In the case of a SQL INSERT statement; no data will be returned; because you are
sending data from the application to the database。 Thus; you will need to execute the method
that does not expect any return data; which is ExecuteNonQuery(); like this:
…………………………………………………………Page 407……………………………………………………………
C HA P TE R 1 4 ■ L E AR N I N G AB O U T R E L AT IO N A L D AT AB A SE D A TA 385
Dim retval As Integer = cmd。ExecuteNonQuery()
Console。WriteLine(〃retval (〃 & retval & 〃)〃)
ExecuteNonQuery() returns the number of rows affected by the statement (which should be
1; because we’ve inserted one row)。
Selecting Data from a Table
After your tables have some data; you probably will want to retrieve the data。 To do that; you
use the SELECT statement。 Here is the code for viewing data in the draws table:
Dim connection As IDbConnection = _
New SqlConnection(DatabaseConsoleEx。My。Settings。Default。lotteryConnectionString)
connection。Open()
Dim cmd As IDbmand = New Sqlmand(〃SELECT * FROM draws〃)
cmd。Connection = connection
Dim reader As IDataReader = cmd。ExecuteReader()
While reader。Read()
Console。WriteLine(〃(〃 & reader。GetDateTime(0)。ToString() & 〃) 〃 _
& reader。GetInt32(1))
End While
reader。Close()
connection。Close()
To select data; the steps are to open a connection; create a mand; and execute the
mand。 In the example; the SELECT statement did not have any parameters。 SELECT * means
to select all columns。 Alternatively; you could define identifiers to select specific columns。
When you are using a SELECT statement; the server will return data。 To read the returned
data; you call the method ExecuteReader(); which will also execute the mand。 This is different
from the ExecuteNonQuery() method you use to insert data; mainly in that ExecuteReader() returns
an instance of IDataReader。 IDataReader is an interface used to iterate individual records; which
gives you the chance to retrieve the individual fields of the records; and you can examine the
data in the columns for each record。
To access the fields; use the appropriate GetNNN () method; with the index of the field。
Knowing which index to use is a bit perplexing。 The index of the appropriate field is related to
the position of the column in the SELECT mand (though; in this case; we’ve selected all the
data with *; so it depends on the position of the column in the table)。 For example; the following
code retrieves the fourth column from a result set。
reader。GetDouble(3)
In our example; we obtain the date and the first draw number。
…………………………………………………………Page 408……………………………………………………………
386 CH AP T E R 1 4 ■ L E A R N I N G A B OU T R E L A TI O N AL DA TA B AS E D AT A
Deleting Data from the Database
Of course; you may need to delete data from table。 The SQL statement for removing data from
a table is DELETE。 The following example deletes a particular lottery drawing entry from the
draws table。
Dim cmd As IDbmand = _
New Sqlmand(〃DELETE FROM draws WHERE draw_date=@pDrawDate〃; _
CType(connection; SqlConnection))
Dim paramDate As IDbDataParameter = New SqlParameter()
paramDate。ParameterName = 〃@pDrawDate〃
paramDate。DbType = System。Data。DbType。DateTime
paramDate。Size = 8
paramDate。SourceColumn = 〃draw_date〃
paramDate。Value = DateTime。Now
cmd。Parameters。Add(paramDate)
cmd。ExecuteNonQuery()
connection。Close()
As with the INSERT statement; you use ExecuteNonQuery() with DELETE; which does not
return any results (except for the number of rows affected by the mand)。
Recapping ADO Usage
Looking at all of the code presented in this section; you should notice the following points
about using ADO directly:
o There are general interfaces; implemented by a specific library。
o The IDbConnection and IDbmand interfaces are implemented by all ADO drivers。
o SqlConnection; Sqlmand; and SqlParameter are specific classes from the database driver。
o The database code involves opening a connection; defining a SQL mand such as
INSERT; SELECT; and so on; assigning the SQL parameters; executing the SQL mand;
and then closing the connection。
■Note For more details on SQL; see Wikipedia’s SQL entry (http://en。wikipedia。org/wiki/SQL) and
W3School’s SQL tutorial (http://w3schools。/sql/default。asp)。 These provide a good expla
nation of the basics of SQL。 The MSDN documentation is fairly good as well。
Next; let’s take a look at another Visual Studio tool for working with database applications。
…………………………………………………………Page 409……………………………………………………………
C HA P TE R 1 4 ■ L E AR N I N G AB O U T R E L AT IO N A L D AT AB A SE D A TA 387
Using the Dataset Designer
The Dataset Designer is a utility p