แฟนพันธ์แท้ของ C++/CLI บน Windows Form มีคนบอกมาว่าโคตรยากมาก

ในห้อง 'คอมพิวเตอร์ & อินเตอร์เน็ต' ตั้งกระทู้โดย ledphong, 19 สิงหาคม 2014.

  1. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    จะเริ่มทยอยลงเรื่อย ๆ คอยอัฟเดทข้อมูลกับ mr.ledphong หรือ ledphong@gmail.com หรือ โทรมาปรึกษาได้ที่ 080-7428876 ยินดีให้คำปรึกษาภาษาที่ยากนักโปรแกรมเมอร์จะเข้าใจ
    ================================
    ทำไมโปรแกรมเมอร์ในเมืองไทยถึงไม่ถึงกึ๋นในด้านภาษา C++/CLI เพราะอะไร หรือเขียนได้แค่ VB.NET หรือ C#.NET เท่านั้นแสดงว่าเรากำลังจำกัดความรู้อยูที่จุดเดียวหรือ Study One Stop Study (SOSS) เท่านั้นจริงหรือแล้วใครบ้างจะมีบทความเจ๋ง ๆ มาแจกให้เพื่อนสมาชิกเราดูบ้างหากมีมากเราก็จะได้เป็นเร็วในส่วนของ C++/CLI เพราะบางคนบอกว่าในเมืองไทยไม่มีคนเก่งจริงในภาษา C++/CLI .....จากคุณเลิศพงศ์
     
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 19 สิงหาคม 2014
  2. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    คนไทยมองเรื่องนี้เป็นเรื่องยากมากเพราะว่ายังทำไม่ถึงกึ๋นหรือไม่ก็ไม่ได้ลองหรือไม่ก็ไม่มีตัวอย่างลอกอะไรสักอย่างแน่ถึงบอกว่ายาก จาก C++/CLI Windows Form
     
  3. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    1.จะปรับแก้จากการโหลด Form1.h เป็นอย่างอื่นได้หรือไม่

    คำตอบได้ดูตัวอย่างเลย
    =====================
    // Help.cpp : main project file.

    #include "stdafx.h"
    #include "FormMain.h" // ปรับแก้จาก Form1.h เป็น FormMain.h ถึงมองหา FormMain.h ได้

    using namespace Help1;

    [STAThreadAttribute]
    int main(array<System::String ^> ^args)
    {
    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);

    // Create the main window and run it
    Application::Run(gcnew Form1());//ไม่ต้องแก้เพราะเป็นระบบ System ของโปรแกรมกำหนดมาให้
    return 0;
    }
     

    ไฟล์ที่แนบมา:

    • CPP_CLI_5.jpg
      CPP_CLI_5.jpg
      ขนาดไฟล์:
      260.4 KB
      เปิดดู:
      163
    • CPP_CLI_6.jpg
      CPP_CLI_6.jpg
      ขนาดไฟล์:
      274.2 KB
      เปิดดู:
      138
  4. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    2.หากแก้ Application::Run(gcnew Form1())

    เป็น Application::Run(gcnew FormMain());
    ============================
    ผลคือ ขึ้นได้ แต่มี Error ด้านล่างครับ
     

    ไฟล์ที่แนบมา:

    • CPP_CLI_7.jpg
      CPP_CLI_7.jpg
      ขนาดไฟล์:
      260.8 KB
      เปิดดู:
      115
  5. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    3.จากโปรแกรม Visual Studio 2010 Ultimate

    ท่านเขียนเป็นกี่ตัวแล้วมีโปรแกรมอะไรที่ท่านยังไม่เข้าใจค่อยมาดูตามบทความของผม ผมจะอัฟเดทให้ดูทุกวัน จะได้เป็นและฉลาดขึ้นมากกว่าเดิม....จากเลิศพงศ์ 080-7428876 (สถานที่ทำงาน อบต.หนองสูงใต้ จังหวัดมุกดาหาร ตำแหน่งนักพัฒนาชุมชน)
    ผมเอาแผ่นภาพของ Windows 8 มาให้ดูว่าเขาเขียนด้วยภาษาอะไรบ้าง
    มีนักศึกษามหาวิทยาลัยอยากศึกษาโปรแกรม C++/CLI บน Windows Form หาดูและศึกษายากมากและโคตรหินมากๆๆเลยมีคนบอกผมมาอย่างงั้น ครับแต่จริงๆ คุณยังไม่ได้ศึกษาแบบทุ่มสุด ๆ หรือทดลองแบบค่อยเป็นค่อยไปก็เป็นไปได้ว่าท่านจะไปถึงดวงดาวหรือไม่
    แล้วยังบอกมาว่าผมจบโปรแกรมเมอร์ สาขาวิศวะคอมพิวเตอร์ มาอยากทำงานในโรงงานหรือ เขียนโปรแกรมขาย แต่มีคู่มือมาวางขายที่แปลเป็นไทยมีแต่ VB 6.0 ,VB.NET ,C#.NET แต่ไอ้ C++/CLI บน Windows Form ทำไมไม่มีสักกะทีหรือว่ายากเกินไป
     

    ไฟล์ที่แนบมา:

    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 19 สิงหาคม 2014
  6. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    4.จัด FormMain ให้โหลดกึ่งกลางจอภาพ ทำได้อย่างไร

    จัด FormMain ให้โหลดกึ่งกลางจอภาพ ทำได้อย่างไร
    ===========================
    StartPosition = FormStartPosition::CenterScreen;//จัดกึ่งกลาง
     

    ไฟล์ที่แนบมา:

    • CPP_CLI_9.jpg
      CPP_CLI_9.jpg
      ขนาดไฟล์:
      257.3 KB
      เปิดดู:
      98
  7. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    5.ADO.NET Namespaces ใช้ติดต่อฐานข้อมูลมีอะไรบ้าง

    System::Data::OleDb ติดต่อฐานข้อมูล MS Access DataBase
    ===================================
    System::Data::SqlClient ติดต่อฐานข้อมูล MS SQL Server DataBase
    ===================================
    System::Data::SQLite ติดต่อฐานข้อมูล MS SQL ServerCe DataBase (ไฟล์เล็ก ๆ บน Pocket PC หรือมือถือ Tablet)
     
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 19 สิงหาคม 2014
  8. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    6.ตัวอย่างแบบนี้จะเริ่มต้นยังไงดีถึงจะ Run Program แบบไม่มี Error

    กำหนดตัวแปรและโครงสร้างดูตามภาพ
    =======================
    txtCodePK คือ ลำดับที่
    txtFirstName คือ ชื่อ
    txtLastName คือ สกุล
    btnAdd คือ ปุ่มบันทึก
    btnShow คือ ปุ่มแสดงข้อมูล
    btnRefresh คือ ปุ่มรีเฟรซ
     

    ไฟล์ที่แนบมา:

    • CPP_CLI_10.jpg
      CPP_CLI_10.jpg
      ขนาดไฟล์:
      285.5 KB
      เปิดดู:
      101
  9. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    7.ตัวอย่าง C++/CLI GUI ติดต่อฐานข้อมูล MS Access 2003

    ไปกำหนดค่าตัวแปรต่าง ๆ ใน MS Access 2003 ดูตามรูปครับ
    กำหนดชื่อฐานข้อมูล dbAccess (เก็บไว้ใน C:\)
    ชื่อตารางกำหนดชื่อว่า tblAccess และลองกรอกข้อมูลอะไรลงไปในฐานข้อมูลซะก่อนสัก 2-3 ชื่อ เพื่อจะได้แสดงข้อมูลออกมาว่าถูกต้องหรือไม่
     

    ไฟล์ที่แนบมา:

    • CPP_CLI_11.jpg
      CPP_CLI_11.jpg
      ขนาดไฟล์:
      210.4 KB
      เปิดดู:
      102
    • CPP_CLI_12.jpg
      CPP_CLI_12.jpg
      ขนาดไฟล์:
      180.1 KB
      เปิดดู:
      85
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 19 สิงหาคม 2014
  10. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    8.กลับมาที่หน้าจอ FormMain.h เพื่อคลิ๊กเม้าส์ขวาดูโค้ด

    โค้ดเดิม ๆ ยังไม่เพิ่มอะไร
    ===============================
    #pragma once

    namespace Help1 {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
     

    ไฟล์ที่แนบมา:

    • CPP_CLI_13.jpg
      CPP_CLI_13.jpg
      ขนาดไฟล์:
      280 KB
      เปิดดู:
      86
  11. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    9.เพิ่มคำสั่งในการติดต่อฐานข้อมูล MS Access 2003

    #pragma once

    namespace dbOlder {

    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; // for MS SQL Server database
    using namespace System::Data::OleDb; // for MS Access database (เพิ่มบรรทัดนี้เข้ามา)
     

    ไฟล์ที่แนบมา:

    • CPP_CLI_14.jpg
      CPP_CLI_14.jpg
      ขนาดไฟล์:
      266.2 KB
      เปิดดู:
      93
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 19 สิงหาคม 2014
  12. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    10.เพิ่มคำสั่งเข้ามาอีกเพื่อประกาศให้รับทราบตัวแปร

    #pragma once

    namespace Help1 {

    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; // for MS SQL Server database
    using namespace System::Data::OleDb; // for MS Access database (เพิ่มบรรทัดนี้เข้ามา)

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    /*public:*/

    private:
    // determines number lines of text each row should allow:
    static int iRowHeight = 0;
    String^ strConnection; // DB connection string
    StringFormat^ strFormat; // to format the grid rows.
    ArrayList^ arrColumnLefts; // left coordinates of columns
    ArrayList^ arrColumnWidths; // column widths
    int iCellHeight; // datagridview cell dimensions
    int iTotalWidth;
    int iRow; // counter
    bool bFirstPage; // to check whether we are printing first page
    bool bNewPage; // to check whether we are printing a new page
     

    ไฟล์ที่แนบมา:

    • CPP_CLI_15.jpg
      CPP_CLI_15.jpg
      ขนาดไฟล์:
      307.8 KB
      เปิดดู:
      100
  13. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    12.กลับออกมาแล้วเข้าไปคลิ๊กปุ่ม Run ดู หรือ กดปุ่ม F5

    แม้โปรแกรมจะขึ้น Error ก็อย่าพึ่งตกใจเพราะเรายังกำหนดคำสั่งหรือตัวแปรไม่หมดครับ
     

    ไฟล์ที่แนบมา:

    • CPP_CLI_16.jpg
      CPP_CLI_16.jpg
      ขนาดไฟล์:
      284.4 KB
      เปิดดู:
      102
  14. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    13.คำสั่งเพื่อติดต่อฐานข้อมูลประกาศไว้

    ดูตามรูปครับในกรอบสีแดง ๆ
    ====================
    ได้แล้วครับโค้ดแรก
    =====================
    #pragma once

    namespace dbOlder {

    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; // for MS SQL Server database
    using namespace System::Data::OleDb; // for MS Access database
    using namespace System::Text;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {

    private:
    // determines number lines of text each row should allow:
    static int iRowHeight = 0;
    String^ strConnection; // DB connection string
    StringFormat^ strFormat; // to format the grid rows.
    ArrayList^ arrColumnLefts; // left coordinates of columns
    ArrayList^ arrColumnWidths; // column widths
    int iCellHeight; // datagridview cell dimensions
    int iTotalWidth;
    int iRow; // counter
    bool bFirstPage; // to check whether we are printing first page
    bool bNewPage; // to check whether we are printing a new page
    private: System::Windows::Forms::TextBox^ txtCodePK;



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

    private: System::Windows::Forms::Button^ button2;
    private: System::Windows::Forms::Label^ label1;
    private: System::Windows::Forms::Label^ label2;
    private: System::Windows::Forms::Label^ label3;

    private: System::Windows::Forms::Button^ button3;
    private: System::Windows::Forms::DataGridViewTextBoxColumn^ Initials;
    private: System::Windows::Forms::DataGridViewTextBoxColumn^ First_Name;
    private: System::Windows::Forms::DataGridViewTextBoxColumn^ Last_Name;
    int iHeaderHeight; // header height

    public:
    Form1(void)
    {
    InitializeComponent();
    InitializeVariables();//
    //
    //TODO: Add the constructor code here
    //
    StartPosition = FormStartPosition::CenterScreen;//จัดกึ่งกลาง
    }

    protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
    if (components)
    {
    delete components;
    }
    }

    private:
    void InitializeVariables()
    {
    // For SQL server side database:
    // strConnection = "data source=localhost;Integrated Security=SSPI;Initial Catalog=yourDatabase;"
    // For client side OLE database:
    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\dbAccess.mdb";
    arrColumnLefts = gcnew ArrayList();
    arrColumnWidths = gcnew ArrayList();
    iCellHeight = 0;
    iTotalWidth = 0;
    iRow = 0;
    bFirstPage = false;
    bNewPage = false;
    iHeaderHeight = 0;
    }

    private: System::Windows::Forms::DataGridView^ dataGridView1;
    private: System::Windows::Forms::Button^ button1;


    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->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
    this->Initials = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
    this->First_Name = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
    this->Last_Name = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
    this->button1 = (gcnew System::Windows::Forms::Button());
    this->txtCodePK = (gcnew System::Windows::Forms::TextBox());
    this->txtFirstName = (gcnew System::Windows::Forms::TextBox());
    this->txtLastName = (gcnew System::Windows::Forms::TextBox());
    this->button2 = (gcnew System::Windows::Forms::Button());
    this->label1 = (gcnew System::Windows::Forms::Label());
    this->label2 = (gcnew System::Windows::Forms::Label());
    this->label3 = (gcnew System::Windows::Forms::Label());
    this->button3 = (gcnew System::Windows::Forms::Button());
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->BeginInit();
    this->SuspendLayout();
    //
    // dataGridView1
    //
    this->dataGridView1->AllowUserToAddRows = false;
    this->dataGridView1->AllowUserToDeleteRows = false;
    this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
    this->dataGridView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^ >(3) {this->Initials,
    this->First_Name, this->Last_Name});
    this->dataGridView1->Location = System::Drawing::point(12, 12);
    this->dataGridView1->Name = L"dataGridView1";
    this->dataGridView1->ReadOnly = true;
    this->dataGridView1->Size = System::Drawing::Size(368, 205);
    this->dataGridView1->TabIndex = 0;
    //
    // Initials
    //
    this->Initials->HeaderText = L"ลำดับที่";
    this->Initials->Name = L"Initials";
    this->Initials->ReadOnly = true;
    this->Initials->Width = 80;
    //
    // First_Name
    //
    this->First_Name->HeaderText = L"ชื่อ";
    this->First_Name->Name = L"First_Name";
    this->First_Name->ReadOnly = true;
    this->First_Name->Width = 120;
    //
    // Last_Name
    //
    this->Last_Name->HeaderText = L"สกุล";
    this->Last_Name->Name = L"Last_Name";
    this->Last_Name->ReadOnly = true;
    this->Last_Name->Width = 120;
    //
    // button1
    //
    this->button1->Anchor = System::Windows::Forms::AnchorStyles::None;
    this->button1->Location = System::Drawing::point(402, 12);
    this->button1->Name = L"button1";
    this->button1->Size = System::Drawing::Size(103, 43);
    this->button1->TabIndex = 1;
    this->button1->Text = L"แสดงข้อมูล";
    this->button1->UseVisualStyleBackColor = true;
    this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    //
    // txtCodePK
    //
    this->txtCodePK->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->txtCodePK->Location = System::Drawing::point(118, 225);
    this->txtCodePK->Name = L"txtCodePK";
    this->txtCodePK->Size = System::Drawing::Size(100, 20);
    this->txtCodePK->TabIndex = 2;
    //
    // txtFirstName
    //
    this->txtFirstName->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->txtFirstName->Location = System::Drawing::point(118, 251);
    this->txtFirstName->Name = L"txtFirstName";
    this->txtFirstName->Size = System::Drawing::Size(100, 20);
    this->txtFirstName->TabIndex = 3;
    //
    // txtLastName
    //
    this->txtLastName->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->txtLastName->Location = System::Drawing::point(118, 277);
    this->txtLastName->Name = L"txtLastName";
    this->txtLastName->Size = System::Drawing::Size(100, 20);
    this->txtLastName->TabIndex = 4;
    //
    // button2
    //
    this->button2->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->button2->Location = System::Drawing::point(252, 225);
    this->button2->Name = L"button2";
    this->button2->Size = System::Drawing::Size(75, 25);
    this->button2->TabIndex = 9;
    this->button2->Text = L"บันทึก";
    this->button2->UseVisualStyleBackColor = true;
    this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
    //
    // label1
    //
    this->label1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->label1->AutoSize = true;
    this->label1->Location = System::Drawing::point(50, 225);
    this->label1->Name = L"label1";
    this->label1->Size = System::Drawing::Size(45, 13);
    this->label1->TabIndex = 10;
    this->label1->Text = L"ลำดับที่ :";
    //
    // label2
    //
    this->label2->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->label2->AutoSize = true;
    this->label2->Location = System::Drawing::point(69, 251);
    this->label2->Name = L"label2";
    this->label2->Size = System::Drawing::Size(26, 13);
    this->label2->TabIndex = 11;
    this->label2->Text = L"ชื่อ :";
    //
    // label3
    //
    this->label3->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->label3->AutoSize = true;
    this->label3->Location = System::Drawing::point(63, 277);
    this->label3->Name = L"label3";
    this->label3->Size = System::Drawing::Size(32, 13);
    this->label3->TabIndex = 12;
    this->label3->Text = L"สกุล :";
    //
    // button3
    //
    this->button3->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->button3->Location = System::Drawing::point(252, 256);
    this->button3->Name = L"button3";
    this->button3->Size = System::Drawing::Size(75, 25);
    this->button3->TabIndex = 17;
    this->button3->Text = L"Refresh";
    this->button3->UseVisualStyleBackColor = true;
    this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
    //
    // Form1
    //
    this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    this->ClientSize = System::Drawing::Size(531, 318);
    this->Controls->Add(this->button3);
    this->Controls->Add(this->label3);
    this->Controls->Add(this->label2);
    this->Controls->Add(this->label1);
    this->Controls->Add(this->button2);
    this->Controls->Add(this->txtLastName);
    this->Controls->Add(this->txtFirstName);
    this->Controls->Add(this->txtCodePK);
    this->Controls->Add(this->button1);
    this->Controls->Add(this->dataGridView1);
    this->Name = L"Form1";
    this->Text = L"โปรแกรมทดสอบการแสดงผล C++/CLI บนฐานข้อมูล MS Access 2003 ";
    this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->EndInit();
    this->ResumeLayout(false);
    this->PerformLayout();

    }
    #pragma endregion
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    Cursor->Current = Cursors::WaitCursor;

    // SQL:
    //SqlConnection ^ sqlConnection;
    //SqlCommand ^ sqlCommand;
    //SqlDataReader ^ sqlReader;

    // OLE DB:
    OleDbConnection^ oleDbConnection;
    OleDbCommand ^ oleDbCommand;
    OleDbDataReader^ oleDbDataReader;

    try{
    String^ strQuery = "SELECT CodePK , FirstName, LastName FROM tblAccess ";

    // SQL:
    //sqlConnection = gcnew SqlConnection(strConnection);
    //sqlConnection.Open();
    //sqlCommand = gcnew SqlCommand(strQuery, sqlConnection);
    //sqlReader = sqlCommand->ExecuteReader();
    //while (sqlReader->Read()){

    // OLE DB:
    oleDbConnection = gcnew OleDbConnection(strConnection);
    oleDbConnection->Open();
    oleDbCommand = gcnew OleDbCommand(strQuery, oleDbConnection);
    oleDbDataReader = oleDbCommand->ExecuteReader();
    while(oleDbDataReader->Read()){
    //Reformat dateTime
    // String ^ thisDate = String::Empty;
    // if (oleDbDataReader->GetFieldType(5)->ToString() =="System.DateTime"){
    // if (oleDbDataReader[5] != System::DBNull::Value){
    // DateTime^ dt = (DateTime^)oleDbDataReader[5];
    // thisDate = dt->ToString("dd MMM yyyy");
    // }
    // }

    //
    array<System::Object^>^ row = { oleDbDataReader[0], oleDbDataReader[1], oleDbDataReader[2] };
    this->dataGridView1->Rows->Add(row);
    }
    }
    catch(Exception^ e){
    MessageBox::Show(e->Message, "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
    return;
    }
    finally{
    Cursor->Current = Cursors::Default;
    oleDbConnection->Close();
    if(oleDbDataReader != nullptr){
    oleDbDataReader->Close();
    oleDbDataReader = nullptr;
    }
    if(oleDbCommand != nullptr){
    oleDbCommand = nullptr;
    }
    }

    }
    private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

    }
    private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
    //====Clear Text Box=======
    txtCodePK->Text = "";
    txtFirstName->Text = "";
    txtLastName->Text = "";
    // txtMobile->Text = "";
    // txtEmail->Text = "";
    // txtDOB->Text = "";
    // txtAddress->Text = "";
    }
    private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
    /*MySqlConnection^ con = gcnew MySqlConnection("Network Address=localhost;" "User Name='root';" "Password='password';" "database=Polynomigra");
    con->Open();
    string username = usernameText.Text;
    MySqlCommand^ cmdExecute = gcnew MySqlCommand("INSERT INTO logins VALUES ('1','testuser','testpassword')", con);
    MySqlDataReader^ reader = cmdExecute->ExecuteReader();
    con->Close();*/
    //==================================================
    /* try{
    MessageBox::Show("Inserting Value ...");
    SqlCommand ^cmdInsertValue = db->CreateCommand();
    cmdInsertValue->CommandText = "INSERT INTO tblOlder VALUES(NULL, 'A Value');";
    cmdInsertValue->ExecuteNonQuery();
    MessageBox::Show("Value Inserted.");
    }
    catch (Exception ^e)
    {
    MessageBox::Show("Error Executing SQL: " + e->ToString(), "Exception While Inserting Value Into MyTable ...");
    }*/

    //==================================================
    }
    };
    }
     

    ไฟล์ที่แนบมา:

    • CPP_CLI_17.jpg
      CPP_CLI_17.jpg
      ขนาดไฟล์:
      301.4 KB
      เปิดดู:
      93
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 19 สิงหาคม 2014
  15. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    14.โค้ดเพิ่มเติมในส่วนแรกที่ยังค้างต่อ

    ทำฐานข้อมูลใหม่เก็บไว้ในไดร์ฟ C:\
     

    ไฟล์ที่แนบมา:

    • CPP1.jpg
      CPP1.jpg
      ขนาดไฟล์:
      173.8 KB
      เปิดดู:
      79
    • CPP2.jpg
      CPP2.jpg
      ขนาดไฟล์:
      194.5 KB
      เปิดดู:
      76
    • CPP3.jpg
      CPP3.jpg
      ขนาดไฟล์:
      222.8 KB
      เปิดดู:
      76
    • CPP4.jpg
      CPP4.jpg
      ขนาดไฟล์:
      244.7 KB
      เปิดดู:
      73
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 19 สิงหาคม 2014
  16. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    15.โค้ดสำเร็จเลยครับ หากต้องการโค้ดฝาก Email ไว้ได้เลยครับ

    จากเลิศพงศ์ ledphong@gmail.com
    ==================================
    #pragma once

    namespace dbOlder {

    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; // for MS SQL Server database
    using namespace System::Data::OleDb; // for MS Access database
    using namespace System::Text;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {

    private:
    // determines number lines of text each row should allow:
    static int iRowHeight = 0;
    String^ strConnection; // DB connection string
    StringFormat^ strFormat; // to format the grid rows.
    ArrayList^ arrColumnLefts; // left coordinates of columns
    ArrayList^ arrColumnWidths; // column widths
    int iCellHeight; // datagridview cell dimensions
    int iTotalWidth;
    int iRow; // counter
    bool bFirstPage; // to check whether we are printing first page
    bool bNewPage; // to check whether we are printing a new page
    private: System::Windows::Forms::DataGridViewTextBoxColumn^ Initials;
    private: System::Windows::Forms::DataGridViewTextBoxColumn^ First_Name;
    private: System::Windows::Forms::DataGridViewTextBoxColumn^ Last_Name;
    private: System::Windows::Forms::DataGridViewTextBoxColumn^ Mobile;
    private: System::Windows::Forms::DataGridViewTextBoxColumn^ Email;
    private: System::Windows::Forms::DataGridViewTextBoxColumn^ DOB;
    private: System::Windows::Forms::DataGridViewTextBoxColumn^ Address;
    private: System::Windows::Forms::TextBox^ txtInitials;
    private: System::Windows::Forms::TextBox^ txtFirstName;
    private: System::Windows::Forms::TextBox^ txtLastName;
    private: System::Windows::Forms::TextBox^ txtMobile;
    private: System::Windows::Forms::TextBox^ txtEmail;
    private: System::Windows::Forms::TextBox^ txtDOB;
    private: System::Windows::Forms::TextBox^ txtAddress;

    private: System::Windows::Forms::Button^ button2;
    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::Label^ label5;
    private: System::Windows::Forms::Label^ label6;
    private: System::Windows::Forms::Label^ label7;
    private: System::Windows::Forms::Button^ button3;
    int iHeaderHeight; // header height

    public:
    Form1(void)
    {
    InitializeComponent();
    InitializeVariables();//
    //
    //TODO: Add the constructor code here
    //
    StartPosition = FormStartPosition::CenterScreen;//จัดกึ่งกลาง
    }

    protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
    if (components)
    {
    delete components;
    }
    }

    private:
    void InitializeVariables()
    {
    // For SQL server side database:
    // strConnection = "data source=localhost;Integrated Security=SSPI;Initial Catalog=yourDatabase;"
    // For client side OLE database:
    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\yourDatabase.mdb";
    arrColumnLefts = gcnew ArrayList();
    arrColumnWidths = gcnew ArrayList();
    iCellHeight = 0;
    iTotalWidth = 0;
    iRow = 0;
    bFirstPage = false;
    bNewPage = false;
    iHeaderHeight = 0;
    }

    private: System::Windows::Forms::DataGridView^ dataGridView1;
    private: System::Windows::Forms::Button^ button1;


    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->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
    this->Initials = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
    this->First_Name = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
    this->Last_Name = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
    this->Mobile = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
    this->Email = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
    this->DOB = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
    this->Address = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
    this->button1 = (gcnew System::Windows::Forms::Button());
    this->txtInitials = (gcnew System::Windows::Forms::TextBox());
    this->txtFirstName = (gcnew System::Windows::Forms::TextBox());
    this->txtLastName = (gcnew System::Windows::Forms::TextBox());
    this->txtMobile = (gcnew System::Windows::Forms::TextBox());
    this->txtEmail = (gcnew System::Windows::Forms::TextBox());
    this->txtDOB = (gcnew System::Windows::Forms::TextBox());
    this->txtAddress = (gcnew System::Windows::Forms::TextBox());
    this->button2 = (gcnew System::Windows::Forms::Button());
    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->label5 = (gcnew System::Windows::Forms::Label());
    this->label6 = (gcnew System::Windows::Forms::Label());
    this->label7 = (gcnew System::Windows::Forms::Label());
    this->button3 = (gcnew System::Windows::Forms::Button());
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->BeginInit();
    this->SuspendLayout();
    //
    // dataGridView1
    //
    this->dataGridView1->AllowUserToAddRows = false;
    this->dataGridView1->AllowUserToDeleteRows = false;
    this->dataGridView1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
    | System::Windows::Forms::AnchorStyles::Left)
    | System::Windows::Forms::AnchorStyles::Right));
    this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
    this->dataGridView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^ >(7) {this->Initials,
    this->First_Name, this->Last_Name, this->Mobile, this->Email, this->DOB, this->Address});
    this->dataGridView1->Location = System::Drawing::point(3, 1);
    this->dataGridView1->Name = L"dataGridView1";
    this->dataGridView1->ReadOnly = true;
    this->dataGridView1->Size = System::Drawing::Size(838, 205);
    this->dataGridView1->TabIndex = 0;
    //
    // Initials
    //
    this->Initials->HeaderText = L"ลำดับที่";
    this->Initials->Name = L"Initials";
    this->Initials->ReadOnly = true;
    this->Initials->Width = 80;
    //
    // First_Name
    //
    this->First_Name->HeaderText = L"ชื่อ";
    this->First_Name->Name = L"First_Name";
    this->First_Name->ReadOnly = true;
    this->First_Name->Width = 120;
    //
    // Last_Name
    //
    this->Last_Name->HeaderText = L"สกุล";
    this->Last_Name->Name = L"Last_Name";
    this->Last_Name->ReadOnly = true;
    this->Last_Name->Width = 120;
    //
    // Mobile
    //
    this->Mobile->HeaderText = L"โทรศัพท์";
    this->Mobile->Name = L"Mobile";
    this->Mobile->ReadOnly = true;
    //
    // Email
    //
    this->Email->HeaderText = L"อีเมลล์";
    this->Email->Name = L"Email";
    this->Email->ReadOnly = true;
    this->Email->Width = 150;
    //
    // DOB
    //
    this->DOB->HeaderText = L"วันเกิด";
    this->DOB->Name = L"DOB";
    this->DOB->ReadOnly = true;
    //
    // Address
    //
    this->Address->HeaderText = L"ที่อยู่";
    this->Address->Name = L"Address";
    this->Address->ReadOnly = true;
    this->Address->Width = 120;
    //
    // button1
    //
    this->button1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Right));
    this->button1->Location = System::Drawing::point(847, 1);
    this->button1->Name = L"button1";
    this->button1->Size = System::Drawing::Size(103, 43);
    this->button1->TabIndex = 1;
    this->button1->Text = L"แสดงข้อมูล";
    this->button1->UseVisualStyleBackColor = true;
    this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    //
    // txtInitials
    //
    this->txtInitials->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->txtInitials->Location = System::Drawing::point(118, 225);
    this->txtInitials->Name = L"txtInitials";
    this->txtInitials->Size = System::Drawing::Size(100, 20);
    this->txtInitials->TabIndex = 2;
    //
    // txtFirstName
    //
    this->txtFirstName->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->txtFirstName->Location = System::Drawing::point(118, 251);
    this->txtFirstName->Name = L"txtFirstName";
    this->txtFirstName->Size = System::Drawing::Size(100, 20);
    this->txtFirstName->TabIndex = 3;
    //
    // txtLastName
    //
    this->txtLastName->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->txtLastName->Location = System::Drawing::point(118, 277);
    this->txtLastName->Name = L"txtLastName";
    this->txtLastName->Size = System::Drawing::Size(100, 20);
    this->txtLastName->TabIndex = 4;
    //
    // txtMobile
    //
    this->txtMobile->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->txtMobile->Location = System::Drawing::point(322, 225);
    this->txtMobile->Name = L"txtMobile";
    this->txtMobile->Size = System::Drawing::Size(100, 20);
    this->txtMobile->TabIndex = 5;
    //
    // txtEmail
    //
    this->txtEmail->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->txtEmail->Location = System::Drawing::point(322, 251);
    this->txtEmail->Name = L"txtEmail";
    this->txtEmail->Size = System::Drawing::Size(100, 20);
    this->txtEmail->TabIndex = 6;
    //
    // txtDOB
    //
    this->txtDOB->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->txtDOB->Location = System::Drawing::point(322, 277);
    this->txtDOB->Name = L"txtDOB";
    this->txtDOB->Size = System::Drawing::Size(100, 20);
    this->txtDOB->TabIndex = 7;
    //
    // txtAddress
    //
    this->txtAddress->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->txtAddress->Location = System::Drawing::point(522, 223);
    this->txtAddress->Name = L"txtAddress";
    this->txtAddress->Size = System::Drawing::Size(100, 20);
    this->txtAddress->TabIndex = 8;
    //
    // button2
    //
    this->button2->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->button2->Location = System::Drawing::point(642, 220);
    this->button2->Name = L"button2";
    this->button2->Size = System::Drawing::Size(75, 25);
    this->button2->TabIndex = 9;
    this->button2->Text = L"บันทึก";
    this->button2->UseVisualStyleBackColor = true;
    this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
    //
    // label1
    //
    this->label1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->label1->AutoSize = true;
    this->label1->Location = System::Drawing::point(50, 225);
    this->label1->Name = L"label1";
    this->label1->Size = System::Drawing::Size(45, 13);
    this->label1->TabIndex = 10;
    this->label1->Text = L"ลำดับที่ :";
    //
    // label2
    //
    this->label2->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->label2->AutoSize = true;
    this->label2->Location = System::Drawing::point(69, 251);
    this->label2->Name = L"label2";
    this->label2->Size = System::Drawing::Size(26, 13);
    this->label2->TabIndex = 11;
    this->label2->Text = L"ชื่อ :";
    //
    // label3
    //
    this->label3->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->label3->AutoSize = true;
    this->label3->Location = System::Drawing::point(63, 277);
    this->label3->Name = L"label3";
    this->label3->Size = System::Drawing::Size(32, 13);
    this->label3->TabIndex = 12;
    this->label3->Text = L"สกุล :";
    //
    // label4
    //
    this->label4->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->label4->AutoSize = true;
    this->label4->Location = System::Drawing::point(262, 223);
    this->label4->Name = L"label4";
    this->label4->Size = System::Drawing::Size(54, 13);
    this->label4->TabIndex = 13;
    this->label4->Text = L"โทรศัพท์ :";
    //
    // label5
    //
    this->label5->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->label5->AutoSize = true;
    this->label5->Location = System::Drawing::point(271, 251);
    this->label5->Name = L"label5";
    this->label5->Size = System::Drawing::Size(43, 13);
    this->label5->TabIndex = 14;
    this->label5->Text = L"อีเมลล์ :";
    //
    // label6
    //
    this->label6->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->label6->AutoSize = true;
    this->label6->Location = System::Drawing::point(268, 277);
    this->label6->Name = L"label6";
    this->label6->Size = System::Drawing::Size(46, 13);
    this->label6->TabIndex = 15;
    this->label6->Text = L"วันเกิด :";
    //
    // label7
    //
    this->label7->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->label7->AutoSize = true;
    this->label7->Location = System::Drawing::point(469, 223);
    this->label7->Name = L"label7";
    this->label7->Size = System::Drawing::Size(33, 13);
    this->label7->TabIndex = 16;
    this->label7->Text = L"ที่อยู่ :";
    //
    // button3
    //
    this->button3->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
    this->button3->Location = System::Drawing::point(642, 251);
    this->button3->Name = L"button3";
    this->button3->Size = System::Drawing::Size(75, 25);
    this->button3->TabIndex = 17;
    this->button3->Text = L"Refresh";
    this->button3->UseVisualStyleBackColor = true;
    this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
    //
    // Form1
    //
    this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    this->ClientSize = System::Drawing::Size(954, 318);
    this->Controls->Add(this->button3);
    this->Controls->Add(this->label7);
    this->Controls->Add(this->label6);
    this->Controls->Add(this->label5);
    this->Controls->Add(this->label4);
    this->Controls->Add(this->label3);
    this->Controls->Add(this->label2);
    this->Controls->Add(this->label1);
    this->Controls->Add(this->button2);
    this->Controls->Add(this->txtAddress);
    this->Controls->Add(this->txtDOB);
    this->Controls->Add(this->txtEmail);
    this->Controls->Add(this->txtMobile);
    this->Controls->Add(this->txtLastName);
    this->Controls->Add(this->txtFirstName);
    this->Controls->Add(this->txtInitials);
    this->Controls->Add(this->button1);
    this->Controls->Add(this->dataGridView1);
    this->Name = L"Form1";
    this->Text = L"โปรแกรมทดสอบการแสดงผล C++/CLI บนฐานข้อมูล MS Access 2003 ";
    this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->EndInit();
    this->ResumeLayout(false);
    this->PerformLayout();

    }
    #pragma endregion
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    Cursor->Current = Cursors::WaitCursor;

    // SQL:
    //SqlConnection ^ sqlConnection;
    //SqlCommand ^ sqlCommand;
    //SqlDataReader ^ sqlReader;

    // OLE DB:
    OleDbConnection^ oleDbConnection;
    OleDbCommand ^ oleDbCommand;
    OleDbDataReader^ oleDbDataReader;

    try{
    String^ strQuery = "SELECT Initials , First_Name, Last_Name, Mobile, Email, DOB, Address FROM VI_Patient";

    // SQL:
    //sqlConnection = gcnew SqlConnection(strConnection);
    //sqlConnection.Open();
    //sqlCommand = gcnew SqlCommand(strQuery, sqlConnection);
    //sqlReader = sqlCommand->ExecuteReader();
    //while (sqlReader->Read()){

    // OLE DB:
    oleDbConnection = gcnew OleDbConnection(strConnection);
    oleDbConnection->Open();
    oleDbCommand = gcnew OleDbCommand(strQuery, oleDbConnection);
    oleDbDataReader = oleDbCommand->ExecuteReader();
    while(oleDbDataReader->Read()){
    //Reformat dateTime
    String ^ thisDate = String::Empty;
    if (oleDbDataReader->GetFieldType(5)->ToString() =="System.DateTime"){
    if (oleDbDataReader[5] != System::DBNull::Value){
    DateTime^ dt = (DateTime^)oleDbDataReader[5];
    thisDate = dt->ToString("dd MMM yyyy");
    }
    }

    //
    array<System::Object^>^ row = { oleDbDataReader[0], oleDbDataReader[1], oleDbDataReader[2],
    oleDbDataReader[3], oleDbDataReader[4], thisDate, oleDbDataReader[6] };
    this->dataGridView1->Rows->Add(row);
    }
    }
    catch(Exception^ e){
    MessageBox::Show(e->Message, "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
    return;
    }
    finally{
    Cursor->Current = Cursors::Default;
    oleDbConnection->Close();
    if(oleDbDataReader != nullptr){
    oleDbDataReader->Close();
    oleDbDataReader = nullptr;
    }
    if(oleDbCommand != nullptr){
    oleDbCommand = nullptr;
    }
    }

    }
    private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

    }
    private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
    //====Clear Text Box=======
    txtInitials->Text = "";
    txtFirstName->Text = "";
    txtLastName->Text = "";
    txtMobile->Text = "";
    txtEmail->Text = "";
    txtDOB->Text = "";
    txtAddress->Text = "";
    }
    private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
    /*MySqlConnection^ con = gcnew MySqlConnection("Network Address=localhost;" "User Name='root';" "Password='password';" "database=Polynomigra");
    con->Open();
    string username = usernameText.Text;
    MySqlCommand^ cmdExecute = gcnew MySqlCommand("INSERT INTO logins VALUES ('1','testuser','testpassword')", con);
    MySqlDataReader^ reader = cmdExecute->ExecuteReader();
    con->Close();*/
    //==================================================
    /* try{
    MessageBox::Show("Inserting Value ...");
    SqlCommand ^cmdInsertValue = db->CreateCommand();
    cmdInsertValue->CommandText = "INSERT INTO tblOlder VALUES(NULL, 'A Value');";
    cmdInsertValue->ExecuteNonQuery();
    MessageBox::Show("Value Inserted.");
    }
    catch (Exception ^e)
    {
    MessageBox::Show("Error Executing SQL: " + e->ToString(), "Exception While Inserting Value Into MyTable ...");
    }*/

    //==================================================
    }
    };
    }
     

    ไฟล์ที่แนบมา:

    • CPP5.jpg
      CPP5.jpg
      ขนาดไฟล์:
      71.9 KB
      เปิดดู:
      75
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 19 สิงหาคม 2014
  17. ledphong

    ledphong เป็นที่รู้จักกันดี

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    ฝากอีเมลล์ไว้เลยครับจะจัดส่งโค้ดตัวอย่างนี้ให้ครับเพราะลงไว้เดี๋ยว Copy ไม่หมด
     

แชร์หน้านี้

Loading...