C++/CLI ตัวอย่างที่สามารถไปพัฒนาต่อยอดเป็นโปรเจ็คจบมหาวิทยาลัยได้

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

  1. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 20
    ไปดับเบิ้ลคลิ๊กที่ ปุ่มเพิ่มข้อมูล
    กำหนดภายใต้ #pragma endregion
    ===================================
    //===============ปุ่มเพิ่มข้อมูล==========================

    //=======ตรวจสอบการกดปุ่มก่อนครับ===================
    if (String::IsNullOrEmpty(txtCodePK->Text)){
    MessageBox::Show("กรุณากดปุ่ม GenID ก่อนครับ","รายงานสถานะ");
    return;
    }
    //=============================================
    if (String::IsNullOrEmpty(txtFirstName->Text)){
    MessageBox::Show("กรุณาพิมพ์ชื่อก่อนครับ","รายงานสถานะ");
    return;
    }
    //==============================================
    if (String::IsNullOrEmpty(txtLastName->Text)){
    MessageBox::Show("กรุณาพิมพ์สกุลก่อนครับ","รายงานสถานะ");
    return;
    }
    //==============================================

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

    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\dbPhone.mdb";//Data Source=..\\dbPhone.mdb";
    oleDbConnection = gcnew OleDbConnection(strConnection);
    oleDbConnection->Open();
    oleDbCommand = gcnew OleDbCommand("INSERT INTO tblPhone(CodePK,FirstName, " +
    "LastName,PhoneNumber) VALUES(@CodePK, " +
    "@FirstName,@LastName,@PhoneNumber)", oleDbConnection);
    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"@CodePK", SqlDbType::Int));
    oleDbCommand->Parameters["@CodePK"]->Value = txtCodePK->Text;

    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"@FirstName", SqlDbType::VarChar));
    oleDbCommand->Parameters["@FirstName"]->Value = txtFirstName->Text;

    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"@LastName", SqlDbType::VarChar));
    oleDbCommand->Parameters["@LastName"]->Value = txtLastName->Text;

    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"@PhoneNumber", SqlDbType::VarChar));
    oleDbCommand->Parameters["@PhoneNumber"]->Value = txtPhoneNumber->Text;

    //http://www.gibraltarsoftware.com/Support/VistaDB/Documentation/VistaDB.5.NET40~VistaDB.VistaDBType.html

    oleDbCommand->ExecuteNonQuery();

    oleDbConnection->Close();
    MessageBox::Show("Inserted Successfully","รายงานสถานะ");
    //=========Load Data=============
    ClearAll();
    LoadData();
    //==============จบการเพิ่มข้อมูล=========================
     
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 21 สิงหาคม 2014
  2. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    เรื่องเปลี่ยน Form1 เป็นชื่ออื่น เช่น frmMain ทำอย่างไร

    1.ดูรูปภาพประกอบโค้ดเดิม ยังไม่เปลี่ยน
     

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

    • pic20.jpg
      pic20.jpg
      ขนาดไฟล์:
      209.3 KB
      เปิดดู:
      24
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 21 สิงหาคม 2014
  3. ledphong

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

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

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

    • pic21.jpg
      pic21.jpg
      ขนาดไฟล์:
      209.6 KB
      เปิดดู:
      27
  4. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    3.คลิ๊กขวาใน frmMain.h แล้วมองหาบรรทัด
    ======================
    ~Form1()
    {
    if (components)
    {
    delete components;
    }
    }
    ===================
    ให้ทำการลบทิ้งซ่ะก่อน
     

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

    • pic22.jpg
      pic22.jpg
      ขนาดไฟล์:
      221 KB
      เปิดดู:
      19
  5. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    3.กด F5 ดูแล้วขึ้น Error อย่าพึ่งตกใจแก้ไขยังไม่เสร็จ
     

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

    • pic23.jpg
      pic23.jpg
      ขนาดไฟล์:
      285.1 KB
      เปิดดู:
      22
  6. ledphong

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

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

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

    • pic24.jpg
      pic24.jpg
      ขนาดไฟล์:
      261.6 KB
      เปิดดู:
      21
    • pic25.jpg
      pic25.jpg
      ขนาดไฟล์:
      261.6 KB
      เปิดดู:
      25
    • pic26.jpg
      pic26.jpg
      ขนาดไฟล์:
      248 KB
      เปิดดู:
      20
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 21 สิงหาคม 2014
  7. ledphong

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

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

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

    • pic27.jpg
      pic27.jpg
      ขนาดไฟล์:
      223.2 KB
      เปิดดู:
      19
  8. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    ต่อจาก Step 20 เรามาดูว่าโค้ดของเราเป็นยังไงบ้าง

    ดูโค้ดประกอบ
    ==================
    #pragma once

    namespace Phone2014 {

    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::OleDb; // ติดต่อฐานข้อมูล MS Access 2003 database

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

    String^ strConnection; // กำหนดตัวแปร DB connection string

    public:
    frmMain(void)
    {
    InitializeComponent();
    StartPosition = FormStartPosition::CenterScreen;//จัดกึ่งกลาง
    }

    protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>

    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:\\dbPhone.mdb";// หรือ Data Source=..\\dbPhone.mdb";
    }

    private: System::Windows::Forms::Button^ btnSearch;
    protected:
    private: System::Windows::Forms::TextBox^ txtSearch;
    private: System::Windows::Forms::Label^ label5;
    private: System::Windows::Forms::Button^ btnExit;
    private: System::Windows::Forms::Button^ btnRefresh;
    private: System::Windows::Forms::Button^ btnDelete;
    private: System::Windows::Forms::Button^ btnUpdate;
    private: System::Windows::Forms::Button^ btnAdd;
    private: System::Windows::Forms::Label^ lblCount;
    private: System::Windows::Forms::Button^ btnGenID;
    private: System::Windows::Forms::TextBox^ txtPhoneNumber;
    private: System::Windows::Forms::TextBox^ txtLastName;
    private: System::Windows::Forms::TextBox^ txtFirstName;
    private: System::Windows::Forms::TextBox^ txtCodePK;
    private: System::Windows::Forms::Label^ label4;
    private: System::Windows::Forms::Label^ label3;
    private: System::Windows::Forms::Label^ label2;
    private: System::Windows::Forms::Label^ label1;
    private: System::Windows::Forms::ListView^ listView1;
    private: System::Windows::Forms::ColumnHeader^ CodePK;
    private: System::Windows::Forms::ColumnHeader^ FirstName;
    private: System::Windows::Forms::ColumnHeader^ LastName;
    private: System::Windows::Forms::ColumnHeader^ PhoneNumber;

    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->btnSearch = (gcnew System::Windows::Forms::Button());
    this->txtSearch = (gcnew System::Windows::Forms::TextBox());
    this->label5 = (gcnew System::Windows::Forms::Label());
    this->btnExit = (gcnew System::Windows::Forms::Button());
    this->btnRefresh = (gcnew System::Windows::Forms::Button());
    this->btnDelete = (gcnew System::Windows::Forms::Button());
    this->btnUpdate = (gcnew System::Windows::Forms::Button());
    this->btnAdd = (gcnew System::Windows::Forms::Button());
    this->lblCount = (gcnew System::Windows::Forms::Label());
    this->btnGenID = (gcnew System::Windows::Forms::Button());
    this->txtPhoneNumber = (gcnew System::Windows::Forms::TextBox());
    this->txtLastName = (gcnew System::Windows::Forms::TextBox());
    this->txtFirstName = (gcnew System::Windows::Forms::TextBox());
    this->txtCodePK = (gcnew System::Windows::Forms::TextBox());
    this->label4 = (gcnew System::Windows::Forms::Label());
    this->label3 = (gcnew System::Windows::Forms::Label());
    this->label2 = (gcnew System::Windows::Forms::Label());
    this->label1 = (gcnew System::Windows::Forms::Label());
    this->listView1 = (gcnew System::Windows::Forms::ListView());
    this->CodePK = (gcnew System::Windows::Forms::ColumnHeader());
    this->FirstName = (gcnew System::Windows::Forms::ColumnHeader());
    this->LastName = (gcnew System::Windows::Forms::ColumnHeader());
    this->PhoneNumber = (gcnew System::Windows::Forms::ColumnHeader());
    this->SuspendLayout();
    //
    // btnSearch
    //
    this->btnSearch->Location = System::Drawing::point(555, 257);
    this->btnSearch->Name = L"btnSearch";
    this->btnSearch->Size = System::Drawing::Size(75, 23);
    this->btnSearch->TabIndex = 37;
    this->btnSearch->Text = L"ค้นหา";
    this->btnSearch->UseVisualStyleBackColor = true;
    //
    // txtSearch
    //
    this->txtSearch->Location = System::Drawing::point(483, 282);
    this->txtSearch->Name = L"txtSearch";
    this->txtSearch->Size = System::Drawing::Size(147, 20);
    this->txtSearch->TabIndex = 36;
    //
    // label5
    //
    this->label5->AutoSize = true;
    this->label5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->label5->ForeColor = System::Drawing::Color::Black;
    this->label5->Location = System::Drawing::point(480, 263);
    this->label5->Name = L"label5";
    this->label5->Size = System::Drawing::Size(46, 13);
    this->label5->TabIndex = 35;
    this->label5->Text = L"ค้นหา :";
    //
    // btnExit
    //
    this->btnExit->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->btnExit->ForeColor = System::Drawing::Color::Red;
    this->btnExit->Location = System::Drawing::point(480, 309);
    this->btnExit->Name = L"btnExit";
    this->btnExit->Size = System::Drawing::Size(75, 45);
    this->btnExit->TabIndex = 34;
    this->btnExit->Text = L"ออก";
    this->btnExit->UseVisualStyleBackColor = true;
    //
    // btnRefresh
    //
    this->btnRefresh->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->btnRefresh->ForeColor = System::Drawing::Color::Blue;
    this->btnRefresh->Location = System::Drawing::point(384, 309);
    this->btnRefresh->Name = L"btnRefresh";
    this->btnRefresh->Size = System::Drawing::Size(90, 45);
    this->btnRefresh->TabIndex = 33;
    this->btnRefresh->Text = L"รีเฟรซข้อมูล";
    this->btnRefresh->UseVisualStyleBackColor = true;
    //
    // btnDelete
    //
    this->btnDelete->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->btnDelete->ForeColor = System::Drawing::Color::Blue;
    this->btnDelete->Location = System::Drawing::point(303, 309);
    this->btnDelete->Name = L"btnDelete";
    this->btnDelete->Size = System::Drawing::Size(75, 45);
    this->btnDelete->TabIndex = 32;
    this->btnDelete->Text = L"ลบข้อมูล";
    this->btnDelete->UseVisualStyleBackColor = true;
    //
    // btnUpdate
    //
    this->btnUpdate->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->btnUpdate->ForeColor = System::Drawing::Color::Blue;
    this->btnUpdate->Location = System::Drawing::point(384, 257);
    this->btnUpdate->Name = L"btnUpdate";
    this->btnUpdate->Size = System::Drawing::Size(90, 45);
    this->btnUpdate->TabIndex = 31;
    this->btnUpdate->Text = L"แก้ไขข้อมูล";
    this->btnUpdate->UseVisualStyleBackColor = true;
    //
    // btnAdd
    //
    this->btnAdd->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->btnAdd->ForeColor = System::Drawing::Color::Blue;
    this->btnAdd->Location = System::Drawing::point(303, 257);
    this->btnAdd->Name = L"btnAdd";
    this->btnAdd->Size = System::Drawing::Size(75, 41);
    this->btnAdd->TabIndex = 30;
    this->btnAdd->Text = L"เพิ่มข้อมูล";
    this->btnAdd->UseVisualStyleBackColor = true;
    //
    // lblCount
    //
    this->lblCount->AutoSize = true;
    this->lblCount->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->lblCount->ForeColor = System::Drawing::Color::Blue;
    this->lblCount->Location = System::Drawing::point(244, 362);
    this->lblCount->Name = L"lblCount";
    this->lblCount->Size = System::Drawing::Size(53, 13);
    this->lblCount->TabIndex = 29;
    this->lblCount->Text = L"lblCount";
    //
    // btnGenID
    //
    this->btnGenID->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->btnGenID->ForeColor = System::Drawing::Color::Blue;
    this->btnGenID->Location = System::Drawing::point(191, 257);
    this->btnGenID->Name = L"btnGenID";
    this->btnGenID->Size = System::Drawing::Size(106, 25);
    this->btnGenID->TabIndex = 28;
    this->btnGenID->Text = L"กำหนดเลข Auto";
    this->btnGenID->UseVisualStyleBackColor = true;
    //
    // txtPhoneNumber
    //
    this->txtPhoneNumber->Location = System::Drawing::point(112, 334);
    this->txtPhoneNumber->Name = L"txtPhoneNumber";
    this->txtPhoneNumber->Size = System::Drawing::Size(134, 20);
    this->txtPhoneNumber->TabIndex = 27;
    //
    // txtLastName
    //
    this->txtLastName->Location = System::Drawing::point(112, 309);
    this->txtLastName->Name = L"txtLastName";
    this->txtLastName->Size = System::Drawing::Size(185, 20);
    this->txtLastName->TabIndex = 26;
    //
    // txtFirstName
    //
    this->txtFirstName->Location = System::Drawing::point(112, 285);
    this->txtFirstName->Name = L"txtFirstName";
    this->txtFirstName->Size = System::Drawing::Size(185, 20);
    this->txtFirstName->TabIndex = 25;
    //
    // txtCodePK
    //
    this->txtCodePK->Location = System::Drawing::point(112, 259);
    this->txtCodePK->Name = L"txtCodePK";
    this->txtCodePK->Size = System::Drawing::Size(73, 20);
    this->txtCodePK->TabIndex = 24;
    //
    // label4
    //
    this->label4->AutoSize = true;
    this->label4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->label4->ForeColor = System::Drawing::Color::Black;
    this->label4->Location = System::Drawing::point(16, 334);
    this->label4->Name = L"label4";
    this->label4->Size = System::Drawing::Size(90, 13);
    this->label4->TabIndex = 23;
    this->label4->Text = L"เบอร์โทรศัพท์ :";
    //
    // label3
    //
    this->label3->AutoSize = true;
    this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->label3->ForeColor = System::Drawing::Color::Black;
    this->label3->Location = System::Drawing::point(69, 309);
    this->label3->Name = L"label3";
    this->label3->Size = System::Drawing::Size(37, 13);
    this->label3->TabIndex = 22;
    this->label3->Text = L"สกุล :";
    //
    // label2
    //
    this->label2->AutoSize = true;
    this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->label2->ForeColor = System::Drawing::Color::Black;
    this->label2->Location = System::Drawing::point(76, 285);
    this->label2->Name = L"label2";
    this->label2->Size = System::Drawing::Size(30, 13);
    this->label2->TabIndex = 21;
    this->label2->Text = L"ชื่อ :";
    //
    // label1
    //
    this->label1->AutoSize = true;
    this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::point,
    static_cast<System::Byte>(222)));
    this->label1->ForeColor = System::Drawing::Color::Black;
    this->label1->Location = System::Drawing::point(54, 259);
    this->label1->Name = L"label1";
    this->label1->Size = System::Drawing::Size(52, 13);
    this->label1->TabIndex = 20;
    this->label1->Text = L"ลำดับที่ :";
    //
    // listView1
    //
    this->listView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::ColumnHeader^ >(4) {this->CodePK, this->FirstName,
    this->LastName, this->PhoneNumber});
    this->listView1->Location = System::Drawing::point(2, 2);
    this->listView1->Name = L"listView1";
    this->listView1->Size = System::Drawing::Size(630, 249);
    this->listView1->TabIndex = 19;
    this->listView1->UseCompatibleStateImageBehavior = false;
    this->listView1->View = System::Windows::Forms::View::Details;
    //
    // CodePK
    //
    this->CodePK->Text = L"ลำดับที่";
    this->CodePK->Width = 80;
    //
    // FirstName
    //
    this->FirstName->Text = L"ชื่อ";
    this->FirstName->Width = 120;
    //
    // LastName
    //
    this->LastName->Text = L"สกุล";
    this->LastName->Width = 180;
    //
    // PhoneNumber
    //
    this->PhoneNumber->Text = L"เบอร์โทรศัพท์";
    this->PhoneNumber->Width = 200;
    //
    // frmMain
    //
    this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    this->ClientSize = System::Drawing::Size(639, 386);
    this->Controls->Add(this->btnSearch);
    this->Controls->Add(this->txtSearch);
    this->Controls->Add(this->label5);
    this->Controls->Add(this->btnExit);
    this->Controls->Add(this->btnRefresh);
    this->Controls->Add(this->btnDelete);
    this->Controls->Add(this->btnUpdate);
    this->Controls->Add(this->btnAdd);
    this->Controls->Add(this->lblCount);
    this->Controls->Add(this->btnGenID);
    this->Controls->Add(this->txtPhoneNumber);
    this->Controls->Add(this->txtLastName);
    this->Controls->Add(this->txtFirstName);
    this->Controls->Add(this->txtCodePK);
    this->Controls->Add(this->label4);
    this->Controls->Add(this->label3);
    this->Controls->Add(this->label2);
    this->Controls->Add(this->label1);
    this->Controls->Add(this->listView1);
    this->Name = L"frmMain";
    this->Text = L"โปรแกรมเบอร์โทรศัพท์ Phone Number V.2014 ( C++/CLI , Access 2003 , listView )";
    this->Load += gcnew System::EventHandler(this, &frmMain::frmMain_Load);
    this->ResumeLayout(false);
    this->PerformLayout();

    }
    #pragma endregion
    private: System::Void btnGenID_Click(System::Object^ sender, System::EventArgs^ e) {
    //============ปุ่ม GenID====================
    OleDbConnection^ oleDbConnection;
    OleDbCommand ^ oleDbCommand;

    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\dbPhone.mdb";//Data Source=..\\dbPhone.mdb";
    oleDbConnection = gcnew OleDbConnection(strConnection);
    oleDbConnection->Open();
    oleDbCommand = gcnew OleDbCommand("SELECT Count(CodePK)+1 FROM tblPhone", oleDbConnection);
    Object ^MaxID = oleDbCommand->ExecuteScalar();
    txtCodePK->Text = MaxID->ToString();//ค่าสูงสุด
    // txtCodeID->Text = MaxID->ToString()->PadLeft(7, '0'); // กำหนดเลข เช่น 0000001,0000002,0000003....
    }

    private :
    void LoadData(){
    //=============Load Data=====================
    OleDbConnection^ oleDbConnection;
    OleDbCommand ^ oleDbCommand;
    OleDbDataReader^ oleDbDataReader;
    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\dbPhone.mdb";//Data Source=..\\dbPhone.mdb";
    try{
    String^ strQuery = "SELECT CodePK, FirstName, LastName, PhoneNumber FROM tblPhone ";
    oleDbConnection = gcnew OleDbConnection(strConnection);
    oleDbConnection->Open();
    oleDbCommand = gcnew OleDbCommand(strQuery, oleDbConnection);
    oleDbDataReader = oleDbCommand->ExecuteReader();

    while(oleDbDataReader->Read()){
    ListViewItem^ item = gcnew ListViewItem(oleDbDataReader["CodePK"]->ToString());
    item->SubItems->Add(oleDbDataReader["FirstName"]->ToString());
    item->SubItems->Add(oleDbDataReader["LastName"]->ToString());
    item->SubItems->Add(oleDbDataReader["PhoneNumber"]->ToString());
    listView1->Items->Add(item);
    }
    oleDbDataReader->Close();
    }
    catch(Exception^ e){
    MessageBox::Show(e->Message, "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
    return;
    }
    finally{
    oleDbConnection->Close();
    if(oleDbDataReader != nullptr){
    oleDbDataReader->Close();
    oleDbDataReader = nullptr;
    }
    if(oleDbCommand != nullptr){
    oleDbCommand = nullptr;
    }
    }
    }

    private: System::Void frmMain_Load(System::Object^ sender, System::EventArgs^ e) {
    //=============Load Data==========
    LoadData();// ทำการโหลดข้อมูลตอนเปิดโปรแกรมครั้งแรก
    //================================
    }
    };
    }
     
  9. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 22
    เอาเม้าส์ไปคลิ๊กที่ตาราง listView1 แล้วมองหา GridLines=True
    =========================
     

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

    • pic28.jpg
      pic28.jpg
      ขนาดไฟล์:
      273.4 KB
      เปิดดู:
      21
  10. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 23
    ทำคำสั่ง ClearAll() ไว้ซ่ะก่อน
    ======================
    private:
    void ClearAll(){
    //====Clear Text Box=======
    txtCodePK->Text = "";
    txtFirstName->Text = "";
    txtLastName->Text = "";
    txtPhoneNumber->Text = "";
    listView1->Items->Clear();
    }
     

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

    • pic29.jpg
      pic29.jpg
      ขนาดไฟล์:
      267.5 KB
      เปิดดู:
      24
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 21 สิงหาคม 2014
  11. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 24
    กำหนดปุ่มเลข Auto
    =============================
    //============ปุ่ม GenID====================
    OleDbConnection^ oleDbConnection;
    OleDbCommand ^ oleDbCommand;

    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\dbPhone.mdb";//Data Source=..\\dbPhone.mdb";
    oleDbConnection = gcnew OleDbConnection(strConnection);
    oleDbConnection->Open();
    oleDbCommand = gcnew OleDbCommand("SELECT Count(CodePK)+1 FROM tblPhone", oleDbConnection);
    Object ^MaxID = oleDbCommand->ExecuteScalar();
    txtCodePK->Text = MaxID->ToString();//ค่าสูงสุด
    // txtCodeID->Text = MaxID->ToString()->PadLeft(7, '0'); // กำหนดเลข เช่น 0000001,0000002,0000003....
     

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

    • pic30.jpg
      pic30.jpg
      ขนาดไฟล์:
      317 KB
      เปิดดู:
      20
  12. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 25
    กำหนดปุ่มเลข Auto ดู
    ===================
    ว่า OK ไหมครับ
     

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

    • pic31.jpg
      pic31.jpg
      ขนาดไฟล์:
      291.9 KB
      เปิดดู:
      15
  13. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 26
    ปุ่มเพิ่มข้อมูล
    ======================
    //===========ปุ่มเพิ่มข้อมูล=========================
    //=======ตรวจสอบการกดปุ่มก่อนครับ===================
    if (String::IsNullOrEmpty(txtCodePK->Text)){
    MessageBox::Show("กรุณากดปุ่ม GenID ก่อนครับ","รายงานสถานะ");
    return;
    }
    //=============================================
    if (String::IsNullOrEmpty(txtFirstName->Text)){
    MessageBox::Show("กรุณาพิมพ์ชื่อก่อนครับ","รายงานสถานะ");
    return;
    }
    //==============================================
    if (String::IsNullOrEmpty(txtLastName->Text)){
    MessageBox::Show("กรุณาพิมพ์สกุลก่อนครับ","รายงานสถานะ");
    return;
    }
    //=============================
    OleDbConnection^ oleDbConnection;
    OleDbCommand ^ oleDbCommand;

    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\dbPhone.mdb";//Data Source=..\\dbPhone.mdb";
    oleDbConnection = gcnew OleDbConnection(strConnection);
    oleDbConnection->Open();
    oleDbCommand = gcnew OleDbCommand("INSERT INTO tblPhone(CodePK,FirstName, " +
    "LastName,PhoneNumber) VALUES(@CodePK, " +
    "@FirstName,@LastName,@PhoneNumber)", oleDbConnection);
    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"@CodePK", SqlDbType::Int));
    oleDbCommand->Parameters["@CodePK"]->Value = txtCodePK->Text;

    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"@FirstName", SqlDbType::VarChar));
    oleDbCommand->Parameters["@FirstName"]->Value = txtFirstName->Text;

    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"@LastName", SqlDbType::VarChar));
    oleDbCommand->Parameters["@LastName"]->Value = txtLastName->Text;

    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"@PhoneNumber", SqlDbType::VarChar));
    oleDbCommand->Parameters["@PhoneNumber"]->Value = txtPhoneNumber->Text;

    //http://www.gibraltarsoftware.com/Support/VistaDB/Documentation/VistaDB.5.NET40~VistaDB.VistaDBType.html

    oleDbCommand->ExecuteNonQuery();

    oleDbConnection->Close();
    MessageBox::Show("Inserted Successfully","รายงานสถานะ");
    //=========Load Data=============
    ClearAll();//ลบข้อมูลจาก TextBox หลังการบันทึกข้อมูล
    LoadData();// โหลดข้อมูลมาโชว์
    //==============================
     
  14. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 27
    เพิ่มการนับแถวในตาราง ใช้ตัวแปร lblC
    ====================
    //========lblCount==Use ExecuteScalar========
    oleDbCommand = gcnew OleDbCommand("SELECT Count(CodePK) FROM tblPhone", oleDbConnection);
    Object ^lblC = oleDbCommand->ExecuteScalar();//กำหนด ตัวแปร ใน ExecuteScalar คือ lblC
    //lblC เพื่อเก็บ ข้อมูลในตารางว่ามีกี่แถวแล้ว
    lblCount->Text = "[ จำนวน : " + lblC->ToString() + " คน ]";//นับจำนวน lblCount (คน)
    //=======================
     

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

    • pic32.jpg
      pic32.jpg
      ขนาดไฟล์:
      326 KB
      เปิดดู:
      21
    • pic33.jpg
      pic33.jpg
      ขนาดไฟล์:
      309 KB
      เปิดดู:
      21
  15. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 28
    ปุ่ม Refresh
    ========================
    //==========Refresh TextBox=======
    txtCodePK->Text = "";
    txtFirstName->Text = "";
    txtLastName->Text = "";
    txtPhoneNumber->Text = "";
    btnAdd->Enabled = true;//ปุ่ม Add ใช้ได้
    btnGenID->Enabled = true;//ปุ่ม GenID ใช้ได้
    //============================
     

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

    • pic34.jpg
      pic34.jpg
      ขนาดไฟล์:
      294.8 KB
      เปิดดู:
      21
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 21 สิงหาคม 2014
  16. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 29
    ปุ่มแก้ไขข้อมูล
    =========================
    หากเรา Double Click ที่ listView1 ให้ข้อมูลแถวที่เราเลือกมาปรากฎใน TextBox
    =========================
    //==============Mouse Double Click=========
    txtCodePK->Text = listView1->SelectedItems[0]->SubItems[0]->Text;
    txtFirstName->Text = listView1->SelectedItems[0]->SubItems[1]->Text;
    txtLastName->Text = listView1->SelectedItems[0]->SubItems[2]->Text;
    txtPhoneNumber->Text = listView1->SelectedItems[0]->SubItems[3]->Text;
    btnAdd->Enabled = false;//ปุ่ม Add เลือนลาง
    btnGenID->Enabled = false;//ปุ่ม GenID เลือนลาง
    //==============================
     

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

    • pic35.jpg
      pic35.jpg
      ขนาดไฟล์:
      272.7 KB
      เปิดดู:
      19
    • pic36.jpg
      pic36.jpg
      ขนาดไฟล์:
      265.3 KB
      เปิดดู:
      23
    • pic37.jpg
      pic37.jpg
      ขนาดไฟล์:
      273 KB
      เปิดดู:
      22
    • pic38.jpg
      pic38.jpg
      ขนาดไฟล์:
      310.9 KB
      เปิดดู:
      21
    • pic40.jpg
      pic40.jpg
      ขนาดไฟล์:
      335.5 KB
      เปิดดู:
      22
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 21 สิงหาคม 2014
  17. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 30
    ทำการแก้ไขใน TextBox แล้วต่อมาค่อยมากดปุ่มแก้ไขข้อมูล
    ==============================
    Double Click ที่ปุ่มแก้ไขข้อมูล
    ==============================

    //=============Edit Data===================================================
    if (String::IsNullOrEmpty(txtCodePK->Text)){
    MessageBox::Show("ท่านยังไม่ได้เลือกแถวข้อมูลที่จะอัฟเดท!","รายงานสถานะ");
    return;
    }
    // OLE DB:
    OleDbConnection^ oleDbConnection;
    OleDbCommand ^ oleDbCommand;

    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\dbPhone.mdb";//Data Source=..\\dbPhone.mdb";
    oleDbConnection = gcnew OleDbConnection(strConnection);
    oleDbConnection->Open();
    oleDbCommand = gcnew OleDbCommand("Update tblPhone set " +
    "CodePK=@CodePK,FirstName=@FirstName, " +
    "LastName=@LastName ,PhoneNumber=@PhoneNumber " +
    "Where CodePK=" + txtCodePK->Text , oleDbConnection);
    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"CodePK", SqlDbType::Int));
    oleDbCommand->Parameters["CodePK"]->Value = txtCodePK->Text;

    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"FirstName", SqlDbType::VarChar));
    oleDbCommand->Parameters["FirstName"]->Value = txtFirstName->Text;

    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"LastName", SqlDbType::VarChar));
    oleDbCommand->Parameters["LastName"]->Value = txtLastName->Text;

    oleDbCommand->Parameters->Add(gcnew OleDbParameter(L"PhoneNumber", SqlDbType::VarChar));
    oleDbCommand->Parameters["PhoneNumber"]->Value = txtPhoneNumber->Text;

    oleDbCommand->ExecuteNonQuery();

    oleDbConnection->Close();
    MessageBox::Show("Update Successfully","รายงานสถานะ");
    //=========Load Data=============
    ClearAll();//ลบข้อมูลจาก TextBox หลังการบันทึกข้อมูล
    LoadData();// โหลดข้อมูลมาโชว์
    //===============================
     

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

    • pic39.jpg
      pic39.jpg
      ขนาดไฟล์:
      268 KB
      เปิดดู:
      20
    • pic41.jpg
      pic41.jpg
      ขนาดไฟล์:
      348.5 KB
      เปิดดู:
      28
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 21 สิงหาคม 2014
  18. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 31
    ปุ่มลบข้อมูล
    ใช้หลักการดับเบิ้ลคลิ๊กให้ข้อมูลมาปรากฎใน TextBox แล้วมากดปุ่มลบข้อมูล
    =======================
    //============Delete===========
    if (String::IsNullOrEmpty(txtCodePK->Text)){
    MessageBox::Show("ท่านยังไม่ได้เลือกข้อมูลที่จะลบ!","ยืนยันการลบ");
    return;
    }
    //=============================
    OleDbConnection^ oleDbConnection;
    OleDbCommand ^ oleDbCommand;
    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\dbPhone.mdb";//Data Source=..\\dbPhone.mdb";
    oleDbConnection = gcnew OleDbConnection(strConnection);
    oleDbConnection->Open();
    oleDbCommand = gcnew OleDbCommand("DELETE FROM tblPhone WHERE CodePK = " + txtCodePK->Text , oleDbConnection);
    oleDbCommand->ExecuteNonQuery();
    //=========Load Data=============
    ClearAll();//ลบข้อมูลจาก TextBox หลังการบันทึกข้อมูล
    LoadData();// โหลดข้อมูลมาโชว์
    //===============================
     

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

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

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 32
    ปุ่ม Exit หรือ จบโปรแกรม หรือ ออก
    =================
    Close();
     

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

    • pic43.jpg
      pic43.jpg
      ขนาดไฟล์:
      281.3 KB
      เปิดดู:
      20
  20. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    Step 33
    ปุ่มค้นหาข้อมูล
    =======================
    อาศัยคำสั่ง SQL Query ได้หลากหลาย
    เช่น sql = "SELECT * FROM tblPhone WHERE FirstName = " + txtSearch.Text;// ค้นหาชื่อ
    =======================
    OleDbConnection^ oleDbConnection;
    OleDbCommand ^ oleDbCommand;
    OleDbDataReader^ oleDbDataReader;
    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\dbPhone.mdb";//Data Source=..\\dbPhone.mdb";
    try{
    String^ strQuery = "SELECT CodePK, FirstName, LastName, PhoneNumber " +
    "FROM tblPhone " +
    "Where (FirstName Like '%" + txtSearch->Text + "%')";
    // "ORDER BY [CodePK] ASC";
    oleDbConnection = gcnew OleDbConnection(strConnection);
    oleDbConnection->Open();
    oleDbCommand = gcnew OleDbCommand(strQuery, oleDbConnection);
    oleDbDataReader = oleDbCommand->ExecuteReader();

    while(oleDbDataReader->Read()){
    ListViewItem^ item = gcnew ListViewItem(oleDbDataReader["CodePK"]->ToString());
    item->SubItems->Add(oleDbDataReader["FirstName"]->ToString());
    item->SubItems->Add(oleDbDataReader["LastName"]->ToString());
    item->SubItems->Add(oleDbDataReader["PhoneNumber"]->ToString());
    listView1->Items->Add(item);
    }
    //===========lblCount==Use ExecuteScalar==================================
    oleDbCommand = gcnew OleDbCommand("SELECT Count(CodePK) FROM tblPhone ", oleDbConnection);
    Object ^lblC = oleDbCommand->ExecuteScalar();//กำหนด ตัวแปร ใน ExecuteScalar คือ lblC
    //lblC เพื่อเก็บ ข้อมูลในตารางว่ามีกี่แถวแล้ว
    lblCount->Text = "[ จำนวน : " + lblC->ToString() + " คน ]";//นับจำนวน lblCount (คน)
    //=========================================================================
    oleDbDataReader->Close();
    }
    catch(Exception^ e){
    MessageBox::Show(e->Message, "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
    return;
    }
    finally{
    oleDbConnection->Close();
    if(oleDbDataReader != nullptr){
    oleDbDataReader->Close();
    oleDbDataReader = nullptr;
    }
    if(oleDbCommand != nullptr){
    oleDbCommand = nullptr;
    }
    }
    //===================

    //======================================
    ในส่วนค้นหาข้อมูล
    ผมทำมีบักเอาไว้ให้คิดต่อยอดเอาเองนะครับจะได้เป็นไงครับ
     

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

    • pic44.jpg
      pic44.jpg
      ขนาดไฟล์:
      278.1 KB
      เปิดดู:
      23
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 21 สิงหาคม 2014

แชร์หน้านี้

Loading...