我正在创建一个注册应用程序,它有两种工作方式:在线和离线。我们使用条形码将自己扫描到应用程序中。当您扫描在线条形码时,它会给您 6 个数字和一个“L”,例如 242565L ,程序将选择它并在 SQL 数据库中搜索相应的 ID 号。并且离线条形码包含一个带有“ ”的名称文本值,例如 Smith, John。
当我运行它并扫描在线条形码时,它工作正常。但是,当我断开与互联网的连接,因此它找不到数据库并扫描我的代码时,它会拾取 1 个字母并冻结并挂起,直到我停止该过程。
请问有什么想法吗?C# 新手
这是主要代码:
{
SqlConnection DBConnection = new SqlConnection("DataSource=DATABASE;Initial Catalog=imis;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
Object returnValue;
string txtend = textBox1.Text;
try
{
DBConnection.Open();
}
catch
{
DBConnection.Close();
}
if (DBConnection.State == ConnectionState.Open)
{
if (textBox1.Text.Length != 7) return;
cmd.CommandText = "SELECT last_name +', '+ first_name +'\t ('+major_key+')\t' from name where id ='" + textBox1.Text.Replace(@"L", "") + "'";
// MessageBox.Show(textBox1.Text.Replace(@"L", ""));
cmd.CommandType = CommandType.Text;
cmd.Connection = DBConnection;
// sqlConnection1.Open();
returnValue = cmd.ExecuteScalar() + "\t (" + textBox1.Text.Replace(@"L", "") + ")";
DBConnection.Close();
if (listBox1.Items.Contains(returnValue))
{
for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
string removelistitem = returnValue.ToString();
if (listBox1.Items[n].ToString().Contains(removelistitem))
{
// listBox1.Items.RemoveAt(n);
}
}
}
else
listBox1.Items.Add(returnValue);
textBox1.Text = null;
if (listBox1.Items.Count != 0) { DisableCloseButton(); }
else
{
EnableCloseButton();
}
label6.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance.";
}
else
{
try
{
string lastTwoChars = txtend.Substring(txtend.Length - 1);
returnValue = textBox1.Text.Replace(@"*", "");
if (lastTwoChars != "*") return;
{
if (listBox1.Items.Contains(returnValue))
{
for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
string removelistitem = returnValue.ToString();
if (listBox1.Items[n].ToString().Contains(removelistitem))
{
//listBox1.Items.RemoveAt(n);
}
}
}
else
listBox1.Items.Add(returnValue);
textBox1.Text = null;
if (listBox1.Items.Count != 0) { DisableCloseButton(); }
else
{
EnableCloseButton();
}
label6.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance.";
}
}
catch
{
}
}
}