首页 > 解决方案 > 检查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();
                    }
                }
                }

        }
    }

标签: c#ms-accessexecutescalar

解决方案


推荐阅读