Because the team project is to develop a multiplayer online games, so need to use the registration page, while the game of some real-time data are all recorded in the database
So, recently learned the connection between VS2013 and SQL Server2012, with the following code:
#include"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 namespacestd; Sqlhenv henv=sql_null_henv; SQLHDBC HDBC1=Sql_null_hdbc; Sqlhstmt HSTMT1=sql_null_hstmt; RETCODE RETCODE;BOOLlink () {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;}voidInputChar*id,Char*password) {Sqlinteger p; UCHAR pre_sql[ -] ="INSERT into Internet values (?,?)"; Retcode= Sqlallochandle (sql_handle_stmt, HDBC1, &HSTMT1); P=sql_nts; SQLPrepare (HSTMT1, Pre_sql, -); SQLBindParameter (HSTMT1,1, Sql_param_input, Sql_c_char, Sql_char, strlen (ID),0Id0, &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);}voidRegister (Char*id,Char*password) { BOOLb =true; Sqlinteger p; CHAR getid[ -],getpassword[ -]; intSet_num =0; UCHAR inquiry[ -] ="Select ID from the 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))//move the cursor until the end of the collection{SQLGetData (HSTMT1,1, Sql_c_char, GetID, -, &p); b=false; } if(b==false) {printf ("Registration Failed"); System ("Pause"); } Else{input (Id,password); printf ("Registration Successful"); 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 (intARGC, _tchar*argv[]) {link (); Register ("Ji5jin45","7979lxd"); return 0;}
First, this is the implementation of a registration system:
The first step is to configure the ODBC server:
Win+r Key to open the Run menu, enter ODBCAD32, click Enter
Click Add
Select SQL Server Native Client 11.0
Edit the name of the database, server select the name of the SQL Server server
Enter the user name and password for SQL Server (be sure to match the login and login password for SQL Server)
The configuration is successful, you can see all of your SQL Server databases in this menu, select the database you need to connect to, and then keep the next step.
Step two, edit the code on the VS2013 side
BOOLlink () {UCHAR szdsn[sql_max_dsn_length+1] ="lxd123",//server-defined name Szuid[maxname]="SA",//Database login name Szauthstr[maxname]="1231231"; Database login Password Retcode= Sqlallochandle (Sql_handle_env, NULL, &henv); Defining Connection Variables Retcode=sqlsetenvattr (Henv, Sql_attr_odbc_version, (Sqlpointer) SQL_OV_ODBC3, Sql_is_integer); Retcode= Sqlallochandle (Sql_handle_dbc, henv, &HDBC1); Defining a handle 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;}
This is the connection code of the program and the database, HDBC1 is the handle of the connection database, this concept will explain, if the previous setting is not wrong, it should be connected smoothly
voidInputChar*id,Char*password) {Sqlinteger p; Database input variable UCHAR pre_sql[ -] ="INSERT into Internet values (?,?)"; The statement passed to the database execution Retcode= Sqlallochandle (sql_handle_stmt, HDBC1, &HSTMT1); P=sql_nts; SQLPrepare (HSTMT1, Pre_sql, -); Prepare the parameters, assign the Pre_sql to Hstmt1 SQLBindParameter (HSTMT1,1, Sql_param_input, Sql_c_char, Sql_char, strlen (ID),0Id0, &p); Pass the HSTMT1 to the database and let the database execute the table statement 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);}
The result of this piece of code
Enter a user name: ZXBSBA, password 21356316
If the registered account already exists:
Team project: VS2013 and SQL Server2012 connection use