【发布时间】:2011-12-27 10:14:53
【问题描述】:
下面是VC++中的插入函数。 当我将 char 更改为 string 数据类型以读取以下代码中的 amount 变量的值时,出现此错误。
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 *;
amount 变量包含浮点值。 如何复制浮点值将其分配给字符串变量?
【问题讨论】:
-
为什么是c标签?
::是 C 中的语法错误,string和try是普通标识符!删除了 C 标记。
标签: visual-c++