0

I am creating one application in C# in which I am using datetimepicker control. I have created a database in MS Access which contains one table with Date_Entry, Emp_No, Emp_Name, In_Time, Out_Time columns.

Now I want to retrieve these data in textbox by clicking date on datetimepicker control. This date is point the date in Date_Entry field of database and fetch the data according to that date.

How to do it?

 private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
 {
 try
 {
 OleDbConnection conn = new OleDbConnection    ("Provider=Microsoft.Jet.OLEDB.4.0,DataSource=C:\\Users\\jd\\Desktop\\Attendance.mdb");
 OleDbCommand cmd = new OleDbCommand("SELECT * FROM Attendance_Details WHERE Date_Entry=" +     dateTimePicker1.Value + "", conn);
 cmd.CommandType = CommandType.Text;
 OleDbDataAdapter da = new OleDbDataAdapter(cmd);
 DataSet ds = new DataSet();
 da.Fill(ds, "Attendance_Details");
 txtDate.Text = ds.Tables[0].Rows[0][0].ToString();
 txtEmpNo.Text = ds.Tables[0].Rows[0][1].ToString();
 txtEmpName.Text = ds.Tables[0].Rows[0][2].ToString();
 txtInTime.Text = ds.Tables[0].Rows[0][3].ToString();
 txtOutTime.Text = ds.Tables[0].Rows[0][4].ToString();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.ToString());
 }
 }
 }
4

2 回答 2

0

回答您关于“错误 1 ​​'System.Data.OleDb.OleDbParameterCollection' 不包含 'Addwithvalue' 的定义并且没有扩展方法 'Addwithvalue' 接受'System.Data.OleDb.OleDbParameterCollection' 类型的第一个参数”的问题

...你谷歌了吗?https://www.google.nl/search?q=Addwithvalue它实际上向您显示出了什么问题。

关于选择日期:阅读此。这可能很棘手。确保使用正确的语法。

于 2013-05-15T08:01:32.520 回答
0

以上代码存在一些问题。我已经提炼了它。现在它将起作用。

以下应该是代码:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
 {
 try
 {

 OleDbConnection conn = new OleDbConnection    ("Provider=Microsoft.Jet.OLEDB.4.0,DataSource=C:\\Users\\jd\\Desktop\\Attendance.mdb");
 con.open();
 OleDbCommand cmd = new OleDbCommand("SELECT * FROM Attendance_Details WHERE Date_Entry=@dtpDate", conn);
 cmd.Parameters.Addwithvalue("@dtpDate", dateTimePicker1.Value);
 cmd.CommandType = CommandType.Text;
 OleDbDataAdapter da = new OleDbDataAdapter(cmd);
 DataSet ds = new DataSet();
 da.Fill(ds);
 txtDate.Text = ds.Tables[0].Rows[0][0].ToString();
 txtEmpNo.Text = ds.Tables[0].Rows[0][1].ToString();
 txtEmpName.Text = ds.Tables[0].Rows[0][2].ToString();
 txtInTime.Text = ds.Tables[0].Rows[0][3].ToString();
 txtOutTime.Text = ds.Tables[0].Rows[0][4].ToString();
 con.close();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 }
 finally
 {
   con.close();
 }
 }
 }

希望它有帮助。

于 2013-05-15T05:12:02.690 回答