1.使用C语言来操作SQL SERVER数据库,采用ODBC开放式数据库连接进行数据的添加,修改,删除,查询等操作。
step1:启动SQLSERVER服务,例如:HNHJ,开始菜单 ->运行 ->net start mssqlserver
step2:打开企业管理器,建立数据库test,在test库中建立test表(a varchar(200),b varchar(200))
step3:建立系统DSN,开始菜单 ->运行 ->odbcad32,
添加->SQL SERVER
名称:csql,服务器:HNHJ
使用用户使用登录ID和密码的SQLSERVER验证,登录ID:sa,密码:
更改默认的数据库为:test
...
测试数据源,测试成功,即DNS添加成功。
2.cpp文件完整代码
//##########################save.cpp##########################
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- SQLHENV henv = SQL_NULL_HENV;
- SQLHDBC hdbc1 = SQL_NULL_HDBC;
- SQLHSTMT hstmt1 = SQL_NULL_HSTMT;
-
-
-
-
-
-
- int main(){
- RETCODE retcode;
- UCHAR szDSN[SQL_MAX_DSN_LENGTH+1] = "csql",
- szUID[MAXNAME] = "sa",
- szAuthStr[MAXNAME] = "";
-
-
- UCHAR sql[37] = "insert into test values('aaa','100')";
-
- UCHAR pre_sql[29] = "insert into test values(?,?)";
-
-
- retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
- retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
- (SQLPOINTER)SQL_OV_ODBC3,
- SQL_IS_INTEGER);
-
- retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
- retcode = SQLConnect(hdbc1, szDSN, 4, szUID, 2, szAuthStr, 0);
-
- if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
- printf("连接失败!/n");
- } else {
-
-
-
-
-
-
-
- retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
-
-
-
-
-
-
-
- char a[200]="bbb";
- char b[200]="200";
- SQLINTEGER p = SQL_NTS;
-
- SQLPrepare(hstmt1,pre_sql,29);
-
- SQLBindParameter(hstmt1,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,200,0,&a,0,&p);
- SQLBindParameter(hstmt1,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,200,0,&b,0,&p);
-
- SQLExecute(hstmt1);
-
- printf("操作成功!");
-
- SQLCloseCursor (hstmt1);
- SQLFreeHandle (SQL_HANDLE_STMT, hstmt1);
-
- }
-
-
-
-
-
-
- SQLDisconnect(hdbc1);
- SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
- SQLFreeHandle(SQL_HANDLE_ENV, henv);
- return(0);
- }
#include
#include
#include
#include
#include
#include
#include
SQLHENV henv = SQL_NULL_HENV;
SQLHDBC hdbc1 = SQL_NULL_HDBC;
SQLHSTMT hstmt1 = SQL_NULL_HSTMT;
/*
cpp文件功能说明:
1.数据库操作中的添加,修改,删除,主要体现在SQL语句上
2.采用直接执行方式和参数预编译执行方式两种
*/
int main(){
RETCODE retcode;
UCHAR szDSN[SQL_MAX_DSN_LENGTH+1] = "csql",
szUID[MAXNAME] = "sa",
szAuthStr[MAXNAME] = "";
//SQL语句
//直接SQL语句
UCHAR sql[37] = "insert into test values('aaa','100')";
//预编译SQL语句
UCHAR pre_sql[29] = "insert into test values(?,?)";
//1.连接数据源
//1.环境句柄
retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3,
SQL_IS_INTEGER);
//2.连接句柄
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
retcode = SQLConnect(hdbc1, szDSN, 4, szUID, 2, szAuthStr, 0);
//判断连接是否成功
if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
printf("连接失败!/n");
} else {
//2.创建并执行一条或多条SQL语句
/*
1.分配一个语句句柄(statement handle)
2.创建SQL语句
3.执行语句
4.销毁语句
*/
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
//第一种方式
//直接执行
//添加操作
//SQLExecDirect (hstmt1,sql,37);
//第二种方式
//绑定参数方式
char a[200]="bbb";
char b[200]="200";
SQLINTEGER p = SQL_NTS;
//1预编译
SQLPrepare(hstmt1,pre_sql,29); //第三个参数与数组大小相同,而不是数据库列相同
//2绑定参数值
SQLBindParameter(hstmt1,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,200,0,&a,0,&p);
SQLBindParameter(hstmt1,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,200,0,&b,0,&p);
//3 执行
SQLExecute(hstmt1);
printf("操作成功!");
//释放语句句柄
SQLCloseCursor (hstmt1);
SQLFreeHandle (SQL_HANDLE_STMT, hstmt1);
}
//3.断开数据源
/*
1.断开与数据源的连接.
2.释放连接句柄.
3.释放环境句柄 (如果不再需要在这个环境中作更多连接)
*/
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return(0);
}
//##########################list.cpp##########################
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- SQLHENV henv = SQL_NULL_HENV;
- SQLHDBC hdbc1 = SQL_NULL_HDBC;
- SQLHSTMT hstmt1 = SQL_NULL_HSTMT;
-
-
-
-
- int main(){
- RETCODE retcode;
- UCHAR szDSN[SQL_MAX_DSN_LENGTH+1] = "csql",
- szUID[MAXNAME] = "sa",
- szAuthStr[MAXNAME] = "";
- UCHAR sql1[39] = "select b from test where a = 'aaa'";
- UCHAR sql2[35] = "select b from test where a = ? ";
- UCHAR sql3[19] = "select b from test";
-
- retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
- retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
- (SQLPOINTER)SQL_OV_ODBC3,
- SQL_IS_INTEGER);
- retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
-
- retcode = SQLConnect(hdbc1, szDSN, 4, szUID, 2, szAuthStr, 0);
- if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
- printf("连接失败!");
- } else {
-
-
-
-
-
-
-
- retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- SQLExecDirect (hstmt1,sql3,19);
- char list[5];
- SQLBindCol(hstmt1, 1, SQL_C_CHAR, list, 5, 0);
- do{
- retcode = SQLFetch(hstmt1);
- if(retcode == SQL_NO_DATA){
- break;
- }
- printf("%s/n",list);
- }while(1);
-
-
- SQLCloseCursor (hstmt1);
- SQLFreeHandle (SQL_HANDLE_STMT, hstmt1);
-
- }
-
-
-
-
-
-
-
- SQLDisconnect(hdbc1);
- SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
- SQLFreeHandle(SQL_HANDLE_ENV, henv);
- return(0);
- }
#include
#include
#include
#include
#include
#include
#include
SQLHENV henv = SQL_NULL_HENV;
SQLHDBC hdbc1 = SQL_NULL_HDBC;
SQLHSTMT hstmt1 = SQL_NULL_HSTMT;
/*
查询SQLSERVER数据库,1.条件查询,2.直接查询全部
*/
int main(){
RETCODE retcode;
UCHAR szDSN[SQL_MAX_DSN_LENGTH+1] = "csql",
szUID[MAXNAME] = "sa",
szAuthStr[MAXNAME] = "";
UCHAR sql1[39] = "select b from test where a = 'aaa'";
UCHAR sql2[35] = "select b from test where a = ? ";
UCHAR sql3[19] = "select b from test";
retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3,
SQL_IS_INTEGER);
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
//1.连接数据源
retcode = SQLConnect(hdbc1, szDSN, 4, szUID, 2, szAuthStr, 0);
if ( (retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) ) {
printf("连接失败!");
} else {
//2.创建并执行一条或多条SQL语句
/*
1.分配一个语句句柄(statement handle)
2.创建SQL语句
3.执行语句
4.销毁语句
*/
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
//第一种方式
/*
//直接执行
SQLExecDirect (hstmt1,sql1,39);
char list[5];
SQLBindCol(hstmt1, 1, SQL_C_CHAR, list, 5, 0);
SQLFetch(hstmt1);
printf("%s/n",list);
*/
//第二种方式
/*
//绑定参数方式
char a[200]="aaa";
SQLINTEGER p = SQL_NTS;
//1.预编译
SQLPrepare(hstmt1,sql2,35); //第三个参数与数组大小相同,而不是数据库列相同
//2.绑定参数值
SQLBindParameter(hstmt1,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,200,0,&a,0,&p);
//3.执行
SQLExecute(hstmt1);
char list[5];
SQLBindCol(hstmt1, 1, SQL_C_CHAR, list, 5, 0);
SQLFetch(hstmt1);
printf("%s/n",list);
*/
//第三种方式全部输出
/*
1.确认一个结果集是否可用。
2.将结果集的列绑定在适当的变量上。
3.取得行
*/
//3.检查结果记录(如果有的话)
SQLExecDirect (hstmt1,sql3,19);
char list[5];
SQLBindCol(hstmt1, 1, SQL_C_CHAR, list, 5, 0);
do{
retcode = SQLFetch(hstmt1);
if(retcode == SQL_NO_DATA){
break;
}
printf("%s/n",list);
}while(1);
//释放语句句柄
SQLCloseCursor (hstmt1);
SQLFreeHandle (SQL_HANDLE_STMT, hstmt1);
}
//4.断开数据源
/*
1.断开与数据源的连接.
2.释放连接句柄.
3.释放环境句柄 (如果不再需要在这个环境中作更多连接)
*/
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return(0);
}
3.总结:ODBC数据库操作与JDBC步骤上类似,可以融汇贯通来学习
本文来自于http://simpledev.javaeye.com/blog/339537