以下是VC++中的插入函数。 我得到这个错误,当我改变字符到字符串数据类型来读取下面的代码中的变量的值。错误C2664:'sprintf':无法将参数1从'std :: string'转换为'char *'
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();
}
}
每当我运行这个获取错误。然后我将char tempbuf[50];
更改为std::string str1;
。
现在它表示:
Error C2664: 'sprintf' : cannot convert parameter 1 from 'std::string' to 'char *;
量变量包含浮点值。 如何复制浮点值将其赋值给字符串变量?
为什么c标签? '::'是C,'string'和'try'中的语法错误,是普通标识符! C标签被删除。 – pmg 2011-12-27 11:22:15