Global。System。Data。SqlDbType。Int; 0;
Global。System。Data。ParameterDirection。Input; 0; 0; 〃first_number〃;
Global。System。Data。DataRowVersion。Current; false; Nothing; 〃〃; 〃〃; 〃〃))
。 。 。
End Sub
The bolded code shows how close the generated code is to a variation of the code used to
insert data in the ADO section。 The generated code is more explicit and verbose than the
previous code; but that does not matter; since you are not supposed to edit the generated code。
You are supposed to use the Dataset Designer。
■Note The Dataset Designer gives you the advantage of not having to provide bindings between Visual
Basic and a database; and an easy binding between a user interface and the database。 By providing a GUI
where you can drag; drop; and so on; the Dataset Designer just makes it much simpler to write that code。
Using the Generated Code
Using the code generated by the Dataset Designer is easy; as long as you know what is going on。
The Dataset Designer generates two major pieces of code: a table adapter and a dataset。 The
table adapter is code used to interact with the table directly and is the link between your code
and the database。 If you wanted to add; select; or delete items; use the table adapter。 When you
select data; the data will fill a dataset。
The following is the code to insert a record into the draws table (in the DatabaseConsoleEx
application)。
Dim table As DatabaseConsoleEx。lotteryDataSetTableAdapters。drawsTableAdapter = _
New DatabaseConsoleEx。lotteryDataSetTableAdapters。drawsTableAdapter()
table。Insert(DateTime。Now; 2; 12; 14; 31; 18; 4; 20)
The code is a trivial two…liner。 It instantiates the drawsTableAdapter; and then calls the
Insert() method to add a record。 Connecting to the database; executing the mand; and
assigning the parameters are done automatically。
…………………………………………………………Page 414……………………………………………………………
392 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
■Note Generated code is both a blessing and a curse。 Generated code hides plexity and makes it
simpler for you to get your job done。 But as you saw in the code excerpts; there is quite a bit going on behind
the scenes; and if you don’t understand ADO; you will not know what to do when things go wrong。 For
example; when is a connection to the database made? The only way to know that is to look at the generated
source code and follow the ADO calls。 A connection to a database is made the first time you call one of
the SQL methods (for example; Insert()); not when the adapter is initialized。
To retrieve and iterate the data in the table; use the following code。
Dim dataset As DatabaseConsoleEx。lotteryDataSet = _
New DatabaseConsoleEx。lotteryDataSet()
Dim table As DatabaseConsoleEx。lotteryDataSetTableAdapters。drawsTableAdapter = _
New DatabaseConsoleEx。lotteryDataSetTableAdapters。drawsTableAdapter()
Dim count As Integer = table。Fill(dataset。draws)
Console。WriteLine(〃Record count is (〃 & dataset。draws。Count & 〃)(〃 & count & 〃)〃)
For Each row As DatabaseConsoleEx。lotteryDataSet。drawsRow In dataset。draws
Console。WriteLine(〃Date (〃 + row。draw_date。ToString() & 〃) (〃 _
& row。first_number & 〃)〃)
Next
The variable table is initialized to an adapter that connects to the draws table。 The variable
dataset is an empty collection ready for the draws table。
To fill the data table in the dataset; the method table。Fill() is called; and the destination
is the dataset。draws data table。 After having called Fill(); the number of records read is returned
and assigned to count。
To iterate the individual rows; a For Each loop is used。 It references the type drawsRow; and
each instance of drawsRow has data members that represent the draw_date; first_number; and
other columns。 The iteration of the individual rows resembles the iteration of a collection。 The
ADO example used a While loop and required you to know which column was associated
with which SELECT field。
The Important Stuff to Remember
In this chapter; you learned about the basics of ADO and the Dataset Designer。 Here are
the main points to remember:
o The real problem when using a relational database and a programming language like
Visual Basic is the mismatch of set…based operations and individual object operations。
o To access a relational database; you can use ADO。 There is an ADO database
driver for each relational database。
o The Dataset Designer code is based on ADO; and thus if you understand ADO; you
will be able to understand how the Dataset Designer works and how it can be optimized。
…………………………………………………………Page 415……………………………………………………………
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 393
o When using ADO; the steps are typically to connect to a database; create a mand;
populate the parameters; execute the mand; retrieve the data (if necessary); close
the mand; and close the connection。
o SQL is a language used to manipulate the tables of a relational database。 You need to
learn SQL on top of learning how to use ADO。
Some Things for You to Do
The following are two exercises for applying what you’ve learned in this chapter。
1。 The basis of the lottery application is defined in terms of a database; tables; and data。
Wri
小说推荐
- oracle从入门到精通(PDF格式)
- -Page 1-Oracle 从入门到精通-Page 2-资源来自网络,仅供学习 Oracle 从入门到精通一、SQL 8
- 其他
- 最新章:第37章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 其他
- 最新章:第4章
- Java编程思想第4版[中文版](PDF格式)
- -Page 1-Page 2《Thinking In Java》中文版作者:Bruce Eckel主页:http/BruceEckel.编译:Trans Bot主页:http/memberease~transbot致谢-献给那些直到现在仍在孜孜不倦创造下一代计算机语言的人们!指导您利用万维网的语言进
- 其他
- 最新章:第295章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 其他
- 最新章:第309章
- VC语言6.0程序设计从入门到精通
- -Page 1-Visual C 6.0 程序设计从入门到精通求是科技 王正军 编著
- 其他
- 最新章:第136章
- SQL 21日自学通(V3.0)(PDF格式)
- -Page 1-SQL 21 日自学通(V1.0 翻译人 笨猪目录目录 1译者的话 14第一周概貌 16从这里开始 16
- 其他
- 最新章:第170章
- 2008年青年文摘精编版
- 作者:中国青年出版社“初恋”的惩罚.作者:凡 凡 文章来源《真情》2005年第4期 点击数:6608 更新时间:2005-6-5过了年,我就十八岁了。离高考只剩下四个多月了。这一段,班里的男女生相互间递纸条、写情书、约会等地下活动慢慢的多了起来。我这个“尖子生”也突然感到了不安、慌乱,并且自责。不知
- 文学名著
- 最新章:第230章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 其他
- 最新章:第27章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 其他
- 最新章:第28章