01.
using
System;
02.
using
System.Linq;
03.
using
System.Collections.Generic;
04.
using
System.ComponentModel;
05.
using
System.Data;
06.
using
System.Drawing;
07.
using
System.Text;
08.
using
System.Windows.Forms;
09.
using
System.Data.SqlServerCe;
10.
using
System.Data.SqlTypes;
11.
12.
namespace
WindowsFormsApplication
13.
{
14.
public
partial
class
frmAdd : Form
15.
{
16.
17.
public
frmAdd()
18.
{
19.
InitializeComponent();
20.
}
21.
22.
23.
24.
private
void
btnAdd_Click(
object
sender, EventArgs e)
25.
{
26.
if
(
string
.IsNullOrEmpty(
this
.txtID.Text))
27.
{
28.
MessageBox.Show(
"Please input (ID)"
);
29.
this
.txtID.Focus();
30.
return
;
31.
}
32.
33.
if
(
string
.IsNullOrEmpty(
this
.txtName.Text))
34.
{
35.
MessageBox.Show(
"Please input (Name)"
);
36.
this
.txtName.Focus();
37.
return
;
38.
}
39.
40.
if
(
string
.IsNullOrEmpty(
this
.txtUnit.Text))
41.
{
42.
MessageBox.Show(
"Please input (Unit)"
);
43.
this
.txtUnit.Focus();
44.
return
;
45.
}
46.
47.
if
(
string
.IsNullOrEmpty(
this
.txtLocation.Text))
48.
{
49.
MessageBox.Show(
"Please input (Location)"
);
50.
this
.txtLocation.Focus();
51.
return
;
52.
}
53.
54.
if
(
string
.IsNullOrEmpty(
this
.txtDate.Text))
55.
{
56.
MessageBox.Show(
"Please input (Date)"
);
57.
this
.txtDate.Focus();
58.
return
;
59.
}
60.
61.
SqlCeConnection myConnection =
default
(SqlCeConnection);
62.
63.
myConnection =
new
SqlCeConnection(
"Data Source =C:\\Users\\Nick\\Documents\\Visual Studio 2010\\Projects\\Present\\KIKI\\WindowsFormsApplication\\Database1.sdf;"
);
64.
myConnection.Open();
65.
SqlCeCommand myCommand = myConnection.CreateCommand();
66.
myCommand.CommandText =
"INSERT INTO [mytable] ([ID], [Name], [Unit], [Location], [Date]) VALUES "
+
" ('"
+
this
.txtID.Text +
"','"
+
this
.txtName.Text +
"','"
+
this
.txtUnit.Text +
"','"
+
this
.txtLocation.Text +
"','"
+
this
.txtDate.Text +
"') "
;
67.
myCommand.CommandType = CommandType.Text;
68.
myCommand.ExecuteNonQuery();
69.
myConnection.Close();
70.
71.
MessageBox.Show(
"Save Successfully"
);
72.
73.
this
.Hide();
74.
frmHome f =
new
frmHome();
75.
f.Show();
76.
}
77.
78.
private
void
btnExit_Click(
object
sender, EventArgs e)
79.
{
80.
this
.Close();
81.
}
82.
83.
private
void
frmAdd_Load(
object
sender, EventArgs e)
84.
{
85.
86.
}
87.
88.
}
89.
}