一、語法、程式碼大小寫:這點算是所有學程式的人公認、切身體會過的痛點,有時在打一行程式碼,你的軟體會自動校正某些區段的程式,然而那個程式不見得是正確的程式,因此增加你要去透過翻閱書目、教學影片、詢問專業的人等多種管道校正這個區段,並完成這項作業。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 會員資料庫建置
{
public partial class 會員表單 : Form
{
public 會員表單()
{
InitializeComponent();
}
private void dataGridView_KeyDown(object sender, KeyEventArgs e)
{
if (MessageBox.Show("Are you sure want to delete this record?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
會員資料庫BindingSource.RemoveCurrent();
}
private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
{
if (string.IsNullOrEmpty(txtSearch.Text))
dataGridView.DataSource = 會員資料庫BindingSource;
else
{
var query = from o in this.會員資料庫_DataSet.會員資料庫
where o.姓名.Contains(txtSearch.Text) || o.電話號碼 == txtSearch.Text || o.信箱 == txtSearch.Text || o.地址.Contains(txtSearch.Text)
select o;
dataGridView.DataSource = query.ToList();
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg*", ValidateNames = true, Multiselect = false })
{
if (ofd.ShowDialog() == DialogResult.OK)
pictureBox1.Image = Image.FromFile(ofd.FileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void New_Click(object sender, EventArgs e)
{
try
{
panel.Enabled = true;
txtName.Focus();
this.會員資料庫_DataSet.會員資料庫.Add會員資料庫Row(this.會員資料庫_DataSet.會員資料庫.New會員資料庫Row());
會員資料庫BindingSource.MoveLast();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
會員資料庫BindingSource.ResetBindings(false);
}
}
private void Edit_Click(object sender, EventArgs e)
{
panel.Enabled = true;
txtName.Focus();
}
private void Cancel_Click(object sender, EventArgs e)
{
panel.Enabled = true;
txtName.Focus();
}
private void Save_Click(object sender, EventArgs e)
{
try
{
會員資料庫BindingSource.EndEdit();
panel.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
會員資料庫BindingSource.ResetBindings(false);
panel.Enabled = false;
}
}
private void 會員表單_Load(object sender, EventArgs e)
{
// TODO: 這行程式碼會將資料載入 '會員資料庫_DataSet.會員資料庫' 資料表。您可以視需要進行移動或移除。
this.會員資料庫TableAdapter1.Fill(this.會員資料庫_DataSet.會員資料庫);
會員資料庫BindingSource.DataSource = this.會員資料庫_DataSet.會員資料庫;
}
}
}
【影片名稱】:[color=var(--ytd-video-primary-info-renderer-title-color, var(--yt-primary-text-color))]C# Application - Insert Delete Update Select in MS Access Database | FoxLearn