友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
一世书城 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

VB2008从入门到精通(PDF格式英文版)-第136章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 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 IDbDataParam
返回目录 上一页 下一页 回到顶部 0 1
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!