数据库:access
struct Q
{
unsigned sn[8];
}PP;
cc(PP *p){
//使用托管
OleDbConnection ^myConnection = gcnew OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\data\\test.mdb");
try
{
myConnection->Open();
if (myConnection->State == ConnectionState::Open)
{
Console::WriteLine("Connection opened successfully");
OleDbCommand ^oleCmd = gcnew OleDbCommand
("insert into pub(iResult,aucSn) values(1,'asd')", myConnection);
oleCmd->ExecuteNonQuery();
}
else
Console::WriteLine("Connection could not be established");
}
catch(Exception ^ex)
{
Console::Write(ex->Message);
}
__finally
{
myConnection->Close();
}
}
int main()
{
PP s = {"1@-ad8"};
PP *pt = &s;
cc(pt);
return 0;
}
但是
OleDbCommand ^oleCmd = gcnew OleDbCommand
("insert into pub(iResult,aucSn) values(1,'asd')", myConnection);
改成
("insert into pub(iResult,aucSn) values(1,'"+p->sn+"')", myConnection);
就不可以了。
报错信息:错误 1 error C2679: 二进制“+”: 没有找到接受“unsigned char *”类型的右操作数的运算符(或没有可接受的转换)
------解决方案--------------------------------------------------------
前面两句只是为了演示方便,随便写的
关键是第三句。
String^ a2 = Marshal::PtrToStringAnsi((IntPtr) (char *) a);