Below is the insert function in VC++.
I am getting this error when I change the char to string data type to read the value of amount variable in the below code.
static void Insert(t_analysis* analysis)
{
_bstr_t strunitId;
_bstr_t strGdt=time(0);
_bstr_t strvalue;
std::string str;
std::string commandStr = "insert into table1(unitid,g_time_dte_1,h_1,n_1,ch_1,co_1,im_1,ve_1,er_1) Values(123,'" + strGdt +"',";
char tempBuf[50];
for (int j = 0; j < analysis->ubPeaksIntTab;j++ )
{
sprintf(tempBuf, "%d", (analysis->peak + j)->amount);//here it takes the adrress of amount but not the value of amount variable.
str += commandStr + tempBuf;
if(j!=analysis->ubPeaksIntTab-1)
commandStr += ",";
}
commandStr += ")";
_ConnectionPtr pConn = NULL;
try
{
HRESULT hr = S_OK;
CoInitialize(NULL);
hr = pConn.CreateInstance((__uuidof(Connection)));
_bstr_t strCon("Provider=SQLOLEDB;Dataq Source=MYPC\\SQLEXPRESS;Initial Catalog=keerth;User ID=sa;Password=password;Connect Timeout=30;");
if(FAILED(hr))
{
printf("Error instantiating Connection object\n");
}
hr = pConn->Open(strCon,"sa","password",0);
if(FAILED(hr))
{
printf("Error Opening Database object using ADO _ConnectionPtr \n");
}
//Execute the insert statement
pConn->Execute(commandStr.c_str(), NULL,adExecuteNoRecords);
pConn->Close();
}
catch(_com_error &ce)
{
printf("Error:%s\n",ce.ErrorMessage());
pConn->Close();
}
}
Whenever I run this getting the Error. Then I changed the char tempbuf[50]; to std::string str1;.
Now it is showing:
Error C2664: 'sprintf' : cannot convert parameter 1 from 'std::string' to 'char *;
The amount variable contains the float value.
How can I copy the float value assign it to string variable?
You are mixing C++ with C standard library functions.
You should use the C++ primitives. See StringStreams
EDIT:
If you want the same logic in C: The type std::string is not a C type, the standard string type of C is
char *andconst char *for immutable strings.The functions that you want to look at are then:
strncat(concatenate strings) and the safersnprintf.