Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing sql server from c code

Status
Not open for further replies.

amasny

Programmer
Oct 6, 2003
20
PL
Hello,
I'm writing C code - win32 project in VC++.Net environment.
I would like to access sqlserver database for inserting data
to this database. I have no clue how to do this. Give me some help please. Which libraries should i include and how.
Appreciate any help Arek Masny
 
Well it depends on what type of SQL server. For Microsoft SQL Server or Access databases, you should use ODBC. For the MySQL server, use the libraries distributed with it. Other popular databases will probably provide their own client libraries.

I REALLY hope that helps.
Will
 

search the web. there are lots of ado or odbc examples out there.

there will also be examples that are shipped with sql server.
 
I put here a piece of code to give you a hint.
You should link with odbc32.lib.
Code:
bool bIsSQLServerAvailable()
{
	HENV    henv;
	HDBC    hdbc;
	RETCODE retcode;
	BOOL	bResult = FALSE;

	retcode = SQLAllocEnv(&henv);              /* Environment handle */
	if (retcode == SQL_SUCCESS) 
	{
		retcode = SQLAllocConnect(henv, &hdbc); /* Connection handle */
		if (retcode == SQL_SUCCESS) 
		{
			/* Set login timeout to 5 seconds. */
			SQLSetConnectOption(hdbc, SQL_LOGIN_TIMEOUT, 5);

			/* Connect to data source */
			retcode = SQLConnect(hdbc, (unsigned char *)m_csDSN), SQL_NTS, 
				(unsigned char *)m_sSQLServerUID, SQL_NTS, 
				(unsigned char *)m_sSQLServerPWD, SQL_NTS);

			if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
			{
				bResult = TRUE;
			}
			SQLFreeConnect(hdbc);
			ASSERT ( retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO);
		}
		SQLFreeEnv(henv);
		ASSERT ( retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO);
	}
	return bResult;
}

UINT CCMCashDrawer::GetSearchResultFromBackup( LPVOID lpvd)
{
//#define LEN	50
	char *DSN = m_sSQLServerDSN;
	char *UID = m_sSQLServerUID;
	char *PWD = m_sSQLServerPWD;
	

	


	HENV    henv;
	HDBC    hdbc;
	HSTMT   hstmt;
	RETCODE retcode;
	char *csSQLstmt = "select CUSTACCTNO, BLGNUMBER, BLGNUMTYPE, CUSTACCTSTAT, CNALINE1, CNALINE2, RUNGBALDUEAMT  from _table ";
	
	retcode = SQLAllocEnv(&henv);              /* Environment handle */
	if (retcode == SQL_SUCCESS) 
	{
		retcode = SQLAllocConnect(henv, &hdbc); /* Connection handle */
		if (retcode == SQL_SUCCESS) 
		{
			/* Set login timeout to 5 seconds. */
			SQLSetConnectOption(hdbc, SQL_LOGIN_TIMEOUT, 5);

			/* Connect to data source */
			retcode = SQLConnect(hdbc, 
								(unsigned char *)m_csDSN),	SQL_NTS, 
								 (UCHAR FAR *)UID, SQL_NTS, 
								(UCHAR FAR *)PWD, SQL_NTS);
			
			if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
			{
				/* Process data after successful connection */
				retcode = SQLAllocStmt(hdbc, &hstmt); /* Statement handle */
				if (retcode == SQL_SUCCESS) 
				{
					retcode = SQLExecDirect(hstmt, (unsigned char *)(csSQLstmt), SQL_NTS);

					if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) 
					{
						while (TRUE) 
						{
							retcode = SQLFetch(hstmt);
							if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
							{	
								char [128] cBN;
								SDWORD nLen;
								static SQLCHAR szCAN[LEN_CAN], szBN[LEN_BN], szBNT[LEN_BILLING_NO_TYPE], szSTAT[LEN_CUST_ACCOUNT_STAT], szCNA1[LEN_CM_CNALINE1], szCNA2[LEN_CM_CNALINE2];
								static SQLCHAR szBalance[LEN_RUNNING_BALANCE_DUE];
								
								SQLGetData(hstmt, 1, SQL_C_CHAR, szCAN,		LEN_CAN + 1,				&nLen);
								SQLGetData(hstmt, 2, SQL_C_CHAR, szBN,		LEN_BN + 1,					&nLen);
								strcpy(cBN,szBN);
								//cBN.TrimRight();
								SQLGetData(hstmt, 3, SQL_C_CHAR, szBNT,		LEN_BILLING_NO_TYPE + 1,	&nLen);
								SQLGetData(hstmt, 4, SQL_C_CHAR, szSTAT,	LEN_CUST_ACCOUNT_STAT + 1,	&nLen);
								SQLGetData(hstmt, 5, SQL_C_CHAR, szCNA1,	LEN_CM_CNALINE1 + 1,		&nLen);
								SQLGetData(hstmt, 6, SQL_C_CHAR, szCNA2,	LEN_CM_CNALINE2 + 1,		&nLen);
								SQLGetData(hstmt, 7, SQL_C_CHAR, szBalance,	LEN_RUNNING_BALANCE_DUE + 1,&nLen);	

								CCMCustomer* pNewCCMCustomer = new CCMCustomer;
								// copy tran data to new Customer object
						
								pNewCCMCustomer->SetCAN(szCAN);
								pNewCCMCustomer->SetBillingNo(cBN);
								pNewCCMCustomer->SetBillingNoType(szBNT);
								pNewCCMCustomer->SetAccountStat(szSTAT);	
								pNewCCMCustomer->SetCNALine1(szCNA1);
								pNewCCMCustomer->SetCNALine2(szCNA2);
								strcpy( sBalance, szBalance);
								CAppCurrency cy = CAppCurrency(_atoi64(sBalance.Left(LEN_RUNNING_BALANCE_DUE-3)),atol(sBalance.Mid(13,3))*10 );
								cy.Round(2);
								pNewCCMCustomer->SetRunningBalanceDue(cy);
								// end copy
								AddTail(pNewCCMCustomer);
							} 
							else 
							{
								break;
							}
						}
					}
					retcode = SQLFreeStmt(hstmt, SQL_DROP);
				}
				SQLDisconnect(hdbc);
			}
		 	SQLFreeConnect(hdbc);
		}
		SQLFreeEnv(henv);
	}

	if (retcode != SQL_SUCCESS {
		LOGEVENT(EVENTLOG_ERROR_TYPE, 
				" GetSearchResultFromBackup - FAILED" 
				);
	}

	return 0;
}
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top