没有合适的资源?快使用搜索试试~ 我知道了~
示范性介绍对数据库数据进行插入、删除、修改、查询、统计等的基本操作的具体实现
资源推荐
资源详情
资源评论
嵌入式SQL_C语言应用案例
实验示例
SQL Server支持的嵌入式C的详细语法等请参阅SQL Server联机帮助。这里只是示范性介绍对数
据库数据进行插入、删除、修改、查询、统计等的基本操作的具体实现,通过一个个功能的示范与介
绍能体现出用嵌入式C实现一个简单系统的概况。
1、系统的总体功能
为简单起见,假设该学生学习管理系统要处理的信息只涉及到学生、课程与选课。其关系模式
关系名与表结构定义、表初始内容可参阅“实验3 SQL语言操作——SELECT查询操作”。
功能方面如上所述,将以实现基本功能为主。如子系统表的初始创建、表记录的插入、修改与
删除;表记录的查询、统计功能;其它如数据库中用户表的显示、通用SQL的动态执行等。
2、典型功能模块介绍
(1)含数据库的连接的main()主程序及辅助子程序(篇幅所限,可能牺牲程序格式)
int create_student_table(); //创建学生表
int create_sc_table(); //创建成绩表
int create_course_table(); //创建课程表
int insert_rows_into_student_table(); //对学生表添加记录
int insert_rows_into_sc_table(); //对成绩表添加记录
int insert_rows_into_course_table(); //对课程表添加记录
int current_of_delete_for_student(); //对学生表删除记录
int current_of_delete_for_sc(); //对成绩表删除记录
int current_of_delete_for_course(); //对课程表删除记录
int current_of_update_for_student(); //对学生表修改记录
int current_of_update_for_sc(); //对成绩表修改记录
int current_of_update_for_course(); //对课程表修改记录
int sel_student_total_grade_by_sno(); //输入学生学号,统计该生的成绩
int using_cursor_to_total_s_sc(); //分学生统计学生成绩
int using_cursor_to_total_c_sc(); //分课程统计学生成绩
int using_cursor_to_total_ty(); //通用统计功能
int sel_student_by_sno(); //输入学生学号,显示该生的信息
int using_cursor_to_list_s_sc_c(); //显示学生课程成绩表
int using_cursor_to_list_table_names(); //显示数据库用户表名
int using_cursor_to_list_student(); //显示所有学生的信息
int using_cursor_to_list_sc(); //显示所有成绩表信息
int using_cursor_to_list_course(); //显示所有课程表信息
int dynamic_exec_sql_command(); //动态执行SQL命令
void ErrorHandler(void); // 显示错误信息子程序
void pause(); // 暂停子程序
#include <stdlib.h>
#include <stddef.h> // standard C run-time header
#include <stdio.h> // standard C run-time header
#include "gcutil.h" // utility header
main(int argc, char** argv,char** envp)
{ int num=0,nRet;
char fu[2];
1
//先说明变量与主变量,用于连接的主变量说明如下:
EXEC SQL BEGIN DECLARE SECTION; // for CONNECT TO
char szServerDatabase[(SQLID_MAX * 2)+2] = ""; //放数据库服务器名与数据库名。
char szLoginPassword[(SQLID_MAX * 2)+2] = ""; //放登录用户名与口令。
EXEC SQL END DECLARE SECTION;
//接着是连接的相关设置与错误处理设置。
EXEC SQL WHENEVER SQLERROR CALL ErrorHandler();
// set Embedded SQL for C options
EXEC SQL SET OPTION LOGINTIME 10;
EXEC SQL SET OPTION QUERYTIME 100;
printf("Sample Embedded SQL for C application\n"); // display logo
// GetConnectToInfo()实现连接信息的获取,一般在“gcutil.c”C源程序中的。
nRet = GetConnectToInfo(argc, argv, szServerDatabase, szLoginPassword);
if (!nRet) { return (1); }
// 若不使用“GetConnectToInfo()”,则也可直接指定“服务器名.数据库名”与
//“用户名.口令名”来连接,如EXEC SQL CONNECT TO qh.qxz USER sa.sa;
// 这里“qh”为服务器名,“qxz”为数据库名,“sa”为用户名,“sa”为口令。
// 下面CONNECT TO命令真正实现与SQL Server的连接
EXEC SQL CONNECT TO :szServerDatabase USER :szLoginPassword;
if (SQLCODE == 0) { printf("Connection to SQL Server established\n"); }
else { // problem connecting to SQL Server
printf("ERROR: Connection to SQL Server failed\n"); return (1);}
for(;;){ // 循环显示菜单,并调用功能子程序
printf("Please select one function to execute:\n\n");
printf(" 0--exit.\n");
printf(" 1--创建学生表 7--修改学生记录 d--按学号查学生 i--统计某学生成绩 \n");
printf(" 2--创建课程表 8--修改课程记录 e--显示学生记录 j--学生成绩统计表 \n");
printf(" 3--创建成绩表 9--修改成绩记录 f--显示课程记录 k--课程成绩统计表 \n");
printf(" 4--添加学生记录 a--删除学生记录 g--显示成绩记录 l--通用统计功能 \n");
printf(" 5--添加课程记录 b--删除课程记录 h--学生课程成绩表 m--数据库用户表名 \n");
printf(" 6--添加成绩记录 c--删除成绩记录 n--动态执行SQL命令\n");
printf("\n");
fu[0]='0';
scanf("%s",&fu);
if (fu[0]=='0') exit(0);
if (fu[0]=='1') create_student_table();
if (fu[0]=='2') create_course_table();
if (fu[0]=='3') create_sc_table();
if (fu[0]=='4') insert_rows_into_student_table();
if (fu[0]=='5') insert_rows_into_course_table();
if (fu[0]=='6') insert_rows_into_sc_table();
if (fu[0]=='7') current_of_update_for_student();
if (fu[0]=='8') current_of_update_for_course();
if (fu[0]=='9') current_of_update_for_sc();
if (fu[0]=='a') current_of_delete_for_student();
if (fu[0]=='b') current_of_delete_for_course();
2
if (fu[0]=='c') current_of_delete_for_sc();
if (fu[0]=='d') sel_student_by_sno();
if (fu[0]=='e') using_cursor_to_list_student();
if (fu[0]=='f') using_cursor_to_list_course();
if (fu[0]=='g') using_cursor_to_list_sc();
if (fu[0]=='h') using_cursor_to_list_s_sc_c();
if (fu[0]=='i') sel_student_total_grade_by_sno();
if (fu[0]=='j') using_cursor_to_total_s_sc();
if (fu[0]=='k') using_cursor_to_total_c_sc();
if (fu[0]=='l') using_cursor_to_total_ty();
if (fu[0]=='m') using_cursor_to_list_table_names();
if (fu[0]=='n') dynamic_exec_sql_command();
pause();
}
EXEC SQL DISCONNECT ALL; // main()结束时断开SQL Server的连接
}
void ErrorHandler (void) // 显示错误信息子程序
{ // display error information from SQLCA
printf("\nError Handler called:\n");
printf("SQL Code = %li\n", SQLCODE);
printf("SQL Server Message %li: %Fs\n\n", SQLERRD1, SQLERRMC);
}
void pause() // 暂停子程序
{ char junk[80],c;
printf("Press any key to continue!");
c=getchar();gets(junk);
}
(2)表的初始创建(CREATE&INSERT)
系统能在第一次运行前,初始化系统用户表。程序在初始化前,先判断系统库中是否已存在学
生表?若存在,则询问是否要替换它?得到肯定回答后,便DROP已有表,CREATE TABLE创建
student表,接着通过“begin transaction…多条insert into…commit transaction”作为一个完整的事务,完
成插入记录。程序如下:
int create_student_table()
{ char yn[2];
EXEC SQL BEGIN DECLARE SECTION;
char tname[21]="xxxxxxxxxxx";
EXEC SQL END DECLARE SECTION;
EXEC SQL SELECT name into :tname FROM sysobjects
WHERE (xtype = 'U' and name='student');
if (SQLCODE == 0||strcmp(tname,"student")==0)
{ printf("The student table already exists,Do you want to delete it?\n",SQLCODE);
printf("Delete the table?(y--yes,n--no):");
scanf("%s",&yn);
if (yn[0]=='y' ||yn[0]=='Y'){
EXEC SQL drop table student;
if (SQLCODE == 0){ printf("Drop table student successfully!%d\n\n",SQLCODE); }
3
剩余10页未读,继续阅读
资源评论
OREA
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功