Click here to Skip to main content
15,883,901 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

thanks

What I have tried:

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

int main(int argc, char* argv[]) 
{ 
	// create connection object to connect to database 
	SAConnection con; 
	try
	{ 
		// 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 
		con.Disconnect(); 
		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 
		try
		{ 
			// on error rollback changes 
			con.Rollback (); 
		} 
		catch(SAException &) 
		{ 
		} 
		// print error message 
		printf("%s\n", (const char*)x.ErrText()); 
	} 
	return 0; 
}
Posted
Updated 1-Jun-20 12:37pm
v2
Comments
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++: https://docs.microsoft.com/en-us/sql/connect/odbc/cpp-code-example-app-connect-access-sql-db?view=sql-server-ver15

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 https://www.sqlapi.com/ApiDoc/servers/[^] 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
 
Share this answer
 

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