Using the C language to connect MS SQL Server 2008--ODBC programming

Source: Internet
Author: User
Tags change settings sql 2008 odbc connection

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,&AMP;HENV);
Retcode=sqlsetenvattr (Henv,sql_attr_odbc_version, (Sqlpointer) sql_ov_odbc3,sql_is_integer);


2. Connection handle
Retcode=sqlallochandle (SQL_HANDLE_DBC,HENV,&AMP;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,&AMP;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,&AMP;CI,0,&AMP;P1);
Retcode=sqlbindparameter (HSTMT,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,MAXNAME,0,&AMP;LO,0,&AMP;P2);
Retcode=sqlbindparameter (HSTMT,3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,MAXNAME,0,&AMP;LA,0,&AMP;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.