stdafx.h"
#include "stdlib.h"
#include <stdio.h>
#include <
string.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <odbcss.h>
#include <iostream>
using namespace std;
SQLHENV henv =
SQL_NULL_HENV;
SQLHDBC hdbc1 =
SQL_NULL_HDBC;
SQLHSTMT hstmt1 =
SQL_NULL_HSTMT;
RETCODE retcode;
bool link()
{
UCHAR
szDSN[SQL_MAX_DSN_LENGTH +
1] =
"lxd123",
szUID[MAXNAME] =
"sa",
szAuthStr[MAXNAME] =
"123";
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, (SWORD)strlen((
char*)szDSN), szUID, (SWORD)strlen((
char*)szUID), szAuthStr, (SWORD)strlen((
char*
)szAuthStr));
if ((retcode != SQL_SUCCESS) && (retcode !=
SQL_SUCCESS_WITH_INFO)) {
return false;
}
return true;
}
void input(
char *ID,
char *
password)
{
SQLINTEGER p;
UCHAR pre_sql[50] =
"insert into internet values(?,?)";
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &
hstmt1);
p =
SQL_NTS;
SQLPrepare(hstmt1, pre_sql, 50);
SQLBindParameter(hstmt1, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(ID),
0, ID,
0, &
p);
SQLBindParameter(hstmt1, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(password),
0, password,
0, &
p);
SQLExecute(hstmt1);
SQLCloseCursor(hstmt1);
SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
void Register(
char *ID,
char *
password)
{
bool b =
true;
SQLINTEGER p;
CHAR getID[50],getpassword[
50];
int set_num =
0;
UCHAR inquiry[70] =
"select ID from internet where ID=\‘";
strcat((char*
)inquiry, ID);
strcat((char*)inquiry,
"\‘");
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &
hstmt1);
p =
SQL_NTS;
SQLPrepare(hstmt1, inquiry, strlen((char*
)inquiry));
SQLExecute(hstmt1);
while (SQL_NO_DATA != SQLFetch(hstmt1))
//移动光标,一直到集合末尾
{
SQLGetData(hstmt1, 1, SQL_C_CHAR, getID,
50, &
p);
b =
false;
}
if (b==
false)
{
printf("注册失败");
system("pause");
}
else
{
input(ID,password);
printf("注册成功");
system("pause");
}
SQLCloseCursor(hstmt1);
SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
system("pause");
}
int _tmain(
int argc, _TCHAR*
argv[])
{
link();
Register("ji5jin45",
"7979lxd");
return 0;
}
首先这是一个注册系统的实现:
第一步,配置ODBC服务器:
Win+R键打开运行菜单,输入odbcad32,点击回车
点击添加
选择SQL Server Native Client 11.0
编辑数据库的名字,Server选择SQL Server服务器的名字
输入SQL Server的用户名和密码(一定要与SQL Server的登录名和登陆密码一致)
配置成功,在这个菜单里就可以看见你所有的SQL Server的数据库,选择你需要连接的数据库即可,然后一直下一步
第二步,在VS2013端编辑代码
bool link()
{
UCHAR
szDSN[SQL_MAX_DSN_LENGTH + 1] = "lxd123", //服务器定义的名字
szUID[MAXNAME] = "sa", //数据库登录名
szAuthStr[MAXNAME] = "1231231"; //数据库登陆密码
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, (SWORD)strlen((char*)szDSN), szUID, (SWORD)strlen((char*)szUID), szAuthStr, (SWORD)strlen((char*)szAuthStr));
if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO)) {
return false;
}
return true;
}
这个是程序与数据库的连接代码,hdbc1是连接数据库的句柄,这个概念之后会解释到,如果前面设置没有错误的化,这里应该顺利连接
void input(char *ID,char *password)
{
SQLINTEGER p; //数据库输入变量
UCHAR pre_sql[50] = "insert into internet values(?,?)"; //传递给数据库执行的语句
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
p = SQL_NTS;
SQLPrepare(hstmt1, pre_sql, 50); //准备参数,将pre_sql赋值给hstmt1
SQLBindParameter(hstmt1, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(ID), 0, ID, 0, &p); //将hstmt1传给数据库,让数据库执行表语句
SQLBindParameter(hstmt1, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(password), 0, password, 0, &p);
SQLExecute(hstmt1);
SQLCloseCursor(hstmt1);
SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
SQLDisconnect(hdbc1);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
这段代码的的结果
输入一个用户名:zxbsba,密码21356316
如果注册账户已经存在:
团队项目:VS2013和SQL Server2012的连接使用
标签: