VC++中字符串对数据库的修改
long CDepInfo::SqlInsert()
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置INSERT语句
CString strUID;
strUID.Format("%d", UID);
_bstr_t vSQL;
vSQL = "INSERT INTO DepInfo (DepName, Describes, UID) VALUES('"
+ DepName + "','" + Describes + "'," + strUID + ")";
/*这一段,为什么有'" + Describes + "'? ''里面的" + Describes + "是什么意思,然后我再修改添加个DepId变成
vSQL = "INSERT INTO DepInfo (DepId,DepName, Describes, UID) VALUES(" + DepId + ",'"
+ DepName + "','" + Describes + "'," + strUID + ")";
便以显示:error C2110: cannot add two pointers,为什么会出现这种情况,该怎么解决?*/
//执行INSERT语句
m_AdoConn.ExecuteSQL(vSQL);
//读取最大编号
long lDepId;
vSQL = "SELECT MAX(DepId) AS MaxId FROM DepInfo";
//执行SELETE语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
//返回各列的值
if (m_pRecordset->adoEOF)
lDepId = 0;
else
lDepId = atol((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("MaxId"));
//断开与数据库的连接
m_AdoConn.ExitConnect();
return lDepId;
}
Visual?C++ cstring 数据库
[解决办法]
这属于SQL的动态生成,需要调试最终生成的SQL插入语句是否能够在MSSQL中得到运行,
你可以把生成的SQL语句打印出来,如果执行出错就要修改程序。。
在动态组合SQL提醒一点,两个''''相当于一个单引号。
你这句话看上去引号少了一个。。。
vSQL = "INSERT INTO DepInfo (DepId,DepName, Describes, UID) VALUES(''" + DepId + ",''"
+ DepName + "'',''" + Describes + "'','" + strUID + ")";