C++ MS Sql Database’de DataBinding Olayı

 

Merhaba arkadaşlar bu makalemizde BindingNavigator ile Sql veritabanına bağlanacağız. BindingNavigator ile ileri, geri butonlara tıklayarak kayıtlar arasında gezineceğiz. Son olarak kayıtları TextBox’ta göstereceğiz.


Formumuza 1 adet Binding Navigator, 4 adet Label ve TextBox ekleyelim.

C++ da Windows Form nasıl ekleriz? Konusunu daha önceki makalede anlatmıştım. Önceki makaleye ulaşmak için Buraya tıklayabilirsiniz. 

 



 

 

 

 

 

 

 

 

 

 

 

 

Şekil 1


 



 

 












Şekil 2

 

MyForm.h

 

#pragma once

 

namespace cppdatabinding {

 

     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 System::Data::SqlClient;

     /// <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::TextBox^ txtId;

     private: System::Windows::Forms::TextBox^ txtFirstName;

     private: System::Windows::Forms::TextBox^ txtLastName;

     private: System::Windows::Forms::TextBox^ txtContact;

     protected:

 

     protected:

 

 

 

     private: System::Windows::Forms::Label^ label1;

     private: System::Windows::Forms::Label^ label2;

     private: System::Windows::Forms::Label^ label3;

     private: System::Windows::Forms::Label^ label4;

     private: System::Windows::Forms::BindingNavigator^ bindingNavigator1;

     private: System::Windows::Forms::ToolStripButton^ bindingNavigatorAddNewItem;

     private: System::Windows::Forms::ToolStripLabel^ bindingNavigatorCountItem;

     private: System::Windows::Forms::ToolStripButton^ bindingNavigatorDeleteItem;

     private: System::Windows::Forms::ToolStripButton^ bindingNavigatorMoveFirstItem;

     private: System::Windows::Forms::ToolStripButton^ bindingNavigatorMovePreviousItem;

     private: System::Windows::Forms::ToolStripSeparator^ bindingNavigatorSeparator;

     private: System::Windows::Forms::ToolStripTextBox^ bindingNavigatorPositionItem;

     private: System::Windows::Forms::ToolStripSeparator^ bindingNavigatorSeparator1;

     private: System::Windows::Forms::ToolStripButton^ bindingNavigatorMoveNextItem;

     private: System::Windows::Forms::ToolStripButton^ bindingNavigatorMoveLastItem;

     private: System::Windows::Forms::ToolStripSeparator^ bindingNavigatorSeparator2;

 

     private: System::ComponentModel::IContainer^ components;

 

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->components = (gcnew System::ComponentModel::Container());

                System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(MyForm::typeid));

                this->txtId = (gcnew System::Windows::Forms::TextBox());

                this->txtFirstName = (gcnew System::Windows::Forms::TextBox());

                this->txtLastName = (gcnew System::Windows::Forms::TextBox());

                this->txtContact = (gcnew System::Windows::Forms::TextBox());

                this->label1 = (gcnew System::Windows::Forms::Label());

                this->label2 = (gcnew System::Windows::Forms::Label());

                this->label3 = (gcnew System::Windows::Forms::Label());

                this->label4 = (gcnew System::Windows::Forms::Label());

                this->bindingNavigator1 = (gcnew System::Windows::Forms::BindingNavigator(this->components));

                this->bindingNavigatorAddNewItem = (gcnew System::Windows::Forms::ToolStripButton());

                this->bindingNavigatorCountItem = (gcnew System::Windows::Forms::ToolStripLabel());

                this->bindingNavigatorDeleteItem = (gcnew System::Windows::Forms::ToolStripButton());

                this->bindingNavigatorMoveFirstItem = (gcnew System::Windows::Forms::ToolStripButton());

                this->bindingNavigatorMovePreviousItem = (gcnew System::Windows::Forms::ToolStripButton());

                this->bindingNavigatorSeparator = (gcnew System::Windows::Forms::ToolStripSeparator());

                this->bindingNavigatorPositionItem = (gcnew System::Windows::Forms::ToolStripTextBox());

                this->bindingNavigatorSeparator1 = (gcnew System::Windows::Forms::ToolStripSeparator());

                this->bindingNavigatorMoveNextItem = (gcnew System::Windows::Forms::ToolStripButton());

                this->bindingNavigatorMoveLastItem = (gcnew System::Windows::Forms::ToolStripButton());

                this->bindingNavigatorSeparator2 = (gcnew System::Windows::Forms::ToolStripSeparator());

               (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->bindingNavigator1))->BeginInit();

                this->bindingNavigator1->SuspendLayout();

                this->SuspendLayout();

                // 

                // txtId

                // 

                this->txtId->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

                     static_cast<System::Byte>(162)));

                this->txtId->Location = System::Drawing::Point(118, 60);

                this->txtId->Name = L"txtId";

                this->txtId->Size = System::Drawing::Size(198, 26);

                this->txtId->TabIndex = 0;

                // 

                // txtFirstName

                // 

                this->txtFirstName->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

                     static_cast<System::Byte>(162)));

                this->txtFirstName->Location = System::Drawing::Point(118, 111);

                this->txtFirstName->Name = L"txtFirstName";

                this->txtFirstName->Size = System::Drawing::Size(198, 26);

                this->txtFirstName->TabIndex = 1;

                // 

                // txtLastName

                // 

                this->txtLastName->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

                     static_cast<System::Byte>(162)));

                this->txtLastName->Location = System::Drawing::Point(119, 164);

                this->txtLastName->Name = L"txtLastName";

                this->txtLastName->Size = System::Drawing::Size(198, 26);

                this->txtLastName->TabIndex = 2;

                // 

                // txtContact

                // 

                this->txtContact->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

                     static_cast<System::Byte>(162)));

                this->txtContact->Location = System::Drawing::Point(119, 211);

                this->txtContact->Name = L"txtContact";

                this->txtContact->Size = System::Drawing::Size(198, 26);

                this->txtContact->TabIndex = 3;

                // 

                // label1

                // 

                this->label1->AutoSize = true;

                this->label1->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

                     static_cast<System::Byte>(162)));

                this->label1->Location = System::Drawing::Point(12, 63);

                this->label1->Name = L"label1";

                this->label1->Size = System::Drawing::Size(75, 20);

                this->label1->TabIndex = 4;

                this->label1->Text = L"Person Id :";

                // 

                // label2

                // 

                this->label2->AutoSize = true;

                this->label2->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

                     static_cast<System::Byte>(162)));

                this->label2->Location = System::Drawing::Point(12, 114);

                this->label2->Name = L"label2";

                this->label2->Size = System::Drawing::Size(81, 20);

                this->label2->TabIndex = 5;

                this->label2->Text = L"First Name :";

                // 

                // label3

                // 

                this->label3->AutoSize = true;

                this->label3->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

                     static_cast<System::Byte>(162)));

                this->label3->Location = System::Drawing::Point(13, 167);

                this->label3->Name = L"label3";

                this->label3->Size = System::Drawing::Size(80, 20);

                this->label3->TabIndex = 6;

                this->label3->Text = L"Last Name :";

                // 

                // label4

                //

this->label4->AutoSize = true;

                this->label4->Font = (gcnew System::Drawing::Font(L"Arial Narrow", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,

                     static_cast<System::Byte>(162)));

                this->label4->Location = System::Drawing::Point(13, 214);

                this->label4->Name = L"label4";

                this->label4->Size = System::Drawing::Size(61, 20);

                this->label4->TabIndex = 7;

                this->label4->Text = L"Contact :";

                // 

                // bindingNavigator1

                // 

                this->bindingNavigator1->AddNewItem = this->bindingNavigatorAddNewItem;

                this->bindingNavigator1->CountItem = this->bindingNavigatorCountItem;

                this->bindingNavigator1->DeleteItem = this->bindingNavigatorDeleteItem;

                this->bindingNavigator1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(11) {

                     this->bindingNavigatorMoveFirstItem,

                          this->bindingNavigatorMovePreviousItem, this->bindingNavigatorSeparator, this->bindingNavigatorPositionItem, this->bindingNavigatorCountItem,

                          this->bindingNavigatorSeparator1, this->bindingNavigatorMoveNextItem, this->bindingNavigatorMoveLastItem, this->bindingNavigatorSeparator2,

                          this->bindingNavigatorAddNewItem, this->bindingNavigatorDeleteItem

                });

                this->bindingNavigator1->Location = System::Drawing::Point(0, 0);

                this->bindingNavigator1->MoveFirstItem = this->bindingNavigatorMoveFirstItem;

                this->bindingNavigator1->MoveLastItem = this->bindingNavigatorMoveLastItem;

                this->bindingNavigator1->MoveNextItem = this->bindingNavigatorMoveNextItem;

                this->bindingNavigator1->MovePreviousItem = this->bindingNavigatorMovePreviousItem;

                this->bindingNavigator1->Name = L"bindingNavigator1";

                this->bindingNavigator1->PositionItem = this->bindingNavigatorPositionItem;

                this->bindingNavigator1->Size = System::Drawing::Size(340, 25);

                this->bindingNavigator1->TabIndex = 8;

                this->bindingNavigator1->Text = L"bindingNavigator1";

                // 

                // bindingNavigatorAddNewItem

                // 

                this->bindingNavigatorAddNewItem->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;

                this->bindingNavigatorAddNewItem->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"bindingNavigatorAddNewItem.Image")));

                this->bindingNavigatorAddNewItem->Name = L"bindingNavigatorAddNewItem";

                this->bindingNavigatorAddNewItem->RightToLeftAutoMirrorImage = true;

                this->bindingNavigatorAddNewItem->Size = System::Drawing::Size(23, 22);

                this->bindingNavigatorAddNewItem->Text = L"Add new";

                // 

                // bindingNavigatorCountItem

                // 

                this->bindingNavigatorCountItem->Name = L"bindingNavigatorCountItem";

                this->bindingNavigatorCountItem->Size = System::Drawing::Size(35, 22);

                this->bindingNavigatorCountItem->Text = L"of {0}";

                this->bindingNavigatorCountItem->ToolTipText = L"Total number of items";

                // 

                // bindingNavigatorDeleteItem

                // 

                this->bindingNavigatorDeleteItem->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;

                this->bindingNavigatorDeleteItem->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"bindingNavigatorDeleteItem.Image")));

                this->bindingNavigatorDeleteItem->Name = L"bindingNavigatorDeleteItem";

                this->bindingNavigatorDeleteItem->RightToLeftAutoMirrorImage = true;

                this->bindingNavigatorDeleteItem->Size = System::Drawing::Size(23, 22);

                this->bindingNavigatorDeleteItem->Text = L"Delete";

                // 

                // bindingNavigatorMoveFirstItem

                // 

                this->bindingNavigatorMoveFirstItem->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;

                this->bindingNavigatorMoveFirstItem->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"bindingNavigatorMoveFirstItem.Image")));

                this->bindingNavigatorMoveFirstItem->Name = L"bindingNavigatorMoveFirstItem";

                this->bindingNavigatorMoveFirstItem->RightToLeftAutoMirrorImage = true;

                this->bindingNavigatorMoveFirstItem->Size = System::Drawing::Size(23, 22);

                this->bindingNavigatorMoveFirstItem->Text = L"Move first";

                // 

                // bindingNavigatorMovePreviousItem

                // 

                this->bindingNavigatorMovePreviousItem->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;

                this->bindingNavigatorMovePreviousItem->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"bindingNavigatorMovePreviousItem.Image")));

                this->bindingNavigatorMovePreviousItem->Name = L"bindingNavigatorMovePreviousItem";

                this->bindingNavigatorMovePreviousItem->RightToLeftAutoMirrorImage = true;

                this->bindingNavigatorMovePreviousItem->Size = System::Drawing::Size(23, 22);

                this->bindingNavigatorMovePreviousItem->Text = L"Move previous";

                // 

                // bindingNavigatorSeparator

                // 

                this->bindingNavigatorSeparator->Name = L"bindingNavigatorSeparator";

                this->bindingNavigatorSeparator->Size = System::Drawing::Size(6, 25);

                // 

                // bindingNavigatorPositionItem

                // 

                this->bindingNavigatorPositionItem->AccessibleName = L"Position";

                this->bindingNavigatorPositionItem->AutoSize = false;

                this->bindingNavigatorPositionItem->Font = (gcnew System::Drawing::Font(L"Segoe UI", 9));

                this->bindingNavigatorPositionItem->Name = L"bindingNavigatorPositionItem";

                this->bindingNavigatorPositionItem->Size = System::Drawing::Size(50, 23);

                this->bindingNavigatorPositionItem->Text = L"0";

                this->bindingNavigatorPositionItem->ToolTipText = L"Current position";

                // 

                // bindingNavigatorSeparator1

                // 

                this->bindingNavigatorSeparator1->Name = L"bindingNavigatorSeparator1";

                this->bindingNavigatorSeparator1->Size = System::Drawing::Size(6, 25);

                // 

                // bindingNavigatorMoveNextItem

                // 

                this->bindingNavigatorMoveNextItem->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;

                this->bindingNavigatorMoveNextItem->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"bindingNavigatorMoveNextItem.Image")));

                this->bindingNavigatorMoveNextItem->Name = L"bindingNavigatorMoveNextItem";

                this->bindingNavigatorMoveNextItem->RightToLeftAutoMirrorImage = true;

                this->bindingNavigatorMoveNextItem->Size = System::Drawing::Size(23, 22);

                this->bindingNavigatorMoveNextItem->Text = L"Move next";

                // 

                // bindingNavigatorMoveLastItem

                // 

               this->bindingNavigatorMoveLastItem->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;

                this->bindingNavigatorMoveLastItem->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"bindingNavigatorMoveLastItem.Image")));

                this->bindingNavigatorMoveLastItem->Name = L"bindingNavigatorMoveLastItem";

                this->bindingNavigatorMoveLastItem->RightToLeftAutoMirrorImage = true;

                this->bindingNavigatorMoveLastItem->Size = System::Drawing::Size(23, 22);

                this->bindingNavigatorMoveLastItem->Text = L"Move last";

                // 

                // bindingNavigatorSeparator2

                // 

                this->bindingNavigatorSeparator2->Name = L"bindingNavigatorSeparator2";

                this->bindingNavigatorSeparator2->Size = System::Drawing::Size(6, 25);

                // 

                // MyForm

                // 

                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

                this->ClientSize = System::Drawing::Size(340, 363);

                this->Controls->Add(this->bindingNavigator1);

                this->Controls->Add(this->label4);

                this->Controls->Add(this->label3);

                this->Controls->Add(this->label2);

                this->Controls->Add(this->label1);

                this->Controls->Add(this->txtContact);

                this->Controls->Add(this->txtLastName);

                this->Controls->Add(this->txtFirstName);

                this->Controls->Add(this->txtId);

                this->Name = L"MyForm";

                this->Text = L"MyForm";

                this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);

               (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->bindingNavigator1))->EndInit();

                this->bindingNavigator1->ResumeLayout(false);

                this->bindingNavigator1->PerformLayout();

                this->ResumeLayout(false);

                this->PerformLayout();

 

          }

#pragma endregion

private: System::Void MyForm_Load(System::Objectsender, System::EventArgse)

     {

 

          SqlConnection^ con = gcnew SqlConnection(L"Data Source=sirius\\SQLEXPRESS02;Initial Catalog=master;User ID=sa;Password=1234;Integrated Security=true");

          SqlCommand^ cmd = gcnew SqlCommand(L"SELECT * FROM dbo.person;", con);

 

          DataSet^ ds = gcnew DataSet(L"ds");

          SqlDataAdapter^ da = gcnew SqlDataAdapter();

          da->SelectCommand = cmd;

          da->Fill(ds);

 

          BindingSource^ bs = gcnew BindingSource();

          bs->DataSource = ds->Tables[0];

          bindingNavigator1->BindingSource = bs;

          txtId->DataBindings->Add(gcnew Binding("Text", bs, "Id"true));

          txtFirstName->DataBindings->Add(gcnew Binding("Text", bs, "Name"true));

          txtLastName->DataBindings->Add(gcnew Binding("Text", bs, "Surname"true));

          txtContact->DataBindings->Add(gcnew Binding("Text", bs, "Contact"true));

          DataRow^ drow = ds->Tables[0]->Rows[0];

 

          int id;

          id = (int)drow[L"Id"];

          txtId->Text = id.ToString();

          txtFirstName->Text = drow[L"Name"]->ToString();

          txtLastName->Text = drow[L"Surname"]->ToString();

          txtContact->Text = drow[L"Contact"]->ToString();

          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);

     cppdatabinding::MyForm form;

     Application::Run(% form);

}

      

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN

About Bahadır Şahin

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Öne Çıkan Yayın

GridView da Seçili Satırı DetailsView da Göstermek

Merhaba arkadaşlar bu makalemizde GridView nesnesi ile birlikte DetailsView nesanesini birlikte kullanacağız. GridView da seçili satırın de...