如何用设计模式去除ifelse多重嵌套?
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;
}
...全文
相关推荐
elseif.js:嵌套ifelse语句
嵌套的`if...else`语句可以增加代码的复杂性,但也提高了灵活性。
ifelser:R 包使 ifelse 语句更容易
ifelser是一个R语言包,旨在提高嵌套ifelse语句的可读性和维护性。通过结合magrittr管道操作符,该包提供了一种链式语法来构建复杂的条件判断逻辑,避免深层嵌套带来的括号混乱问题。项目包含
ifelse分析
标题“ifelse分析”主要涉及的是编程中的条件控制结构,特别是`if`和`else`语句的使用。在编程中,`if`和`else`是基础且至关重要的部分,用于根据特定条件执行不同的代码块。
Java程序设计基础:多分支ifelse语句.pptx
Java程序设计基础中的多分支ifelse语句是编程中实现逻辑决策的关键部分。在本讲中,我们将深入理解如何通过if语句来控制程序的执行流程,根据不同的条件执行不同的操作。主要内容分为以下几个方面:
Java基础if和ifelse选择结构
**多重if-else语句**:多个`if`语句依次判断,直到找到符合条件的分支为止。


