MySqlData :: MySqlClient结果集可填充通用向量c ++

时间:2019-02-26 08:29:30

标签: mysql winforms c++-cli

我想要以某种方式从结果集中读取数据,创建预定义的模型并填充向量,稍后将使用它们填充dataGrid。

我似乎找不到的是如何使用MySqlData的结果集,也许还有另一种方法?为以后的模型和向量创建提取sql数据。


此刻,我将sql直接读入dataGrid并从那里创建模型和向量,但是我知道这是一种很奇怪的方法...

String^ conString = L"datasource=localhost;port=****;username=****;password=****";
    conDatabase = gcnew MySqlConnection(conString);

    String^ queryString = "SELECT film_id,title,release_year,length,description FROM sakila.film";

    adapter = gcnew MySqlDataAdapter(queryString, conDatabase);
    filmsTable = gcnew DataTable();
    adapter->Fill(filmsTable);

    source = gcnew BindingSource();
    source->DataSource = filmsTable;

    filmDataGrid->DataSource = filmsTable;

    filmDataGrid->Columns[0]->Visible = false;


    for (Int32 i = 0; i < filmDataGrid->RowCount - 1; i++) {

        String^ title;
        String^ releaseYear;
        String^ description;
        String^ length;

        title = filmDataGrid->Rows[i]->Cells[1]->Value->ToString();
        releaseYear = filmDataGrid->Rows[i]->Cells[2]->Value->ToString();
        length = filmDataGrid->Rows[i]->Cells[3]->Value->ToString();
        description = filmDataGrid->Rows[i]->Cells[4]->Value->ToString();


        std::string unmanagedTitle = msclr::interop::marshal_as<std::string>(title);
        std::string unmanagedRelYear= msclr::interop::marshal_as<std::string>(releaseYear);
        std::string unmanagedLength = msclr::interop::marshal_as<std::string>(length);
        std::string unmanagedDescription = msclr::interop::marshal_as<std::string>(description);

        Film *film = new Film(unmanagedTitle, unmanagedRelYear, unmanagedLength, unmanagedDescription);
        filmVector->push_back(*film);


    }

0 个答案:

没有答案