Guest User

FreeTDS

a guest
Feb 25th, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #ifdef WIN32
  4.   #include <windows.h>
  5. #endif
  6.  
  7. #include <sql.h>
  8. #include <sqlext.h>
  9. #include <sqltypes.h>
  10.  
  11. #include "unicode/ustdio.h"
  12. #include "unicode/ustring.h"
  13. #include "unicode/unistr.h"
  14. #include "unicode/gregocal.h"
  15. #include "unicode/normlzr.h"
  16.  
  17. using namespace std;
  18.  
  19. int main (int argc, char* argv[])
  20. {
  21.   SQLHSTMT hSQLStatement = 0;
  22.   SQLHENV hSQLEnvironment = 0;
  23.   SQLHDBC hSQLODBC = 0;
  24.  
  25.   SQLRETURN sqlRet = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hSQLEnvironment);
  26.  
  27.   if(SQL_SUCCEEDED(sqlRet))
  28.   {
  29.     sqlRet = SQLSetEnvAttr(hSQLEnvironment,
  30.                            SQL_ATTR_ODBC_VERSION,
  31.                            (void*)SQL_OV_ODBC3,
  32.                            0);
  33.  
  34.     if(SQL_SUCCEEDED(sqlRet))
  35.     {
  36.         sqlRet = SQLAllocHandle(SQL_HANDLE_DBC,
  37.                                 hSQLEnvironment,
  38.                                 &hSQLODBC);
  39.     }
  40.     else
  41.     {
  42.       cout << "Error in SQLAllocHandle for SQL_HANDLE_DBC" << endl;
  43.     }
  44.   }
  45.   else
  46.   {
  47.     cout << "Error in SQLAllocHandle for SQL_HANDLE_ENV" << endl;
  48.   }
  49.  
  50.   UnicodeString DSNName = "TestDSN";
  51.   UnicodeString UserName = "sa";
  52.   UnicodeString Password = "Admin@123";
  53.  
  54.   UnicodeString Value = "";
  55.  
  56.   UChar32 character = 20053;
  57.   Value.append(character);
  58.  
  59.   character = 20077;
  60.   Value.append(character);
  61.  
  62.   character = 131140;
  63.   Value.append(character);
  64.  
  65.   character = 131145;
  66.   Value.append(character);
  67.  
  68.   character = 20090;
  69.   Value.append(character);
  70.  
  71.   character = 19989;
  72.   Value.append(character);
  73.  
  74.   UnicodeString SQLStatement = "INSERT INTO mytable (sample) VALUES(N";
  75.   SQLStatement.append("'");
  76.   SQLStatement.append(Value);
  77.   SQLStatement.append("'");
  78.   SQLStatement.append(")");
  79.  
  80.   if(0 != hSQLODBC)
  81.   {
  82.     SQLRETURN sqlRet = SQLConnectW(hSQLODBC,
  83.                                    (SQLWCHAR*)DSNName.getTerminatedBuffer(),
  84.                                    SQL_NTS,
  85.                                    (SQLWCHAR*)UserName.getTerminatedBuffer(),
  86.                                    SQL_NTS,
  87.                                    (SQLWCHAR*)Password.getTerminatedBuffer(),
  88.                                    SQL_NTS);
  89.  
  90.     if(SQL_SUCCEEDED(sqlRet))
  91.     {
  92.       cout << "Connection to database successful" << endl;
  93.  
  94.       SQLRETURN sqlRet = SQLAllocHandle(SQL_HANDLE_STMT,
  95.                                         hSQLODBC,
  96.                                         &hSQLStatement);
  97.  
  98.       if(SQL_SUCCEEDED(sqlRet))
  99.       {
  100.         sqlRet = SQLExecDirectW(hSQLStatement,
  101.                                 (SQLWCHAR*)SQLStatement.getTerminatedBuffer(),
  102.                                 SQL_NTS);
  103.  
  104.         if(SQL_SUCCEEDED(sqlRet))
  105.         {
  106.           cout << "Query Execution successful" << endl;
  107.         }
  108.         else
  109.           cout << "Query Execution failed" << endl;
  110.       }
  111.     }
  112.     else
  113.     {
  114.       cout << "Connection to database failed" << endl;
  115.     }
  116.   }
  117.  
  118.   return 0;
  119. }
Add Comment
Please, Sign In to add comment