META NAME="Author" CONTENT="">
C++ My Sql Veritabanındaki Verileri listView Nesnesinde Gösterimini Sağlamak ve MySQL Veritabanında Arama Yapmak
Merhaba arkadaşlar bu makalemizde C++ da listView nesnesinde My Sql veritabanındaki verilerin gösterimini sağlayacağız. Ayrıca yazar sütununa göre listView nesnesinde arama yapacağız.
C++ da Windows Form nasıl ekleriz? Konusunu daha önceki makalede anlatmıştım. Önceki makaleye ulaşmak için Buraya tıklayabilirsiniz.
İlk önce Manage Nuget Packages browser kısmından libmysql i kurunuz. Daha sonra References kısmına sağ tıklayalım. Add Referencesi seçip tıklayalım. Açılan Add references penceresnde MySql.Data yı seçip, OK butonuna tıklayalım.
Biz bu örneğimizde MySql de book şemasındaki worldclassics tablosuna bağlanacağız. (Schema:book)
Şekil 1
Şekil 2
MyForm.h
#pragma once
namespace cpplistviewsearch {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace MySql::Data::MySqlClient;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::ListView^ listView1;
private: System::Windows::Forms::TextBox^ txtSearch;
private: System::Windows::Forms::Button^ txtBtn;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->listView1 = (gcnew System::Windows::Forms::ListView());
this->txtSearch = (gcnew System::Windows::Forms::TextBox());
this->txtBtn = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// listView1
//
this->listView1->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->listView1->HideSelection = false;
this->listView1->Location = System::Drawing::Point(1, 65);
this->listView1->Name = L"listView1";
this->listView1->Size = System::Drawing::Size(675, 521);
this->listView1->TabIndex = 0;
this->listView1->UseCompatibleStateImageBehavior = false;
this->listView1->View = System::Windows::Forms::View::Details;
//
// txtSearch
//
this->txtSearch->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->txtSearch->Location = System::Drawing::Point(27, 17);
this->txtSearch->Name = L"txtSearch";
this->txtSearch->Size = System::Drawing::Size(187, 27);
this->txtSearch->TabIndex = 1;
//
// txtBtn
//
this->txtBtn->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->txtBtn->Location = System::Drawing::Point(231, 10);
this->txtBtn->Name = L"txtBtn";
this->txtBtn->Size = System::Drawing::Size(159, 42);
this->txtBtn->TabIndex = 2;
this->txtBtn->Text = L"Search";
this->txtBtn->UseVisualStyleBackColor = true;
this->txtBtn->Click += gcnew System::EventHandler(this, &MyForm::txtBtn_Click);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(676, 588);
this->Controls->Add(this->txtBtn);
this->Controls->Add(this->txtSearch);
this->Controls->Add(this->listView1);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e)
{
String^ str = L"datasource=localhost;port=3306;username=root;password=1234";
listView1->GridLines = true;
listView1->Columns->Add("Id", 40);
listView1->Columns->Add("Author", 250);
listView1->Columns->Add("Book", 300);
listView1->Columns->Add("Price", 80);
String^ sql = "Select * From book.worldclassics";
MySqlConnection^ con = gcnew MySqlConnection();
con->ConnectionString = str;
con->Open();
MySqlCommand^ cmd = gcnew MySqlCommand(sql, con);
MySqlDataReader^ dr = cmd->ExecuteReader();
ListViewItem^ item = gcnew ListViewItem();
while (dr->Read())
for (int i = 1; i <= 1; i++)
{
item = listView1->Items->Add(dr->GetInt32(0).ToString());
item->SubItems->Add(dr->GetString(1));
item->SubItems->Add(dr->GetString(2));
item->SubItems->Add(dr->GetString(3));
//item->BackColor = System::Drawing::Color::LightSkyBlue;
//item->ForeColor = System::Drawing::Color::WhiteSmoke;
item->BackColor = item->Index % 2 == 0 ? System::Drawing::Color::LightCyan: System::Drawing::Color::White;
item->ForeColor = item->Index % 2 == 0 ? System::Drawing::Color::Red : System::Drawing::Color::Navy;
}
con->Close();
}
private: System::Void txtBtn_Click(System::Object^ sender, System::EventArgs^ e)
{
listView1->Items->Clear();
String^ str = "Select * From book.worldclassics Where Author='" + txtSearch->Text + "' ";
MySqlConnection^ con = gcnew MySqlConnection(L"datasource=localhost;port=3306;username=root;password=2344;Integrated Security=true");
MySqlCommand^ cmd = gcnew MySqlCommand(str, con);
con->Open();
DataSet^ ds = gcnew DataSet(L"ds");
MySqlDataAdapter^ da = gcnew MySqlDataAdapter();
da->SelectCommand = cmd;
da->Fill(ds);
MySqlDataReader^ dr = cmd->ExecuteReader();
ListViewItem^ item = gcnew ListViewItem();
while (dr->Read())
for (int i = 1; i <= 1; i++)
{
item = listView1->Items->Add(dr->GetInt32(0).ToString());
item->SubItems->Add(dr->GetString(1));
item->SubItems->Add(dr->GetString(2));
item->SubItems->Add(dr->GetString(3));
//item->BackColor = System::Drawing::Color::LightSkyBlue;
//item->ForeColor = System::Drawing::Color::WhiteSmoke;
item->BackColor = item->Index % 2 == 0 ? System::Drawing::Color::LightCyan : System::Drawing::Color::White;
item->ForeColor = item->Index % 2 == 0 ? System::Drawing::Color::Red : System::Drawing::Color::Navy;
}
con->Close();
}
};
}
MyForm.cpp
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
cpplistviewsearch::MyForm form;
Application::Run(% form);
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN