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;
            }
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM