01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.ComponentModel;
04.
using
System.Data;
05.
using
System.Drawing;
06.
using
System.Text;
07.
using
System.Windows.Forms;
08.
using
System.Data.SqlClient;
09.
using
System.Data.OleDb;
10.
using
System.IO;
11.
12.
namespace
Welcome_form
13.
{
14.
public
partial
class
insertdatapolice : Form
15.
{
16.
public
insertdatapolice()
17.
{
18.
InitializeComponent();
19.
}
20.
21.
private
void
Showdata()
22.
{
23.
string
conStr =
"Data Source=(local); Integrated Security=True; Initial Catalog=Trafficpolice "
;
24.
25.
SqlConnection Conn =
new
SqlConnection(conStr);
26.
Conn.Open();
27.
28.
String Sql =
"SELECT * FROM Police"
;
29.
SqlCommand cmd =
new
SqlCommand(Sql, Conn);
30.
31.
SqlDataAdapter adapter =
new
SqlDataAdapter(cmd);
32.
33.
DataSet data =
new
DataSet();
34.
adapter.Fill(data,
"Pol"
);
35.
36.
dataGridView1.DataSource = data.Tables[
"Pol"
];
37.
38.
Conn.Close();
39.
}
40.
41.
private
void
insertdatapolice_Load(
object
sender, EventArgs e)
42.
{
43.
this
.WindowState = FormWindowState.Maximized;
44.
Showdata();
45.
}
46.
47.
private
void
button1_Click(
object
sender, EventArgs e)
48.
{
49.
this
.Close();
50.
}
51.
52.
private
void
button2_Click(
object
sender, EventArgs e)
53.
{
54.
string
conStr =
"Data Source=(local); Integrated Security=True; Initial Catalog=Trafficpolice "
;
55.
56.
SqlConnection Conn =
new
SqlConnection(conStr);
57.
Conn.Open();
58.
59.
String sql =
"INSERT INTO Police VALUES(@Pol_login, @Pol_pass, @Pol_name, @Pol_lname, @Pol_sex, @Pol_phone, @Pol_station, @Pol_no)"
;
60.
61.
SqlCommand cmd =
new
SqlCommand(sql, Conn);
62.
63.
cmd.Parameters.AddWithValue(
"Pol_login"
, textBox1.Text);
64.
cmd.Parameters.AddWithValue(
"Pol_pass"
, textBox2.Text);
65.
cmd.Parameters.AddWithValue(
"Pol_name"
, textBox3.Text);
66.
cmd.Parameters.AddWithValue(
"Pol_lname"
, textBox4.Text);
67.
cmd.Parameters.AddWithValue(
"Pol_sex"
, textBox5.Text);
68.
cmd.Parameters.AddWithValue(
"Pol_phone"
, textBox6.Text);
69.
cmd.Parameters.AddWithValue(
"Pol_station"
, textBox7.Text);
70.
cmd.Parameters.AddWithValue(
"Pol_no"
, textBox8.Text);
71.
cmd.ExecuteNonQuery(); <------------มันติด error ตรงนี้ครับ
72.
73.
Conn.Close();
74.
Showdata();
75.
}
76.
}
77.
}