C++ My Sql Veritabanindaki Verileri ListView Nesnesinde Gosterimini Saglamak
Merhaba arkada lar bu makalemizde C++ da ListView nesnesinde MySql veritaban ndaki verilerin gösterimini sa layaca 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 kismindan libmysql i kurunuz. Daha sonra References kismina sag tiklayalim. Add Referencesi secip tiklayalim. Acilan Add references penceresnde MySql.Data yi secip, OK butonuna tiklayalim.
Biz bu örne imizde MySql de book emas ndaki worldclassic tablosuna ba lanaca z. (Schema:book)
Formumuzu çal t rd m zda My Sql Veritaban m zdaki worldclassic tablomuz ListGridView nesnesinde a a daki gibi görülecektir.
ekil 1
MyForm.h
#pragma once
namespace cpplistview {
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;
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::ComponentModel::IContainer^ components;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
#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->SuspendLayout();
//
// listView1
//
this->listView1->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->listView1->HideSelection = false;
this->listView1->Location = System::Drawing::Point(0, 0);
this->listView1->Name = L"listView1";
this->listView1->Size = System::Drawing::Size(678, 501);
this->listView1->TabIndex = 0;
this->listView1->UseCompatibleStateImageBehavior = false;
this->listView1->View = System::Windows::Forms::View::Details;
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(680, 496);
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);
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e)
{
String^ str = L"datasource=localhost;port=3306;username=root;password=2344";
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::LightSkyBlue :System::Drawing::Color::White;
item->ForeColor = item->Index % 2 == 0 ? System::Drawing::Color::BlueViolet : 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);
cpplistview::MyForm form;
Application::Run(% form);
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede görü mek üzere. Bahad r AH N