#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <odbcss.h>
SQLHENV henv = SQL_NULL_HENV;
SQLHDBC hdbc1 = SQL_NULL_HDBC;
SQLHSTMT hstmt1 = SQL_NULL_HSTMT;
/*
cpp文件功能说明:
1.数据库操作中的添加,修改,删除,主要体现在SQL语句上
2.采用直接执行方式和参数预编译执行方式两种
*/
#define SQL_NUMBER 2
int insert(SQLHSTMT hstmt1);
int main(){
RETCODE retcode;
UCHAR szDSN[SQL_MAX_DSN_LENGTH+1] = "csqla",
szUID[MAXNAME] = "sa",
szAuthStr[MAXNAME] = "pass";
//插入SQL语句
//UCHAR sql[]= "insert into login values('800','100')";
sprintf(sql,"insert into login values('%d','%d')",800,100);
//删除SQL语句
//UCHAR sql[] = "delete login ";
修改SQL语句
//UCHAR sql[] = "update login set Dlmm='100' where Dlzh='800'";
//sprintf(sql,"update login set Dlmm='%d' where Dlzh='%d',800,100);
//查询SQL语句
//UCHAR sql[] = "select * from login ";
//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,(SWORD)strlen(szDSN), szUID,(SWORD)strlen(szUID), szAuthStr,(SWORD)strlen(szAuthStr));
//判断连接是否成功
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,strlen(sql)); */
{
int insert(SQLHSTMT hstmt1);
insert(hstmt1);
}
printf("操作成功!");
//printf("%d",&sql);
//释放语句句柄
SQLCloseCursor (hstmt1);
SQLFreeHandle (SQL_HANDLE_STMT, hstmt1);
}
//3.断开数据源
/*
1.断开与数据源的连接.
2.释放连接句柄.
3.释放环境句柄(如果不再需要在这个环境中作更多连接)
*/
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
system("pause");
return(0);
}
int insert(SQLHSTMT hstmt1)
{
UCHAR sql[]= "insert into login values('800','100')";
SQLExecDirect (hstmt1,sql,strlen(sql));
return 0;
}
#include <string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <odbcss.h>
SQLHENV henv = SQL_NULL_HENV;
SQLHDBC hdbc1 = SQL_NULL_HDBC;
SQLHSTMT hstmt1 = SQL_NULL_HSTMT;
/*
cpp文件功能说明:
1.数据库操作中的添加,修改,删除,主要体现在SQL语句上
2.采用直接执行方式和参数预编译执行方式两种
*/
#define SQL_NUMBER 2
int insert(SQLHSTMT hstmt1);
int main(){
RETCODE retcode;
UCHAR szDSN[SQL_MAX_DSN_LENGTH+1] = "csqla",
szUID[MAXNAME] = "sa",
szAuthStr[MAXNAME] = "pass";
//插入SQL语句
//UCHAR sql[]= "insert into login values('800','100')";
sprintf(sql,"insert into login values('%d','%d')",800,100);
//删除SQL语句
//UCHAR sql[] = "delete login ";
修改SQL语句
//UCHAR sql[] = "update login set Dlmm='100' where Dlzh='800'";
//sprintf(sql,"update login set Dlmm='%d' where Dlzh='%d',800,100);
//查询SQL语句
//UCHAR sql[] = "select * from login ";
//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,(SWORD)strlen(szDSN), szUID,(SWORD)strlen(szUID), szAuthStr,(SWORD)strlen(szAuthStr));
//判断连接是否成功
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,strlen(sql)); */
{
int insert(SQLHSTMT hstmt1);
insert(hstmt1);
}
printf("操作成功!");
//printf("%d",&sql);
//释放语句句柄
SQLCloseCursor (hstmt1);
SQLFreeHandle (SQL_HANDLE_STMT, hstmt1);
}
//3.断开数据源
/*
1.断开与数据源的连接.
2.释放连接句柄.
3.释放环境句柄(如果不再需要在这个环境中作更多连接)
*/
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
system("pause");
return(0);
}
int insert(SQLHSTMT hstmt1)
{
UCHAR sql[]= "insert into login values('800','100')";
SQLExecDirect (hstmt1,sql,strlen(sql));
return 0;
}