我编写了一个程序,该程序可以在 Access 数据库中读取和写入信息。
(请参阅下面的屏幕截图和代码),效果很好,没有问题但是,我想扩展它,以便能够单击一个附加按钮,并让它检索所有记录,但提示输入日期(或日期范围)第一的。我已经知道如何让 Access 表单或 Access 报表提示,但我希望 C# 程序提示输入日期,并将其传递给查询;然后显示在结果窗口中,如图所示。当报表或表单的表单打开时,这应该消除用户打开 Access。任何帮助表示赞赏,并提前感谢任何建议!
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; }
}
}
表格2
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();
}
}
}