查询后显示到datagridview中,查询条件:某一字段的值的长度应该在两个值之间(比如0~5..也就是字段值的.Length<5)
注意:是字段值的[长度],不是值。下面的例子是在字段值之间的举例,如果能实现,希望高手在此代码基础上修改。    string ConStr = @"server=.\SQLEXPRESS;uid=sa;pwd=sa;database=Northwind";
            SqlConnection con = new SqlConnection(ConStr);
            string SqlStr = "select CustomerID,RequiredDate,Freight,ShipName,ShipName from Orders where Freight between @dtp1 and @dtp2";
            //下面怎么给@dtp1和dtp2赋值啊,@dtp1对应的值为dateTimePicker1.Text  @dtp2对应的值为dateTimePicker1.Text
            SqlCommand cmd = new SqlCommand(SqlStr, con);
            cmd.Parameters.AddWithValue("@dtp1", textBox1.Text);
            cmd.Parameters.AddWithValue("@dtp2", textBox2.Text);            SqlDataAdapter ada = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ada.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0];

解决方案 »

  1.   

    也可以len(Freight ) between 0 and 5
      

  2.   


    string ConStr = @"server=.\SQLEXPRESS;uid=sa;pwd=sa;database=Northwind";
                SqlConnection con = new SqlConnection(ConStr);
                string SqlStr = "select CustomerID,RequiredDate,Freight,ShipName,ShipName from Orders where len(Freight) between @dtp1 and @dtp2";
                //下面怎么给@dtp1和dtp2赋值啊,@dtp1对应的值为dateTimePicker1.Text  @dtp2对应的值为dateTimePicker1.Text
                SqlCommand cmd = new SqlCommand(SqlStr, con);
                cmd.Parameters.AddWithValue("@dtp1", textBox1.Text);
                cmd.Parameters.AddWithValue("@dtp2", textBox2.Text);            SqlDataAdapter ada = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                ada.Fill(ds);
                this.dataGridView1.DataSource = ds.Tables[0];
      

  3.   

    有点问题,你的Freight字段是什么类型的
      

  4.   

    nvarchar(40)的 没碰到问题。