The ODBC connection is divided into two steps:
1. Configuring the Local ODBC environment
2. Code.
I. Configuring the local ODBC environment
1. Start the SQL Server service.
Run->net start MSSQLServer
2. The SQL login method is mixed login
Step: (1) Open SQL 2008, use Windows identity Access, right-click Properties---security--Select hybrid authentication
(2) After you select the database under Security, login name->sa-> Change Password--restart
3. Create a new table
Database name named Test, create a new city table
4. Establish the System DSN, start and run->odbcad32->sql Server
The server name is computer name: Right click My Computer--Properties--View full computer name, change settings, copy computer full name
Click Finish, test data source, test successfully.
Two. Code section
#include <stdio.h>
#include <stdlib.h>
Required header Files
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <odbcss.h>
void Insert_city ();
void Select_all ();
Sqlhenv henv;
SQLHDBC HDBC;
SQLHSTMT hstmt;
Sqlreturn Retcode;
int main ()
{
SQLCHAR szdsn[sql_max_dsn_length+1]= "Test";
SQLCHAR szuid[maxname]= "SA";
SQLCHAR szauthstr[maxname]= "123";
Sqlreturn Retcode;
1. Environment handle
Retcode=sqlallochandle (SQL_HANDLE_ENV,NULL,&HENV);
Retcode=sqlsetenvattr (Henv,sql_attr_odbc_version, (Sqlpointer) sql_ov_odbc3,sql_is_integer);
2. Connection handle
Retcode=sqlallochandle (SQL_HANDLE_DBC,HENV,&HDBC);
Retcode=sqlconnect (Hdbc,szdsn,strlen ("Test"), Szuid,strlen ("sa"), Szauthstr,strlen ("123"));
if (retcode! = sql_success && retcode!=sql_success_with_info)
{
printf ("Connection failed!\n");
}
Else
{
printf ("Connection succeeded!\n");
Insert_city ();
System ("pause");
Select_all ();
}
Releasing the data source
SQLDisconnect (HDBC);
Sqlfreehandle (SQL_HANDLE_DBC,HDBC);
Sqlfreehandle (SQL_HANDLE_ENV,HENV);
return 0;
}
void Insert_city ()
{
printf ("\ n Insert city information ... \ n");
SQLCHAR sql[48]= "insert into city values (' Nanchang ', ' 100 ', ' 105 ')";
SQLCHAR pre_sql[32]= "insert into city values (?,?,?)";
Char Ci[maxname],lo[maxname],la[maxname];
Connection
Retcode=sqlallochandle (SQL_HANDLE_STMT,HDBC,&HSTMT);
Sqlinteger p1=sql_nts,p2=sql_nts,p3=sql_nts;
printf ("Please enter city Name:");
Gets (CI);
printf ("Please enter Longitude:");
Gets (LO);
printf ("Please enter Latitude:");
Gets (LA);
Pre-execution
SQLPrepare (hstmt,pre_sql,31);
Binding parameters
Retcode=sqlbindparameter (HSTMT,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,MAXNAME,0,&CI,0,&P1);
Retcode=sqlbindparameter (HSTMT,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,MAXNAME,0,&LO,0,&P2);
Retcode=sqlbindparameter (HSTMT,3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,MAXNAME,0,&LA,0,&P3);
Direct execution
Retcode=sqlexecute (HSTMT);
if (retcode!=sql_success && retcode!=sql_success_with_info)
printf ("Operation failed!\n");
else printf ("Operation succeeded!\n");
Release
SQLCloseCursor (HSTMT);
Sqlfreehandle (SQL_HANDLE_STMT,HSTMT);
}
void Select_all ()
{
Char ci[50]= "", lo[50]= "", la[50]= "";
Char *sql= "SELECT * from City";
Connection
Retcode = Sqlallochandle (sql_handle_stmt,hdbc,&hstmt);
Perform
SQLExecDirect (hstmt, (sqlchar*) Sql,strlen (SQL));
Binding parameters
SQLBindCol (hstmt,1,sql_c_char,ci,10,0);
SQLBindCol (hstmt,2,sql_c_char,lo,11,0);
SQLBindCol (hstmt,3,sql_c_char,la,11,0);
Do
{
Move
Retcode=sqlfetch (HSTMT);
if (Retcode = = Sql_no_data)
Break
printf ("%s%s%s\n", Ci,lo,la);
}while (1);
}
Pro-Test available!
Using the C language to connect MS SQL Server 2008--ODBC programming