Hi,
I'm using the SQLconnect function in order to connect to my SQL server
I want to connect to a database called "CookieJar" without user name and password
this is the code:
#include "Container.h"
#include <windows.h>
#include <sqlext.h>
int main()
{
HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
HDBC hDBC = NULL; // Connection handle
HSTMT hStmt = NULL;// Statement handle
UCHAR szDSN[1024] = "CookieJar";// Data Source Name buffer
UCHAR szUID[10] = "";// User ID buffer
UCHAR szPasswd[10] = "";// Password buffer
UCHAR szModel[128];// Model buffer
SDWORD cbModel;// Model buffer bytes recieved
char buff[9] = "Testing";
UCHAR szSqlStr[128]= "INSERT into (Tablename) (ColumnName) Values ('Testing')" ;
RETCODE retcode;
//sprintf((char*)szSqlStr,"INSERT into (Tablename)(Columname) Values ('%s')",buff);
// Allocate memory for ODBC Environment handle
SQLAllocEnv (&hEnv);
// Allocate memory for the connection handle
SQLAllocConnect (hEnv, &hDBC);
// Connect to the data source "test" using userid and password.
retcode = SQLConnect (hDBC, (SQLWCHAR*)szDSN, SQL_NTS,/* (SQLWCHAR*)szUID*/ NULL, SQL_NTS, /*(SQLWCHAR*)szPasswd*/ NULL, SQL_NTS);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
// Allocate memory for the statement handle
retcode = SQLAllocStmt (hDBC, &hStmt);
// Prepare the SQL statement by assigning it to the statement handle
retcode = SQLPrepare (hStmt, (SQLWCHAR*)szSqlStr, sizeof (szSqlStr));
// Execute the SQL statement handle
retcode = SQLExecute (hStmt);
// Project only column 1 which is the models
SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel);
// Get row of data from the result set defined above in the statement
retcode = SQLFetch (hStmt);
// Free the allocated statement handle
SQLFreeStmt (hStmt, SQL_DROP);
// Disconnect from datasource
SQLDisconnect (hDBC);
}
// Free the allocated connection handle
SQLFreeConnect (hDBC);
// Free the allocated ODBC environment handle
SQLFreeEnv (hEnv);
return 0;
}
The program works but it doesn't get into the "if" section...
And what do I need to put in szDSN - just the database name or the whole connection string?
please help,
Thanks,
Eli
Hi Eli,
http://msdn2.microsoft.com/en-us/library/ms711810.aspx
note that the string you are using for database is actually the server name.
If you want to specify the database, call SQLSetConnectAttr() with SQL_ATTR_CURRENT_CATALOG.
Still not sure why you are sending NULL username and password. You may want to use 0 instead of SQL_NTS there, with NULL strings -- not sure if we ignore that or not.
Hope that helps,
John
No comments:
Post a Comment