winform程式,感覺ifelse嵌套有點多了,如何用狀態模式去除ifelse嵌套,請高手指教,謝謝。
private void UploadBtn_Click(object sender, EventArgs e) {
if (string.IsNullOrWhiteSpace(ScanTbx.Text)) return;
string text = ScanTbx.Text.Trim();
using (var connection = SqlCon.TestDbConnection()) {
/***********************************************************************/
if (!next) {
if (LuhnAlgorithm.IsValidNumber(text)) {
string imei = $"select * from Binding where IMEI='{text}'";
var imeiQuery = connection.Query<Binding>(imei).ToList();
if ((imeiQuery.Count == 0) || cbxEdit865Code.Checked) {
lblIMEI.Text = text;
next = !next;
label1.Text = "請掃描小條碼";
}
else {
MessageBox.Show("IMEI已掃描", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
else {
MessageBox.Show("IMEI錯誤", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
/***********************************************************************/
else {
if (cbxEditCode.Checked) {
string editSN = $"update Binding set SN='{text}' where IMEI='{lblIMEI.Text}'";
var sqlUpdateSN = connection.Execute(editSN);
if (sqlUpdateSN > 0) {
next = !next;
label1.Text = "請掃描大I條碼";
lblIMEI.Text = "";
cbxEdit865Code.Checked = false;
/*********************************/
string query = "select * from Binding order by BindingDate desc";
var sqlQuery = connection.Query<Binding>(query).ToList();
dataGridView1.DataSource = sqlQuery;
}
else {
MessageBox.Show("修改失敗", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
else {
string sn = $"select * from Binding where SN='{text}'";
var snQuery = connection.Query<Binding>(sn).ToList();
if (snQuery.Count == 0) {
string insert = $"insert into Binding (IMEI,SN) values(" +
$"'{lblIMEI.Text}','{text}')";
var sqlInsert = connection.Execute(insert);
if (sqlInsert > 0) {
next = !next;
label1.Text = "請掃描大條碼";
lblIMEI.Text = "";
/*********************************/
string query = "select * from Binding order by BindingDate desc";
var sqlQuery = connection.Query<Binding>(query).ToList();
dataGridView1.DataSource = sqlQuery;
}
else {
MessageBox.Show("系結失敗", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
else {
MessageBox.Show("小條碼已掃描", "提示", MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
}
}
ScanTbx.Clear();
ScanTbx.Focus();
return;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/262301.html
標籤:C#
上一篇:面試怎么總愛問我的未來職業規劃呀
下一篇:怎么限制父類建構式的呼叫時間?
