using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
public partial class frmMain : Form
{
// By https://www.thaicreate.com (mr.win)//
public frmMain()
{
InitializeComponent();
}
private void btnHome_Click(object sender, EventArgs e)
{
this.Hide();
frmHome f = new frmHome();
f.Show();
}
private void btnExit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure to exit?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
Application.Exit();
}
}
}
}
frmHome
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Data.SqlTypes;
namespace WindowsFormsApplication
{
public partial class frmHome : Form
{
// By https://www.thaicreate.com (mr.win)//
public frmHome()
{
InitializeComponent();
}
private void frmHome_Load(object sender, EventArgs e)
{
BindDataGrid();
}
private void BindDataGrid()
{
SqlCeConnection myConnection = default(SqlCeConnection);
DataTable dt = new DataTable();
SqlCeDataAdapter Adapter = default(SqlCeDataAdapter);
//myConnection = new SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\\AppDatabase1.sdf;"));
myConnection = new SqlCeConnection("Data Source =C:\\WindowsFormsApplication\\WindowsFormsApplication\\Database1.sdf;");
myConnection.Open();
SqlCeCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "SELECT [id], [name], [email] FROM [mytable]";
myCommand.CommandType = CommandType.Text;
Adapter = new SqlCeDataAdapter(myCommand);
Adapter.Fill(dt);
myConnection.Close();
this.dgName.DataSource = dt;
this.dgName.Columns.Clear();
DataGridViewTextBoxColumn column;
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "id";
column.HeaderText = "ID";
column.Width = 50;
this.dgName.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "name";
column.HeaderText = "Name";
column.Width = 100;
this.dgName.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "email";
column.HeaderText = "Email";
column.Width = 150;
this.dgName.Columns.Add(column);
dt = null;
}
private void btnAdd_Click(object sender, EventArgs e)
{
this.Hide();
frmAdd f = new frmAdd();
f.Show();
}
private void btnEdit_Click(object sender, EventArgs e)
{
this.Hide();
frmEdit f = new frmEdit();
f._strID = this.dgName[0, this.dgName.CurrentCell.RowIndex].Value.ToString();
f.Show();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Hide();
frmMain f = new frmMain();
f.Show();
}
private void btnDel_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure to delete?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
string strID = this.dgName[0, this.dgName.CurrentCell.RowIndex].Value.ToString();
SqlCeConnection myConnection = default(SqlCeConnection);
//myConnection = new SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\\AppDatabase1.sdf;"));
myConnection = new SqlCeConnection("Data Source =C:\\WindowsFormsApplication\\WindowsFormsApplication\\Database1.sdf;");
myConnection.Open();
SqlCeCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "DELETE FROM [mytable] WHERE id = '" + strID + "'";
myCommand.CommandType = CommandType.Text;
myCommand.ExecuteNonQuery();
myConnection.Close();
MessageBox.Show("Delete Successfully");
BindDataGrid();
}
}
}
}
frmAdd
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Data.SqlTypes;
namespace WindowsFormsApplication
{
public partial class frmAdd : Form
{
// By https://www.thaicreate.com (mr.win)//
public frmAdd()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.txtName.Text))
{
MessageBox.Show("Please input (Name)");
this.txtName.Focus();
return;
}
if (string.IsNullOrEmpty(this.txtEmail.Text))
{
MessageBox.Show("Please input (Email)");
this.txtEmail.Focus();
return;
}
SqlCeConnection myConnection = default(SqlCeConnection);
//myConnection = new SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\\AppDatabase1.sdf;"));
myConnection = new SqlCeConnection("Data Source =C:\\WindowsFormsApplication\\WindowsFormsApplication\\Database1.sdf;");
myConnection.Open();
SqlCeCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "INSERT INTO [mytable] ([name], [email]) VALUES " + " ('" + this.txtName.Text + "','" + this.txtEmail.Text + "' ) ";
myCommand.CommandType = CommandType.Text;
myCommand.ExecuteNonQuery();
myConnection.Close();
MessageBox.Show("Save Successfully");
this.Hide();
frmHome f = new frmHome();
f.Show();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Hide();
frmHome f = new frmHome();
f.Show();
}
}
}
frmEdit
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Data.SqlTypes;
namespace WindowsFormsApplication
{
public partial class frmEdit : Form
{
// By https://www.thaicreate.com (mr.win)//
public frmEdit()
{
InitializeComponent();
}
string strID = "";
public string _strID
{
get { return strID; }
set { strID = value; }
}
private void frmEdit_Load(object sender, EventArgs e)
{
SqlCeConnection myConnection = default(SqlCeConnection);
DataTable dt = new DataTable();
SqlCeDataAdapter Adapter = default(SqlCeDataAdapter);
//myConnection = new SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\\AppDatabase1.sdf;"));
myConnection = new SqlCeConnection("Data Source =C:\\WindowsFormsApplication\\WindowsFormsApplication\\Database1.sdf;");
myConnection.Open();
SqlCeCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "SELECT [id], [name], [email] FROM [mytable] WHERE id = '" + strID + "' ";
myCommand.CommandType = CommandType.Text;
Adapter = new SqlCeDataAdapter(myCommand);
Adapter.Fill(dt);
myConnection.Close();
if (dt.Rows.Count > 0)
{
this.txtName.Text = dt.Rows[0]["name"].ToString();
this.txtEmail.Text = dt.Rows[0]["email"].ToString();
}
dt = null;
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.txtName.Text))
{
MessageBox.Show("Please input (Name)");
this.txtName.Focus();
return;
}
if (string.IsNullOrEmpty(this.txtEmail.Text))
{
MessageBox.Show("Please input (Email)");
this.txtEmail.Focus();
return;
}
SqlCeConnection myConnection = default(SqlCeConnection);
//myConnection = new SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) + "\\AppDatabase1.sdf;"));
myConnection = new SqlCeConnection("Data Source =C:\\WindowsFormsApplication\\WindowsFormsApplication\\Database1.sdf;");
myConnection.Open();
SqlCeCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "UPDATE [mytable] SET " + " [name] = '" + this.txtName.Text + "', [email] = '" + this.txtEmail.Text + "' " + " WHERE id = '" + strID + "' ";
myCommand.CommandType = CommandType.Text;
myCommand.ExecuteNonQuery();
myConnection.Close();
MessageBox.Show("Update Successfully");
this.Hide();
frmHome f = new frmHome();
f.Show();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Hide();
frmHome f = new frmHome();
f.Show();
}
}
}
ทดสอบการรันโปรแกรม
โดยคลิกที่ Startup Debugging
หน้าจอหลัก
หน้า Home ซึ่งใช้ DataGridView เพื่อดึงข้อมูลออกมาแสดง