有两个textBox。 textBox1让用户输入要查询的起始日期。textBox2让用户输入要查询的截止日期。
当用户输入好开始日期和截止日期以后,点击button1按钮对数据库中的表进行筛选,然后将结果写到界面上的dataGriedview1中。举个例子:表中有三行数据如下
                  ID          NAME               TIME
                   1           录录        2009-1-11 19:00
                   2           清清        2009-1-12 18:00
                   3           平平        2009-1-13 14:00那么,如果用户在textBox1中输入2009-1-11,然后在textBox2中输入2009-1-12.点击button1按钮以后,界面上的dataGriedview1就会出现该表中的前两行数据。这个功能应该如何实现?初学水平,希望高手最好能给出代码。
感激不尽!!!

解决方案 »

  1.   

    string sql=string.Format("select ID,NAME,TIME from tableName where TIME between '{0}' and '{1} 23:59:59.999'",textbox1.Text,textbox2.Text);DataTable dt = new DataTable();DataAdapter adp = new SqlDataAdapter(sql,conn);//conn是一个可用连接对象adp.Fill(dt);datagridview1.DataSource = dt;
      

  2.   

    select [ID],[Name],[Time]  from tb where [Time]>'2009-1-11' and [Time]<'2009-1-13'
      

  3.   


    create table tb
    (
         [ID] int,
         [Name] varchar(50),
         [Time] datetime
    )insert into tb 
    select 1,'录录','2009-1-11 19:00' union all
    select 2,'清清','2009-1-12 18:00' union all
    select 3,'平平','2009-1-13 14:00' private void Form1_Load(object sender, EventArgs e)
            {
                using (SqlConnection con = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=xxx"))
                {
                    SqlDataAdapter da = new SqlDataAdapter("select [ID],[Name],[Time]  from tb ", con);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "tb");
                    if (ds.Tables.Contains("tb"))
                    {
                        dataGridView1.DataSource = ds.Tables["tb"];
                    }
                }        }        private void button1_Click(object sender, EventArgs e)
            {
                using (SqlConnection con = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=xxx"))
                {
                    SqlDataAdapter da = new SqlDataAdapter("select [ID],[Name],[Time]  from tb where [Time]>@StartTime and [Time]<@EndTime", con);
                    DateTime dt;
                    if (!DateTime.TryParse(textBox1.Text, out dt))
                    {
                        MessageBox.Show("开始时间格式不正确");
                        return;
                    }
                    da.SelectCommand.Parameters.Add(new SqlParameter("@StartTime", dt));
                    if (!DateTime.TryParse(textBox2.Text, out dt))
                    {
                        MessageBox.Show("结束时间格式不正确");
                        return;
                    }
                    da.SelectCommand.Parameters.Add(new SqlParameter("@EndTime", dt.AddDays(1)));
                    DataSet ds = new DataSet();
                    da.Fill(ds, "tb");
                    if (ds.Tables.Contains("tb"))
                    {
                        dataGridView1.DataSource = ds.Tables["tb"];
                    }
                }        }
      

  4.   


    private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(402, 44);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 21);
                this.button1.TabIndex = 0;
                this.button1.Text = "查询";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(66, 44);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(100, 21);
                this.textBox1.TabIndex = 1;
                // 
                // textBox2
                // 
                this.textBox2.Location = new System.Drawing.Point(239, 44);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(100, 21);
                this.textBox2.TabIndex = 2;
                // 
                // dataGridView1
                // 
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Location = new System.Drawing.Point(66, 112);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.RowTemplate.Height = 23;
                this.dataGridView1.Size = new System.Drawing.Size(411, 255);
                this.dataGridView1.TabIndex = 3;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(568, 420);
                this.Controls.Add(this.dataGridView1);
                this.Controls.Add(this.textBox2);
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();        }        private System.Windows.Forms.Button button1;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.TextBox textBox2;
            private System.Windows.Forms.DataGridView dataGridView1;