从MySQL数据库检索数据
因此,我试图从MySQL数据库中获取数据并将其显示在messageBox中,但我不知道如何获取我所做查询的结果,以下是我的代码:从MySQL数据库检索数据,mysql,database,visual-studio,c++-cli,Mysql,Database,Visual Studio,C++ Cli,因此,我试图从MySQL数据库中获取数据并将其显示在messageBox中,但我不知道如何获取我所做查询的结果,以下是我的代码: String^ constring = L"datasource=localhost; port=3306; username=root; password="; / MySqlConnection^ conDB = gcnew MySqlConnection(constring); MySqlCommand^ Query = gcnew MySqlCommand(
String^ constring = L"datasource=localhost; port=3306; username=root; password="; /
MySqlConnection^ conDB = gcnew MySqlConnection(constring);
MySqlCommand^ Query = gcnew MySqlCommand("SELECT Bar FROM utilizadores.users WHERE username='"+user+ "' ", conDB );
MySqlDataReader^ reader;
conDB->Open();
try
{
reader = Query->ExecuteReader();
MessageBox::Show(/*Result of the Query here*/);
}
catch (Exception^ex)
{
MessageBox::Show(ex->Message);
}
要显示该查询的结果,我必须在MessageBox中放入什么
谢谢这应该可以:
reader = Query->ExecuteReader();
String bar = "Bar not found for the user '" & user & "'"
if( reader.Read() ) ' hope the statement returns a single row, and hence `if`
bar = reader.GetString( 0 )
end if
reader.close()
MessageBox::Show( bar );
要读取数据,您通常会使用如下代码。这假定条形图为double类型。您可以相应地更改条形图的类型
条的位置\u value=reader->getDouble(1)表示结果在第一列中
因此,如果您检索了两列,并且想要第二列的值也是double类型,那么可以使用second\u column=reader->getDouble(2) 解决方案将类似于此
String^ constring = L"datasource=localhost; port=3306; username=root; password="; /
MySqlConnection^ conDB = gcnew MySqlConnection(constring);
MySqlCommand^ Query = gcnew MySqlCommand("SELECT Bar FROM utilizadores.users WHERE username='"+user+ "' ", conDB );
MySqlDataReader^ reader;
onDB->Open();
double^ bar_value;
String^ messagebox_bar_values;
try
{
reader = Query->ExecuteReader();
while (reader->Read())
{
bar_value=reader->GetDouble(1);
messagebox_bar_values=messagebox_bar_values+bar_value.ToString + ",";
//Alternatively the line below should also work. If we are not concerned about the datatype and read the value directly as a string.
//Just comment out the two lines above
//messagebox_bar_values=messagebox_bar_values+reader["Bar"]->ToString;
}
MessageBox::Show(messagebox_bar_values);
}
catch (Exception^ex)
{
MessageBox::Show(ex->Message);
}
messagebox可能不是显示这一点的最佳方式,尤其是如果您有大量数据,测试当然就足够了。文本框可能是更好的选择。谢谢,我明白你的意思。@sparrow。没有问题,很高兴能帮助。谢谢你的帮助,但是有些代码似乎是在VB.NET中代替C++ CLI,但是我理解你的意思,并且在把代码转换成C++ CLI之后能够成功地应用它。