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

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

  1. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    เฉลย Step 33
    =======================
     

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

    • pic46.jpg
      pic46.jpg
      ขนาดไฟล์:
      255.1 KB
      เปิดดู:
      22
    • pic45.jpg
      pic45.jpg
      ขนาดไฟล์:
      318.5 KB
      เปิดดู:
      29
  2. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    ดูโค้ดตั้งแต่ต้นจนจบ ของโปรแกรม Phone Number 2014

    ของ frmMain.h
    ======================
    #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;
    this->btnSearch->Click += gcnew System::EventHandler(this, &frmMain::btnSearch_Click);
    //
    // 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;
    this->btnExit->Click += gcnew System::EventHandler(this, &frmMain::btnExit_Click);
    //
    // 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;
    this->btnRefresh->Click += gcnew System::EventHandler(this, &frmMain::btnRefresh_Click);
    //
    // 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;
    this->btnDelete->Click += gcnew System::EventHandler(this, &frmMain::btnDelete_Click);
    //
    // 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;
    this->btnUpdate->Click += gcnew System::EventHandler(this, &frmMain::btnUpdate_Click);
    //
    // 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;
    this->btnAdd->Click += gcnew System::EventHandler(this, &frmMain::btnAdd_Click);
    //
    // 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;
    this->btnGenID->Click += gcnew System::EventHandler(this, &frmMain::btnGenID_Click);
    //
    // 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->FullRowSelect = true;
    this->listView1->GridLines = true;
    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;
    this->listView1->MouseDoubleClick += gcnew System::Windows::Forms::MouseEventHandler(this, &frmMain::listView1_MouseDoubleClick);
    //
    // 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 " +
    "Where " + //เพิ่มมาใหม่
    "(FirstName Like '%" + txtSearch->Text + "%')";//เพิ่มมาใหม่
    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 " +
    "Where " + //เพิ่มมาใหม่
    "(FirstName Like '%" + txtSearch->Text + "%')" + //เพิ่มมาใหม่
    "", 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;
    }
    }
    }

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

    private:
    void ClearAll(){
    //====Clear Text Box=======
    txtCodePK->Text = "";
    txtFirstName->Text = "";
    txtLastName->Text = "";
    txtPhoneNumber->Text = "";
    listView1->Items->Clear();
    }
    private: System::Void btnAdd_Click(System::Object^ sender, System::EventArgs^ e) {
    //===========ปุ่มเพิ่มข้อมูล=========================
    //=======ตรวจสอบการกดปุ่มก่อนครับ===================
    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();// โหลดข้อมูลมาโชว์
    //==============================================
    }
    private: System::Void btnRefresh_Click(System::Object^ sender, System::EventArgs^ e) {
    //==========Refresh TextBox=======
    txtCodePK->Text = "";
    txtFirstName->Text = "";
    txtLastName->Text = "";
    txtPhoneNumber->Text = "";
    btnAdd->Enabled = true;//ปุ่ม Add ใช้ได้
    btnGenID->Enabled = true;//ปุ่ม GenID ใช้ได้
    //================================
    }
    private: System::Void listView1_MouseDoubleClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
    //==============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 เลือนลาง
    //==========================================
    }
    private: System::Void btnUpdate_Click(System::Object^ sender, System::EventArgs^ e) {
    //=============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();// โหลดข้อมูลมาโชว์
    //===============================
    }
    private: System::Void btnDelete_Click(System::Object^ sender, System::EventArgs^ e) {
    //============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();// โหลดข้อมูลมาโชว์
    //===============================
    }
    private: System::Void btnExit_Click(System::Object^ sender, System::EventArgs^ e) {
    Close();
    }
    private: System::Void btnSearch_Click(System::Object^ sender, System::EventArgs^ e) {
    //==========Search Text=======
    ClearAll();
    LoadData();
    //============================
    }
    };
    }
     
  3. ledphong

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

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

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

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

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

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

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

    • pic48.jpg
      pic48.jpg
      ขนาดไฟล์:
      98.8 KB
      เปิดดู:
      22
    • pic49.jpg
      pic49.jpg
      ขนาดไฟล์:
      265.9 KB
      เปิดดู:
      25
    • pic50.jpg
      pic50.jpg
      ขนาดไฟล์:
      155.8 KB
      เปิดดู:
      26
    • pic51.jpg
      pic51.jpg
      ขนาดไฟล์:
      273.5 KB
      เปิดดู:
      31
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 21 สิงหาคม 2014
  5. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    พอ Save Program เสร็จ กด F5 ดูว่าได้ตามรูปหรือไม่
     

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

    • pic52.jpg
      pic52.jpg
      ขนาดไฟล์:
      153.3 KB
      เปิดดู:
      25
  6. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    3.ต้องการ รวม หรือ Sum Columns จะใช้คำสั่งไหนดี
    ===========================
    กรณีเช่น เงินเดือน
    ===========================
    //=================================
    oleDbCommand = gcnew OleDbCommand("SELECT SUM(Salary) FROM tblAccess", oleDbConnection);
    Object ^lblD = oleDbCommand->ExecuteScalar();

    lblTotalMoney->Text = "[ รวมเงิน : " + lblD->ToString() + " บาท ]";//รวมเงินทั้งหมด (บาท)
    //==========================

    หรือ

    for each {
    .......
    }
     
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 21 สิงหาคม 2014
  7. ledphong

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

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

    1.ปัญหาเรื่อง Date Time ต้องระมัดระวังเป็นพิเศษ บางคนบอกว่าอ้าวทำไมหรือเขาเขียนโปรแกรมมาแบบนี่ เขาให้ลอกก็เป็นบุญแล้ว ไม่ไม่ไม่เลยครับ
    =================================
    เช่น VB 6.0
    =================================
     

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

    • pic53.jpg
      pic53.jpg
      ขนาดไฟล์:
      243.7 KB
      เปิดดู:
      32
    • pic54.jpg
      pic54.jpg
      ขนาดไฟล์:
      299 KB
      เปิดดู:
      26
    แก้ไขครั้งล่าสุดโดยผู้ดูแล: 21 สิงหาคม 2014
  8. ledphong

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

    วันที่สมัครสมาชิก:
    28 มีนาคม 2009
    โพสต์:
    1,425
    ค่าพลัง:
    +165
    1.ปัญหาเรื่อง Date Time ต้องระมัดระวังเป็นพิเศษ บางคนบอกว่าอ้าวทำไมหรือเขาเขียนโปรแกรมมาแบบนี่ เขาให้ลอกก็เป็นบุญแล้ว ไม่ไม่ไม่เลยครับ
    =================================
    เช่น C#.NET
    =================================
    string dt = Birthdate.Value.Year + "/" + Birthdate.Value.Month + "/" + Birthdate.Value.Day;

    command.Parameters.AddWithValue("Birthdate", dt);
     

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

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

แชร์หน้านี้

Loading...