C++ 参数化查询使用C++。Net MySQL连接器
我自己似乎找不到解决这个问题的办法。我使用此通用函数从数据库中检索数据,如下所示:C++ 参数化查询使用C++。Net MySQL连接器,c++,mysql,parameterized-query,C++,Mysql,Parameterized Query,我自己似乎找不到解决这个问题的办法。我使用此通用函数从数据库中检索数据,如下所示: int id=29 ArrayList^classes=getClassesGeneric(“dep_id”,“=”,id.ToString()) 但是,这不会返回任何结果。如果我通过MySQL Workbench或不使用参数来查询数据库,它就可以正常工作。我错过了什么 ArrayList^ Accessor::getClassesGeneric(String^ col, String^ op, Str
int id=29
ArrayList^classes=getClassesGeneric(“dep_id”,“=”,id.ToString())代码>
但是,这不会返回任何结果。如果我通过MySQL Workbench或不使用参数来查询数据库,它就可以正常工作。我错过了什么
ArrayList^ Accessor::getClassesGeneric(String^ col, String^ op, String^ value)
{
ArrayList^ result = gcnew ArrayList();
this->cmd = gcnew MySqlCommand("SELECT * FROM rpos_db.classes WHERE @col @op @value;", this->con);
try
{
this->cmd->Parameters->AddWithValue("@col", col);
this->cmd->Parameters->AddWithValue("@op", op);
this->cmd->Parameters->AddWithValue("@value", value);
this->cmd->Prepare();
MySqlDataReader^ r = this->cmd->ExecuteReader();
while (r->Read())
{
Class^ c = gcnew Class();
c->id = r->GetInt32(0);
c->dep_id = r->GetInt32(1);
c->name = r->GetString(2);
c->code = r->GetString(3);
result->Add(c);
}
r->Close();
}
catch (Exception^ ex)
{
MessageBox::Show(ex->StackTrace, ex->Message);
}
return result;
}
使用这样的函数会产生缩进结果:
classes=getClassesGeneric(“1”、“1”)代码>参数只能用于替换文字,而不能替换对象名称或语法元素,例如=
运算符。你要么硬编码。如果要动态传递它们,则必须使用字符串操纵:
ArrayList^ Accessor::getClassesGeneric(String^ col, String^ op, String^ value)
{
ArrayList^ result = gcnew ArrayList();
this->cmd = gcnew MySqlCommand
("SELECT * FROM rpos_db.classes WHERE " +
col + " " + op + " @value;", this->con);
try
{
this->cmd->Parameters->AddWithValue("@value", value);
this->cmd->Prepare();
MySqlDataReader^ r = this->cmd->ExecuteReader();
参数只能用于替换文字,而不能替换对象名称或语法元素,例如=
运算符。你要么硬编码。如果要动态传递它们,则必须使用字符串操纵:
ArrayList^ Accessor::getClassesGeneric(String^ col, String^ op, String^ value)
{
ArrayList^ result = gcnew ArrayList();
this->cmd = gcnew MySqlCommand
("SELECT * FROM rpos_db.classes WHERE " +
col + " " + op + " @value;", this->con);
try
{
this->cmd->Parameters->AddWithValue("@value", value);
this->cmd->Prepare();
MySqlDataReader^ r = this->cmd->ExecuteReader();
我知道你在说什么,但是用这种方式怎么会好呢<代码>getClassesGeneric(“1”,“=”,“1”)代码>我知道你在说什么,但是这样使用它怎么会很好呢<代码>getClassesGeneric(“1”,“=”,“1”)代码>