c#中如何用2个datetimepicker1控件查数据库中一段时间内的数据

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter("select * from xxx where 日期字段>=@startdate and 日期字段<=@enddate",new SqlConnection("……")))
            {
                adapter.SelectCommand.Parameters.Add("@startdate", SqlDbType.DateTime);
                adapter.SelectCommand.Parameters.Add("@enddate", SqlDbType.DateTime);
                adapter.SelectCommand.Parameters[0].Value = DateTime.Parse(dateTimePicker1.Text).ToShortDateString();
                adapter.SelectCommand.Parameters[1].Value = DateTime.Parse(dateTimePicker2.Text).ToShortDateString();
                DataTable dt = new DataTable();
                adapter.Fill(dt);

              DataGridView.DataSource = dt;
            }
        }
    }
}

posted @ 2012-12-26 15:27  四条眉毛  阅读(4057)  评论(0编辑  收藏  举报