001.
private
void
check()
002.
003.
{
004.
005.
006.
007.
for
(
int
i = 0; i <= da.Tables[
"Program"
].Rows.Count-1 ; i++)
008.
009.
{
010.
011.
012.
if
(da.Tables[
"Program"
].Rows[i][
"NameProgram"
].ToString().Equals(txtNameProgram.Text.Trim()))
013.
014.
{
015.
016.
MessageBox.Show(
"คุณป้อนข้อมูลซ้ำ !!! กรุณาป้อนข้อมูลใหม่"
,
"ผลการตรวจสอบ"
, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
017.
018.
txtID.Text =
""
;
019.
020.
txtID.Focus();
021.
022.
txtNameProgram.Text =
""
;
023.
024.
txtNameProcess.Text =
""
;
025.
026.
}
027.
028.
}
029.
030.
}
031.
032.
private
void
Add_Click(
object
sender, EventArgs e)
033.
034.
{
035.
036.
if
(txtID.Text.Trim() ==
""
|| txtNameProgram.Text.Trim() ==
""
|| txtNameProcess.Text.Trim() ==
""
)
037.
038.
{
039.
040.
MessageBox.Show(
"กรุณากรอกข้อมูลให้ครบ !!!!!"
,
"Error"
, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
041.
042.
txtID.Text =
""
;
043.
044.
txtID.Focus();
045.
046.
txtNameProgram.Text =
""
;
047.
048.
txtNameProcess.Text =
""
;
049.
050.
return
;
051.
052.
}
053.
054.
055.
if
(MessageBox.Show(
"คุณต้องการเพิ่มโปรแกรมใช่หรือไม่?"
,
"คำยืนยัน"
, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
056.
057.
{
058.
059.
check();
060.
061.
strConn =
"Data Source=SKZ-43D05CDB745;Initial Catalog=server1;Integrated Security=True; User ID=sa;Password=1234"
;
062.
063.
Conn =
new
SqlConnection();
064.
065.
if
(Conn.State == ConnectionState.Open)
066.
067.
{
068.
069.
Conn.Close();
070.
071.
}
072.
073.
Conn.ConnectionString = strConn;
074.
075.
Conn.Open();
076.
077.
078.
StringBuilder sb =
new
StringBuilder();
079.
080.
SqlTransaction tr = Conn.BeginTransaction();
081.
082.
083.
try
084.
085.
{
086.
087.
088.
sb.Remove(0, sb.Length);
089.
090.
sb.Append(
"INSERT INTO Program (IDProgram,NameProgram,NameProcess)"
);
091.
092.
sb.Append(
"VALUES(@IDProgram,@NameProgram, @NameProcess)"
);
093.
094.
095.
string
sqlAdd;
096.
097.
sqlAdd = sb.ToString();
098.
099.
100.
101.
102.
SqlCommand com =
new
SqlCommand();
103.
104.
com.CommandText = sqlAdd;
105.
106.
com.CommandType = CommandType.Text;
107.
108.
com.Connection = Conn;
109.
110.
com.Transaction = tr;
111.
112.
com.Parameters.Clear();
113.
114.
115.
com.Parameters.Add(
"@IDProgram"
, SqlDbType.Int).Value = txtID.Text.Trim(); ;
116.
117.
com.Parameters.Add(
"@NameProgram"
, SqlDbType.NVarChar).Value = txtNameProgram.Text.Trim();
118.
119.
com.Parameters.Add(
"@NameProcess"
, SqlDbType.NVarChar).Value = txtNameProcess.Text.Trim();
120.
121.
com.ExecuteNonQuery();
122.
123.
tr.Commit();
124.
125.
126.
127.
128.
129.
130.
131.
MessageBox.Show(
"เพิ่มข้อมูลโปรแกรมเรียบร้อยแล้ว!!!"
,
"ผลการทำงาน"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
132.
133.
txtID.Text =
""
;
134.
135.
txtID.Focus();
136.
137.
txtNameProgram.Text =
""
;
138.
139.
txtNameProcess.Text =
""
;
140.
141.
ShowData();
142.
143.
}
144.
145.
catch
(Exception ex)
146.
147.
{
148.
149.
MessageBox.Show(
"ไม่สามารถเพิ่มข้อมูลโปรแกรมได้ เนื่องจาก "
+ ex.Message,
"ผลการตรวจสอบ"
, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
150.
151.
txtID.Text =
""
;
152.
153.
txtID.Focus();
154.
155.
txtNameProgram.Text =
""
;
156.
157.
txtNameProcess.Text =
""
;
158.
159.
}
160.
161.
162.
}
163.
164.
}
165.
166.
167.
private
void
ShowData()
168.
169.
{
170.
171.
if
(Conn.State == ConnectionState.Open)
172.
173.
{
174.
175.
Conn.Close();
176.
177.
}
178.
179.
Conn.ConnectionString = strConn;
180.
181.
Conn.Open();
182.
183.
string
sqldata =
" select * from Program "
;
184.
185.
SqlDataAdapter da =
new
SqlDataAdapter(sqldata, Conn);
186.
187.
da.Fill(ds,
"Data1"
);
188.
189.
dgvProgram.ReadOnly =
true
;
190.
191.
dgvProgram.DataSource = ds.Tables[
"Data1"
];
192.
193.
string
sqldata1 =
"select NameProgram from Program"
;
194.
195.
SqlDataAdapter da1 =
new
SqlDataAdapter(sqldata1, Conn);
196.
197.
da.Fill(ds,
"Data2"
);
198.
199.
checkedListBox.DataSource = ds.Tables[
"Data2"
];
200.
201.
checkedListBox.DisplayMember =
"NameProgram"
;
202.
203.
checkedListBox.ValueMember =
"NameProgram"
;
204.
205.
checkedListBox.SelectedIndex = 0;
206.
207.
}