Guest User

Untitled

a guest
Aug 10th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. SQLserver2005 connectivty with VC (code)
  2. #include "stdafx.h"
  3.  
  4. #import "C:Program FilesCommon FilesSystemADOmsado15.dll"
  5. no_namespace rename("EOF", "EndOfFile")
  6.  
  7. int main(int argc, char* argv[])
  8. {
  9.  
  10. /*The following variables will be initialized with necessary values and appended to the strSQL values*/
  11. _bstr_t strName;
  12. _bstr_t strAge;
  13. _bstr_t strDOB;
  14. _bstr_t strSalary;
  15.  
  16. _ConnectionPtr pConn = NULL;
  17. // Define string variables for ADO connection
  18. _bstr_t strCon("Provider=SQLOLEDB.1;Persist Security Info=False;Username=keerth;Password=keerth;Initial Catalog=keerth516;Data Source=(local);Integrated Security=SSPI;");
  19.  
  20. HRESULT hr = S_OK;
  21.  
  22. //Initialize the COM Library
  23. CoInitialize(NULL);
  24.  
  25. try
  26. {
  27. //Create the Connection pointer
  28. hr = pConn.CreateInstance((__uuidof(Connection)));
  29. if(FAILED(hr))
  30. {
  31. printf("Error instantiating Connection objectn");
  32. goto cleanup;
  33. }
  34.  
  35. //Open the SQL Server connection
  36. hr = pConn->Open(strCon,"keerth","keerth",0);
  37. if(FAILED(hr))
  38. {
  39. printf("Error Opening Database object using ADO _ConnectionPtr n");
  40. goto cleanup;
  41. }
  42.  
  43. /*Initialize the values */
  44. strName = "'C++ ADO insert Sample',";
  45. strAge = "23,";
  46. strDOB = "'13/04/1988',";
  47. strSalary = "16600.10)";
  48.  
  49. /* Append the values to the Insert Statement */
  50. _bstr_t strSQL("Insert into Table1(NAME,AGE,DOB,SALARY) Values(");
  51.  
  52. strSQL += strName + strAge + strDOB + strSalary ;
  53.  
  54. printf("%sn",(LPCSTR)strSQL);
  55.  
  56. //Execute the insert statement
  57. pConn->Execute(strSQL,NULL,adExecuteNoRecords);
  58.  
  59. printf("Data Added Successfullyn",(LPCSTR)strSQL);
  60.  
  61. //Close the database
  62. pConn->Close();
  63.  
  64. }
  65. catch(_com_error &ce)
  66. {
  67. //printf("Error:%sn",ce.ErrorInfo);
  68. printf("Error:%sn,",(char*)ce.Description());
  69. }
  70.  
  71.  
  72. cleanup:
  73. CoUninitialize();
  74.  
  75. return 0;
  76. }
Add Comment
Please, Sign In to add comment