26.5.2 数据库中表的建立
在设计表之前,先建立新数据库和设计一个空表。在SQL Server中,通过SQL语句完成下面的插入语句,将所有的数据直接加入到数据库表中。
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('饶斌','950001','男','21','上海市','1981-03-18','三班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('范冰冰','950002','女','19','上海市','1983-11-03','三班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('代冉','950003','女','20','上海市','1982-12-14','三班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('富怡琴','950004','女','20','上海市','1982-09-01','三班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('姜晓燕','950005','女','20','上海市','1982-01-18','三班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('顾乔','950006','男','20','上海市','1982-11-08','三班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('郭振飞','950007','男','20','上海市','1982-01-11','三班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('张韬','950008','男','20','上海市','1982-05-01','三班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('杨慕铿','950009','男','20','上海市','1982-03-01','三班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('史正男','950010','男','22','上海市','1982-06-01','三班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('徐汉城','950011','男','20','上海市','1982-07-08','二班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('陈俊','950012','男','21','上海市','1981-07-13','二班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('卓越','950013','男','20','上海市','1982-08-08','二班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('魏国峰','950014','男','20','上海市','1982-03-23','二班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('袁乃沁','950015','女','18','上海市','1984-04-04','二班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('黄晓峰','950016','男','20','上海市','1982-05-03','二班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('齐宝华','950017','男','20','上海市','1982-11-11','二班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('节连松','950018','男','19','上海市','1983-02-08','一班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('王晓辉','950019','男','20','上海市','1982-04-08','一班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('秦嵩宁','950020','男','20','上海市','1982-04-11','一班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('金凌','950021','女','20','上海市','1982-12-18','一班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('杨坤','950022','男','20','上海市','1982-09-08','一班','计算机系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('徐文波','950101','男','21','上海市','1981-02-08','一班','生物系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('梁文深','950102','男','20','上海市','1982-09-08','一班','生物系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('陈雷','950103','男','20','上海市','1982-09-18','一班','生物系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('夏小勇','950104','男','22','上海市','1980-09-12','一班','生物系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('句龙','950105','男','20','上海市','1982-01-02','一班','生物系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('钱信林','950106','男','20','上海市','1982-03-02','一班','生物系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('张新伟','950107','男','21','上海市','1981-11-18','一班','生物系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('陈倩媚','950108','女','20','上海市','1982-04-14','一班','生物系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('卢婷','950109','女','20','上海市','1982-02-23','一班','生物系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('贺建林','950110','男','20','上海市','1982-02-01','一班','生物系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('阮从胜','950201','男','20','上海市','1982-01-08','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('王燕英','950202','女','20','上海市','1982-01-08','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('方成','950203','男','20','上海市','1982-01-08','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('林正新','950204','男','20','上海市','1982-01-08','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('陈曦','950205','男','20','上海市','1982-01-08','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('谭必奎','950206','男','20','上海市','1982-01-08','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('刘东','950207','男','21','上海市','1981-01-08','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)
values('陈锋','950208','男','21','上海市','1981-01-08','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('姜红','950209','女','20','上海市','1982-01-01','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('张济兴','950210','男','20','上海市','1982-02-18','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('马木龙','950211','男','20','上海市','1982-03-08','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('江润生','950212','男','20','上海市','1982-01-08','二班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('洪芙蓉','950213','女','20','上海市','1982-01-02','三班','机械系');
insert into studentinfo(stname, stcode, stsexy, stage, staddress, stbirthday, class, major)values('赵军','950214','男','20','上海市','1982-01-18','二班','机械系');
说明
这是数据库中运行的代码,这里不给代码编号,也不再单独说明,感兴趣的读者可以参考前面介绍JDBC的章节。
数据库中的表已经建立好了。下面设定ODBC数据源。
(1)单击“开始”|“控制面板”,打开窗口如图26.10所示,双击“管理工具”图标,选择“数据源”,如图26.11所示。
图 26.10 打开控制面板
图 26.11 选中数据源选项
(2)选择“数据源”后,弹出如图26.12所示的界面。由于上例中使用的是SQL数据库,而在用户数据源中没有,所以单击“添加”按钮,打开新建数据源对话框,如图26.13所示。
图 26.12 数据源管理器界面
图 26.13 创建新的数据源界面
(3)选择“SQL Server”选项,单击“完成”按钮,弹出对话框,如图26.14所示。
(4)填写好数据库名称,然后选择连接数据库服务器是哪一个,在这里是事先建立好的一个数据库“schoolmanage”,因为是本地数据库,所以服务器设置为本地计算机名或者“local”。如果是网络数据库,此时必须写入网络服务器名称,或者在下拉列表框中选择服务器。单击“下一步”按钮,出现如图26.15所示的界面。
图 26.14 确定数据库名称和服务器名称
图 26.15 设置登录
(5)单击“下一步”按钮,弹出确认对话框,如图26.16所示。继续单击“下一步”按钮,最后会出现一个测试界面。如果测试成功,说明创建数据源成功,如图26.17所示。
图 26.16 确认数据库名称
图 26.17 设置数据源完成界面
至此,数据源建立完毕。数据源建立后,接下来定义一个类,通过这个类将数据源与软件进行连接。其实这个类就是一个应用软件与数据库进行连接的接口,通过此接口,将软件界面中的控件与数据库连接,才可以让数据显示在控件中。