c#中如何用2个datetimepicker1控件查数据库中一段时间内的数据
2013-11-26
展开全部
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;
}
}
}
}
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;
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询