■Note The one SQL Server numeric type that does not exist in is numeric。 This type behaves like the
decimal type in ; except for the precision。 With numeric; you can specify the number of digits before
and after the decimal point。
Persons Table
The persons table lists all of the people who have won a lottery drawing。 Table 14…2 shows the
column names and types for the persons table。
Table 14…2。 Persons Table Columns
Name Type
id uniqueidentifier
first_name nvarchar(100)
last_name nvarchar(100)
The persons table is a collection of people with their first names and last names。 The challenge
in a relational database is uniquely identifying a user。 Think of it as trying to define a unique
hash code。 The solution most databases use is a number。 When you have millions of records; a
…………………………………………………………Page 403……………………………………………………………
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 381
number might not be adequate as a unique identifier。 In that case; you can use the SQL Server
uniqueidentifier type; as we’re doing for the id column of the persons table。
The first_name and last_name columns both have the type nvarchar(100)。 A string in a
database behaves like a number type; in that strings have length limits。 In the example; we use
the nvarchar type; for a variable…length string with a maximum length of 100 characters。 In
contrast; specifying char(100) would give you a string 100 characters long; regardless of how
many bytes contain letters。 If the entry in a char column has fewer characters than specified;
the remainder of the char string is filled with space characters; by default。
Winners Table
The winners table matches the winning people to their lottery drawing。 Table 14…3 shows the
column names and types for the winners table。
Table 14…3。 Winners Table Columns
Name Type
id uniqueidentifier
draw_date datetime
Both columns are the types of the specific columns in the tables being referenced。 The
idea is to use the winners table in conjunction with the persons table and the draws table to
create the list that shows who won which lottery drawing and their numbers。
After you’ve created the three tables; your Database Explorer will resemble Figure 14…5。
Figure 14…5。 Modified database structure with the added tables
Now that we have a database with some tables; let’s see how to access that database
directly; using ADO。
…………………………………………………………Page 404……………………………………………………………
382 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
Accessing the Database Using ADO
Accessing the database directly using ADO involves using the ADO interfaces。 The
first step is to define a connection。 Once the connection has been established; you can manip
ulate the tables in the database—to add; remove; and update records。
Now we will continue with the sample lottery database created in the previous section。
We’ll write code to add; select; and delete records。
Connecting to a Database
Define a database connection with the following code; which illustrates a general approach
(added to the DatabaseConsoleEx application)。
Imports System。Data。SqlClient
。 。 。
Dim connection As IDbConnection = _
New SqlConnection(DatabaseConsoleEx。My。Settings。Default。lotteryConnectionString)
We need to import the System。Data。SqlClient namespace because it contains the classes
to access and use SQL Server (that is; the ADO driver)。
The variable connection is a SqlConnection object; which is specifically for accessing SQL
Server。 Think of it as picking up the telephone and hearing the telephone tone。 The connection
requires a username; password; and the name of the database to which you want to connect; which
are bundled into a connection string。 That information is stored in lotteryConnectionString;
which was defined when you configured the data source in Visual Basic Express。
Once you have a connection instance; you can create a live connection; which is akin to
dialing a telephone number and hearing that telephone ring。 Here is the code for opening
the connection:
connection。Open()
Now you can work with the tables in the database。
Closing a Database Connection
After having processed your SQL statements; you should close the connection to indicate that
you are finished using the database。 Here’s how:
connection。Close()
It is good practice to open and close database connections as quickly as possible。 You
should open a connection only when you are absolutely ready; and close it as soon as you are
finished with it。
Adding Table Data
The draws database table you created earlier is empty。 Now we will add some content。 To add
data to a database using SQL; use the SQL INSERT mand; as follows:
…………………………………………………………Page 405……………………………………………………………
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 383
Imports System。Data。SqlClient
。 。 。
Dim cmd As IDbmand = _
New Sqlmand(〃INSERT INTO draws (draw_date; first_number; second_number; 〃 _
小说推荐
- 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章