【问题标题】:retrieve the data using monthcalendar control使用月历控件检索数据
【发布时间】:2013-05-16 09:00:47
【问题描述】:
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
    {
        try
        {
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=  C:\\Users\\JAY\\Desktop\\Employee.mdb");
            OleDbCommand cmd = new OleDbCommand("select * from Emp_Details WHERE DOB="+ monthCalendar1.SelectionRange.Start.ToShortDateString() +"", con);
            cmd.CommandType = CommandType.Text;
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "Emp_Details");
            txtEmployeeNo.Text = ds.Tables[0].Rows[0][0].ToString();
            txtName.Text = ds.Tables[0].Rows[0][1].ToString();
            txtAddress.Text = ds.Tables[0].Rows[0][2].ToString();
            comboBox1.Text = ds.Tables[0].Rows[0][3].ToString();
            txtMobNo.Text = ds.Tables[0].Rows[0][4].ToString();


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

基本上我想通过月历控制检索数据...但是 当我单击月历控件的日期时出现异常,位置 0 处没有行

【问题讨论】:

    标签: c# ms-access monthcalendar


    【解决方案1】:

    不要使用内联参数,你可以使用下面的参数化查询

    using (var con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=  C:\Users\JAY\Desktop\Employee.mdb"))
    using (var cmd = new OleDbCommand("select * from Emp_Details WHERE DOB= ?", con))
    {
        cmd.Parameters.AddWithValue("@P1", monthCalendar1.SelectionRange.Start);
        using (var da = new OleDbDataAdapter(cmd))
        {
            da.Fill(ds, "Emp_Details");
            if (ds.Tables["Emp_Details"] !=null && ds.Tables["Emp_Details"].Rows.Count>0)
            {
                DataRow dr = ds.Tables["Emp_Details"].Rows[0];
                txtEmployeeNo.Text = dr[0].ToString();
                txtName.Text = dr[1].ToString();
                txtAddress.Text = dr[2].ToString();
                comboBox1.Text = dr[3].ToString();
                txtMobNo.Text = dr[4].ToString();
            }
        }
    } 
    

    【讨论】:

    • 非常感谢您的回复,但是当我点击日期时仍然有一些问题,即 1991 年 7 月 28 日。它是 access 数据库中的 dob 字段,我无法检索其中的数据文本框
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2017-03-17
    • 2014-01-19
    • 2021-10-08
    • 1970-01-01
    相关资源
    最近更新 更多