将MySQL搜索查询复制到数据表中

时间:2017-04-04 03:45:41

标签: visual-c++ visual-studio-2015 datatable

我无法将DataTable中的数据导入DataGridView。我在Visual Basic中找到了代码,并尝试将其转换为Visual C ++。

        String^ cninfo = "server=localhost; port=3306; username=test; password=Lalala123; database=turismo";
        MySqlConnection^ cn = gcnew MySqlConnection;
        MySqlDataAdapter^ cmdadp;
        DataTable^ Table = gcnew DataTable;

        String^ search = searchtxt->Text;
        int^ rowposition = 0;
        String^ id_searchQuery = ("select * from clientes where CLIENT_ID = @search;");
        String^ name_searchQuery = ("select * from clientes where FIRST_NAME like '%@search%';");
        String^ lastname_searchQuery = ("select * from clientes where LAST_NAME like '%@search%';");
        String^ origin_searchQuery = ("select * from clientes where COUNTRY_ORIGIN like '%@search%';");

        if (radid /*Radbutton for id_searchQuery*/->Checked == true) {

            cn->ConnectionString = cninfo;
            cn->Open();

            //defining parameters for the search query
            MySqlCommand^ searchCmd = gcnew MySqlCommand(id_searchQuery, cn);
            searchCmd->CommandType = CommandType::Text;
            searchCmd->Parameters->AddWithValue("@search", search);

            //filling the DataTable with MySqlDataAdapter
            cmdadp->Fill(Table);

            //counting each DataTale line to enter it into a DataGridView with a while function
            while (rowposition < Table->Rows->Count) {

                DataRow^ MyDataRow = Table->Rows(rowposition);

            }
            cn->Close();

        }

(rowposition < Table->Rows->Count) {我得到“&lt;”突出显示,我得到了

  

无操作员“&lt;”匹配这些命令

DataRow^ MyDataRow = Table->Rows(rowposition);上,我突出显示“表格”,然后我

  

调用句柄类型的对象而没有适当的operator()或转换函数到指针到函数类型

1 个答案:

答案 0 :(得分:1)

如果你只是将rowposition声明为int而不是int ^?

会发生什么

表 - &gt;行是DataRowCollection类的类型。尝试使用Item属性数组表示法访问该行,例如Table-&gt; Rows [rowposition]

@Phil Brubaker回答