c# - 检查2个数据表是否存在记录
问题描述
我正在尝试检查 2 个不同的数据表以查看工单是否存在。. 两个数据表都具有相同的列,但表名 [c#barcode] 和 [c1barcode] 不同。第一次检查 [c# 条码] 是否有相同的工作订单正在工作,但是如果执行标量代码中存在第二次检查 [c1 条码] 的 1 个或多个参数中的错误值,我会收到错误消息。
private void save_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("No data, Please scan workorder");
}
else
{
//Checking if workorder exist in main database
connection.Open();
OleDbCommand checkrecord = new OleDbCommand("SELECT COUNT(*) FROM [c# barcode] WHERE ([c# barcode].[Workorder] = @workorder)", connection);
checkrecord.Parameters.AddWithValue("@workorder", textBox1.Text);
int recordexist = (int)checkrecord.ExecuteScalar();
if (recordexist > 0)
{
textBox1.Clear();
MessageBox.Show("Workorder exist in main database");
connection.Close();
//load after every scan
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string comparequery = "SELECT * from [c# barcode]; ";
command.CommandText = comparequery;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt6 = new DataTable();
da.Fill(dt6);
dataGridView1.DataSource = dt6;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
}
else
{
//checking if record exisit in facility database
OleDbCommand checkrecord1 = new OleDbCommand("SELECT COUNT(*) FROM [c1 barcode] WHERE ([c1 barcode].[Workorder] = @workorder)", connection);
checkrecord.Parameters.AddWithValue("@workorder", textBox1.Text);
int recordexist1 = (int)checkrecord1.ExecuteScalar();//error no value given for 1 or more parameter
if (recordexist1 > 0)
{
textBox1.Clear();
MessageBox.Show("Workorder exist in facility database");
connection.Close();
//load after every scan
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string comparequery = "SELECT * from [c1 barcode]; ";
command.CommandText = comparequery;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt6 = new DataTable();
da.Fill(dt6);
dataGridView1.DataSource = dt6;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
}
else
{ //inserting workorder if it does not exist
string cmdText = "INSERT INTO [c1 barcode] ([Workorder],[Close for handover],[Name handover]) VALUES (@workorder,@Close,@name)";
using (OleDbCommand cmd = new OleDbCommand(cmdText, connection))
{
cmd.Parameters.AddWithValue("@workorder", textBox1.Text);
cmd.Parameters.AddWithValue("@Close", DateTime.Now.ToString("d/M/yyyy"));
cmd.Parameters.AddWithValue("@name", label6.Text);
if (cmd.ExecuteNonQuery() > 0)
{
textBox1.Clear();
//load after every scan
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string comparequery = "SELECT * from [c1 barcode]; ";
command.CommandText = comparequery;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt6 = new DataTable();
da.Fill(dt6);
dataGridView1.DataSource = dt6;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
}
else
{
textBox1.Clear();
MessageBox.Show("Please rescan");
connection.Close();
}
connection.Close();
}
}
}
}
}
解决方案
推荐阅读
- matlab - 在 Matlab 中绘制数据的问题(在 x 轴上设置月份和年份)
- javascript - 单选vue js多选子组
- sql - 如果存在,如何在表上删除聚集索引并添加新的聚集索引?
- php - 为什么 laravel 6 多对多关系不起作用?
- python - 为什么 Numpy 的 int 数据类型与 Numpy 的 int64 数据类型不同?
- rust - 如何在 Rust 中将 std::iter::Iterator::map 用于树状结构?
- asp.net-core - 如何在 ASP.NET Core 中创建下拉列表?
- angular - 第二次之后,如果我使用订阅,用户可以登录
- reactjs - ReactJS - 如何根据服务器响应处理给用户的通知?
- php - 在这种情况下如何限制表中数据的输出?