1 // Connect to the Data source
2 // insert 10 rows in it.
9 inline void checkrc(int rc
, int line
)
13 printf("ERROR %d at line %d\n",rc
,line
);
26 SQLSMALLINT outstrlen
;
28 ret
= SQLAllocHandle(SQL_HANDLE_ENV
, SQL_NULL_HANDLE
, &env
);
29 checkrc(ret
,__LINE__
);
31 ret
= SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
32 checkrc(ret
,__LINE__
);
34 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
35 checkrc(ret
,__LINE__
);
37 ret
= SQLConnect (dbc
,
38 (SQLCHAR
*) "test", (SQLSMALLINT
) strlen ("test"),
40 (SQLSMALLINT
) strlen ("root"),
41 (SQLCHAR
*) "manager",
42 (SQLSMALLINT
) strlen (""));
44 if(SQL_SUCCEEDED(ret
))
46 printf("\nConnect to the data source successfully\n");
50 printf("Failed to connect\n");
55 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
56 checkrc(ret
,__LINE__
);
59 // inserting records into the table.
62 char ename1
[20]="jitu";
64 char ename2
[10][20]= {"Praba","Kishor","Jitu","Sanjit","Sanjay","Bisi","Suman","Vikrant","Eti","Suba"};
65 SQLINTEGER slen
=SQL_NTS
;
67 ret
= SQLPrepare(stmt
,(unsigned char*)"insert into emp values(?,?,?);",SQL_NTS
);
68 checkrc(ret
,__LINE__
);
70 ret
= SQLBindParameter(stmt
,1,SQL_PARAM_INPUT
,SQL_C_SLONG
,SQL_INTEGER
,0,0,&eid1
,0,NULL
);
71 checkrc(ret
,__LINE__
);
73 ret
= SQLBindParameter(stmt
,2,SQL_PARAM_INPUT
,SQL_C_CHAR
,SQL_CHAR
,196,0,(void*)ename1
,0,&slen
);
74 checkrc(ret
,__LINE__
);
76 ret
= SQLBindParameter(stmt
,3,SQL_PARAM_INPUT
,SQL_C_FLOAT
,SQL_REAL
,0,0,&salary1
,0,NULL
);
77 checkrc(ret
,__LINE__
);
83 salary1
= salary1
+ 1000;
84 strcpy(ename1
,ename2
[i
]);
85 ret
= SQLExecute(stmt
);
86 checkrc(ret
,__LINE__
);
88 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
89 checkrc(ret
,__LINE__
);
93 printf("%d rows inserted in 'emp' table\n",count
);
95 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
96 checkrc(ret
,__LINE__
);
98 ret
- SQLDisconnect(dbc
);
99 checkrc(ret
,__LINE__
);
101 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
102 checkrc(ret
,__LINE__
);
104 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
105 checkrc(ret
,__LINE__
);