Click here to Skip to main content
14,739,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everybody ..
sample question please , i have console program and i want connect with databse sql , i want know whhat i should to do


What I have tried:

C++ pgroram for connecting to database (and error handling)
#include<sqlapi.h>		 // main SQLAPI++ header 

int main(int argc, char* argv[]) 
	// create connection object to connect to database 
	SAConnection con; 
		// connect to database 
		// in this example, it is Oracle, 
		// but can also be Sybase, Informix, DB2 
		// SQLServer, InterBase, SQLBase and ODBC 
		con.Connect ("test", // database name 
					"tester", // user name 
					"tester", // password 
					SA_Oracle_Client); //Oracle Client 
		printf("We are connected!\n"); 

		// Disconnect is optional 
		// autodisconnect will occur in destructor if needed 
		printf("We are disconnected!\n"); 

	catch(SAException & x) 
		// SAConnection::Rollback() 
		// can also throw an exception 
		// (if a network error for example), 
		// we will be ready 
			// on error rollback changes 
			con.Rollback (); 
		catch(SAException &) 
		// print error message 
		printf("%s\n", (const char*)x.ErrText()); 
	return 0; 
Updated 1-Jun-20 13:37pm
Afzaal Ahmad Zeeshan 1-Jun-20 17:06pm
And the problem is?

Your OP has the title for C, but tag and program in C++. Also, check out the Microsoft documentation to learn how to do this using C++:

1 solution

It depends on 'what breed/type' of database you want to connect to - for example, for Oracle, you need to install OCI Version 8 (at least), then as you code suggests, use SA_Oracle_Client in the connection. SqlAPI then will use the correct Oracle driver. See here[^] for other database breeds.

As Afzaal Ahmad Zeeshan asked you, you dont really state what the problem is, so your question is a bit broad for a finer level of detail answer - you can use the green 'Improve question' widget to edit the question and supply more information

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900