如何将C#变量传递给Access 2010查询

时间:2012-04-19 19:53:52

标签: c# ms-access variables

我编写了一个程序,用于从Access数据库读取信息和从Access数据库写入信息。

(请参阅下面的屏幕截图和代码),它运行良好,没有任何问题但是,我想扩展它,以便能够单击其他按钮,并让它检索所有记录,但首先提示日期(或日期范围)。我已经知道如何获取Access表单或Access报告来提示,但我希望C#程序提示输入日期,并将其传递给查询;然后在结果窗口中显示,就像在图片中一样。这应该消除用户在报表或表单形式时打开Access。感谢任何帮助,并提前感谢任何建议!

enter image description here

namespace Health_Direct_Receiving
{
    public partial class Form1 : Form
    {

        OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\Receiving\Database.accdb");

        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            {
                vcon.Open();
            }
        }


        private void button1_Click(object sender, EventArgs e)
            {
                {
                    if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
                        {
                            MessageBox.Show("You must fill in all fields.");
                            return;
                        }
                    else
                        {   
                            OleDbCommand dbCommand;
                            OleDbDataReader dbReader;
                            new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\Receiving\Database.accdb");
                            dbCommand = new OleDbCommand("select count(*) as Record_Count from script_received", vcon);

                            dbReader = dbCommand.ExecuteReader();
                            if (dbReader.Read() == true)
                                rowCount = dbReader["Record_Count"].ToString();
                            else
                            return;

                            var date = DateTime.Now.ToString("MM/dd/yyyy");



                            {
                                using(OleDbCommand command = new OleDbCommand("INSERT INTO script_received (script, qty, emp_id, received_Date) VALUES (@script,@qty,@emp_Id,@rec_date)")) 
                                  {
                                      command.CommandType = CommandType.Text;
                                      command.Parameters.Add("@script", OleDbType.Integer).Value = textBox1.Text; 
                                      command.Parameters.Add("@qty", OleDbType.VarChar).Value = textBox2.Text;
                                      command.Parameters.Add("@emp_id", OleDbType.VarChar).Value = textBox3.Text;
                                      command.Parameters.Add("@rec_date", OleDbType.Date).Value = date; 
                                      command.Connection = vcon;

                                      //vcon.Open();

                                      command.ExecuteNonQuery();
                                  }

                                    this.textBox1.Clear();
                                    this.textBox2.Clear();
                                    this.textBox1.Focus();
                        }
                    }
                }
            }

        private void button2_Click(object sender, EventArgs e)
            {
                new Form2().Show();
            }

        private void button3_Click(object sender, EventArgs e)
            {
                new Form3().Show();
            }

        private void button4_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
        public object rowCount { get; set; }
    }
}

Form2

namespace Receiving
           {
              public partial class Form2 : Form
                 { 
                   public Form2() 
                      {
                         InitializeComponent();
                      }
                   private void Form2_Load(object sender, EventArgs e)
                      {
                   this.script_receivedTableAdapter.Fill(this.DatabaseDataSet.script_received);
                      }
                   private void button2_Click(object sender, EventArgs e)
                      {
                         this.Close();
                  }
            }
       }

表格3


namespace Receiving 
    {
        public partial class Form3 : Form
            {
                public Form3()
                    {
                        InitializeComponent();
                    }
                private void Form3_Load(object sender, EventArgs e)
                    {                 this.script_All_ReceivedTableAdapter.Fill(this.DatabaseDataSet.script_All_‌​Received); 
                    }
                private void button2_Click(object sender, EventArgs e)
                    {
                        this.Close(); 
                    }
            }
    }

0 个答案:

没有答案