-
December 22nd, 2011, 02:27 AM
#1
Error while executing the insert query.
Its an console window application.I am getting the error (unknown String)while running the below code.Kindly check the error and let me know where i have to change.
----------------------------------------------------------------------------------------------------------
Code:
#include "stdafx.h"
#include "objbase.h"
#include "icrsint.h"
#include <ole2.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
void Insert();
int main(int argc, char* argv[])
{
//HRESULT CoInitialize(NULL);
if ( FAILED(::CoInitialize(NULL)) )
return -1;
Insert();
::CoUninitialize();
getchar();
return 0;
}
void Insert()
{
/*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;Data Source=DEEPA\\SQLEXPRESS;Initial Catalog=keerth;User ID=sa;Password=tovya;Connect Timeout=30;");
HRESULT hr = S_OK;
//printf("connection string with all correct attributes");
//Initialize the COM Library
//CoInitialize(NULL);
try
{
//Create the Connection pointer
hr = pConn.CreateInstance((__uuidof(Connection)));
// printf("\n connection and create instance.");
if(FAILED(hr))
{
printf("Error instantiating Connection object\n");
goto cleanup;
}
//Open the SQL Server connection
hr = pConn->Open(strCon,"sa","tovya",0);
if(FAILED(hr))
{
printf("Error Opening Database object using ADO _ConnectionPtr \n");
goto cleanup;
}
/*Initialize the values */
strName = "'C++ADOinsert',";
strAge = "23,";
strDOB = "'13/04/1988 12:0:0',";
strSalary = "1660)";
/* Append the values to the Insert Statement */
//printf("\n connection with openString");
_bstr_t strSQL("Insert into table1(NAMES,AGE,DOB,SALARY) Values(");
strSQL += strName + strAge + strDOB + strSalary ;
printf("%s\n",(LPCSTR)strSQL);
//Execute the insert statement
pConn->Execute(strSQL,NULL,adExecuteNoRecords);
printf("Data Added Successfully\n",(LPCSTR)strSQL);
//Close the database
pConn->Close();
}
catch(_com_error &ce)
{
printf("Error:%s\n",ce.ErrorInfo());
pConn->Close();
}
cleanup:
CoUninitialize();
}
Last edited by ovidiucucu; December 22nd, 2011 at 04:47 AM.
Reason: Added [CODE] tags
-
December 22nd, 2011, 04:27 AM
#2
Re: Error while executing the insert query.
Originally Posted by keerth516
Its an console window application.I am getting the error (unknown String)while running the below code.
Please, provide the exact error message and the line where the error was detected.
Originally Posted by keerth516
... Kindly check the error and let me know where i have to change.
Sorry, but it is almost impossible since you didn't use Code tags.
Please, read Announcement: Before you post....
Victor Nijegorodov
-
December 22nd, 2011, 04:35 AM
#3
Re: Error while executing the insert query.
I guess the problem is with the date (DOB column), because the conversion, or lack of conversion, is a common problem with that kind of fields.
Could you try without the date?
Otherwise, enclose your lines of code between [code] and [/code] in your message. This will give a nice frame an indentations like
-
December 22nd, 2011, 04:53 AM
#4
Re: Error while executing the insert query.
[ shooting in the dark ] Make a UNICODE build, i.e. add UNICODE and _UNICODE to project preprocessor definitions.
Although, I don't know that can solve your problem, just have a try.
-
December 22nd, 2011, 05:07 AM
#5
Re: Error while executing the insert query.
Hi Ovidiu!
I don"t think UNICODE build can help here. I use ADO in ANSI builds since years (if not a decade) without any problem.
And, BTW, thank you for inserting Code tags into post of keerth516.
Dear keerth516,
the first thing you should do is forget that goto exists!
There are (or were) some languages where there was impossible to program without goto, but neither C nor C++ belog to those languages. Besides you "cleanup" code does not make any sense because the best place for CoUninitialize is in the main() function *the same module that is used for CoInitialize)
Second, did you debug you code? did you see what exactly your string strSQL contains while being passed in Execute method?
Victor Nijegorodov
-
December 22nd, 2011, 08:05 AM
#6
Re: Error while executing the insert query.
Hi Victor,
Indeed, should be not any problem ANSI vs. UNICODE build, as long as _bstr_t has botn char* and wchar_t* operators as well as constructors taking char* or wchar_t*, as well as...
I was just "shooting in the dark", like said before.
-
December 22nd, 2011, 11:49 PM
#7
Re: Error while executing the insert query.
Hi every one...thanks a lot for ur replies...
It was an date format issues..thats why was getting such error....now it insert the values into database.
-
December 22nd, 2011, 11:54 PM
#8
Re: Error while executing the insert query.
I have one more doubt reagrding insertion of values displayed in the console window.
I have one application which is in C lanaguage.Am using microsoft visual studio VC++ to debug this application.It will gives the finaly ouput display in console window.I want to insert this displayed values into sqlserver2005.
eg:Below line of code is the values displaying in console window.I want to insert the value of amout and area variable into sqlserver database.
Could u please suggest me any idea to implement this.If u provide any example it would be helpful.
printf("comp: %s ammount %f area %u\n",(analysis->peak + i)->comp,(analysis->peak + i)->amount , (analysis->peak + i)->area);
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|