Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SQLserver2005 connectivty with VC (code)
- #include "stdafx.h"
- #import "C:Program FilesCommon FilesSystemADOmsado15.dll"
- no_namespace rename("EOF", "EndOfFile")
- int main(int argc, char* argv[])
- {
- /*The following variables will be initialized with necessary values and appended to the strSQL values*/
- _bstr_t strName;
- _bstr_t strAge;
- _bstr_t strDOB;
- _bstr_t strSalary;
- _ConnectionPtr pConn = NULL;
- // Define string variables for ADO connection
- _bstr_t strCon("Provider=SQLOLEDB.1;Persist Security Info=False;Username=keerth;Password=keerth;Initial Catalog=keerth516;Data Source=(local);Integrated Security=SSPI;");
- HRESULT hr = S_OK;
- //Initialize the COM Library
- CoInitialize(NULL);
- try
- {
- //Create the Connection pointer
- hr = pConn.CreateInstance((__uuidof(Connection)));
- if(FAILED(hr))
- {
- printf("Error instantiating Connection objectn");
- goto cleanup;
- }
- //Open the SQL Server connection
- hr = pConn->Open(strCon,"keerth","keerth",0);
- if(FAILED(hr))
- {
- printf("Error Opening Database object using ADO _ConnectionPtr n");
- goto cleanup;
- }
- /*Initialize the values */
- strName = "'C++ ADO insert Sample',";
- strAge = "23,";
- strDOB = "'13/04/1988',";
- strSalary = "16600.10)";
- /* Append the values to the Insert Statement */
- _bstr_t strSQL("Insert into Table1(NAME,AGE,DOB,SALARY) Values(");
- strSQL += strName + strAge + strDOB + strSalary ;
- printf("%sn",(LPCSTR)strSQL);
- //Execute the insert statement
- pConn->Execute(strSQL,NULL,adExecuteNoRecords);
- printf("Data Added Successfullyn",(LPCSTR)strSQL);
- //Close the database
- pConn->Close();
- }
- catch(_com_error &ce)
- {
- //printf("Error:%sn",ce.ErrorInfo);
- printf("Error:%sn,",(char*)ce.Description());
- }
- cleanup:
- CoUninitialize();
- return 0;
- }
Add Comment
Please, Sign In to add comment