按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 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