菜鸟提问:SQL语句能做到么?(查询条件有点特殊)

ioriyagami4 2011-05-24 05:10:29
查询后显示到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];
...全文
103 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
OROCHIORI691 2011-06-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ojlovecd 的回复:]
C# code


string ConStr = @"server=.\SQLEXPRESS;uid=sa;pwd=sa;database=Northwind";
SqlConnection con = new SqlConnection(ConStr);
string SqlStr = "select CustomerID,Requir……
[/Quote]
right
ioriyagami4 2011-05-24
  • 打赏
  • 举报
回复
谢谢各位
ioriyagami4 2011-05-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ojlovecd 的回复:]
有点问题,你的Freight字段是什么类型的
[/Quote]
nvarchar(40)的 没碰到问题。
我姓区不姓区 2011-05-24
  • 打赏
  • 举报
回复
有点问题,你的Freight字段是什么类型的
我姓区不姓区 2011-05-24
  • 打赏
  • 举报
回复

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];
CANL464970302 2011-05-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ohhero 的回复:]
引用 1 楼 hustliangchen 的回复:
where len(Freight )<100

也可以len(Freight ) between 0 and 5
[/Quote]
++
单线程加锁 2011-05-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hustliangchen 的回复:]
where len(Freight )<100
[/Quote]
也可以len(Freight ) between 0 and 5
lilin8905 2011-05-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hdngo 的回复:]

引用 3 楼 bdmh 的回复:

有这个函数len


+1
[/Quote]

++
HDNGO 2011-05-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bdmh 的回复:]

有这个函数len
[/Quote]

+1
guoke1 2011-05-24
  • 打赏
  • 举报
回复
len
  • 打赏
  • 举报
回复
SQL有这个函数的
bdmh 2011-05-24
  • 打赏
  • 举报
回复
有这个函数len
xuexiaodong2009 2011-05-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hustliangchen 的回复:]

where len(Freight )<100
[/Quote]同意可行
旅行者I号 2011-05-24
  • 打赏
  • 举报
回复
where len(Freight )<100

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧