因此,我试图从MySQL数据库中获取数据并将其显示在messageBox中,但我不知道如何获得我所做的查询的结果,下面是我的代码:
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中放什么?
谢谢
发布于 2014-04-12 20:04:47
要读取数据,通常需要使用如下代码。这假定Bar是类型为double。您可以相应地更改Bar的类型。
bar_value=reader->getDouble(1)的位置表示结果在第一个列中。
因此,如果检索到两列并希望第二列的值也是类型为double,则可以使用second_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);
}
如果您有大量的数据,那么消息盒可能不是最好的显示方式--当然,这就足够了。文本框可能是更好的选择。
发布于 2014-04-12 19:52:14
这应该是可行的:
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 );
https://stackoverflow.com/questions/23038677
复制相似问题