-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddRecord.cs
145 lines (125 loc) · 5.64 KB
/
AddRecord.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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;
using System.Data.SqlClient;
namespace FabricInfo
{
public partial class AddRecord : Form
{
public AddRecord()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label9_Click(object sender, EventArgs e)
{
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void AddRecord_Load(object sender, EventArgs e)
{
this.Dock = DockStyle.Fill; // 确保填充父窗体
this.FormBorderStyle = FormBorderStyle.None; // 去掉边框以完全填满
}
private void makeEmpty_click(object sender, EventArgs e)
{
foreach (Control con in groupBox1.Controls)
{
if (con is TextBox)
{
con.Text = string.Empty;
}
}
foreach (Control con in groupBox2.Controls)
{
if (con is TextBox)
{
con.Text = string.Empty;
}
}
}
private void takePreserve_click(object sender, EventArgs e)
{
string fabric_id = tbfabid.Text.Trim();
string fabric_name = tbfabname.Text.Trim();
string fabric_composition = tbfabcomposition.Text.Trim();
string fabric_origin = tbfaborigin.Text.Trim();
string supplier_id = tbsupid.Text.Trim();
string production_batch = tbprobatch.Text.Trim();
string glossiness = tbfabglo.Text.Trim();
string texture_and_structure = tbfabtands.Text.Trim();
if (string.IsNullOrEmpty(fabric_id) || string.IsNullOrEmpty(fabric_name) || string.IsNullOrEmpty(fabric_composition) ||
string.IsNullOrEmpty(fabric_origin) || string.IsNullOrEmpty(supplier_id) || string.IsNullOrEmpty(production_batch) ||
string.IsNullOrEmpty(glossiness) || string.IsNullOrEmpty(texture_and_structure))
{
MessageBox.Show("所有字段均为必填项,请填写完整!");
return;
}
if (!int.TryParse(glossiness, out _))
{
if (!double.TryParse(glossiness, out _))
{
MessageBox.Show("光泽度必须为数字!");
return;
}
}
String connectionString = "Data Source = .;Initial Catalog = fabric;Integrated Security = True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string checkFabricIdQuery = "SELECT COUNT(*) FROM fabrics WHERE fabric_id = @fabric_id";
using (SqlCommand checkFabricIdCommand = new SqlCommand(checkFabricIdQuery, connection))
{
checkFabricIdCommand.Parameters.AddWithValue("@fabric_id", fabric_id);
int fabricIdCount = (int)checkFabricIdCommand.ExecuteScalar();
if (fabricIdCount > 0)
{
MessageBox.Show("面料ID已存在!");
return;
}
}
string checkSupplierIdQuery = "SELECT COUNT(*) FROM suppliers WHERE supplier_id = @supplier_id";
using (SqlCommand checkSupplierIdCommand = new SqlCommand(checkSupplierIdQuery, connection))
{
checkSupplierIdCommand.Parameters.AddWithValue("@supplier_id", supplier_id);
int supplierIdCount = (int)checkSupplierIdCommand.ExecuteScalar();
if (supplierIdCount == 0)
{
MessageBox.Show("供应商ID不存在,请先添加供应商信息!");
return;
}
}
string query = "INSERT INTO fabrics (fabric_id, fabric_name, fabric_composition, fabric_origin, supplier_id, production_batch, glossiness, texture_and_structure) " +
"VALUES (@fabric_id, @fabric_name, @fabric_composition, @fabric_origin, @supplier_id, @production_batch, @glossiness, @texture_and_structure)";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@fabric_id", fabric_id);
command.Parameters.AddWithValue("@fabric_name", fabric_name);
command.Parameters.AddWithValue("@fabric_composition", fabric_composition);
command.Parameters.AddWithValue("@fabric_origin", fabric_origin);
command.Parameters.AddWithValue("@supplier_id", supplier_id);
command.Parameters.AddWithValue("@production_batch", production_batch);
command.Parameters.AddWithValue("@glossiness", glossiness);
command.Parameters.AddWithValue("@texture_and_structure", texture_and_structure);
command.ExecuteNonQuery();
}
connection.Close();
}
MessageBox.Show("记录添加成功!");
}
private void btnaddexit_Click(object sender, EventArgs e)
{
MessageBox.Show("确定要退出吗?", "退出确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
this.Close();
}
}
}