C# 如何将C变量传递给Access 2010查询
我编写了一个程序,可以在Access数据库中读写信息 请看下面的屏幕截图和代码,它工作得很好,没有任何问题。但是,我想扩展它,能够单击其他按钮,让它检索所有记录,但首先提示输入日期或日期范围。我已经知道如何获取要提示的访问表单或访问报告,但我希望C程序提示输入日期,并将其传递给查询;然后在结果窗口中显示,如图所示。这样,当报表或窗体打开时,用户就不会有访问权限了。感谢您的帮助,并提前感谢您的建议C# 如何将C变量传递给Access 2010查询,c#,ms-access,variables,C#,Ms Access,Variables,我编写了一个程序,可以在Access数据库中读写信息 请看下面的屏幕截图和代码,它工作得很好,没有任何问题。但是,我想扩展它,能够单击其他按钮,让它检索所有记录,但首先提示输入日期或日期范围。我已经知道如何获取要提示的访问表单或访问报告,但我希望C程序提示输入日期,并将其传递给查询;然后在结果窗口中显示,如图所示。这样,当报表或窗体打开时,用户就不会有访问权限了。感谢您的帮助,并提前感谢您的建议 namespace Health_Direct_Receiving { public par
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
表格三
Form2和Form3的代码在哪里?我不明白这应该会消除用户在报表或表单打开时的访问权限。您想键入其他内容吗?只需执行与“读取”中相同的操作,仅用于将参数插入“选择”中。@Geo,请将代码插入主问题中,而不是注释中,它是不可读的。点击编辑question@MattiasJosefsson:我为上面另外两页添加了代码。
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();
}
}
}
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();
}
}
}