小菜再来问一个很简单的问题。

gootey 2010-05-25 07:13:50
用途:查询。
弄两个DropDownList来让用户选定起始时间和结束时间,另外加了一个TextBox控件,让用户输入要查询的内容。以上是“搜索”按钮事件的代码。不知道这个SQL字符串怎么写了,呵呵。我知道这样写的SQL语句不对,请教各位大哥,指正一下,谢谢啦。



protected void soso_Click(object sender, EventArgs e)
{
DateTime dt1 = Convert.ToDateTime(nian.SelectedValue.ToString() + "-" + yue.SelectedValue.ToString() + "-" + ri.SelectedValue.ToString());
DateTime dt2 = Convert.ToDateTime(nian1.SelectedValue.ToString() + "-" + yue1.SelectedValue.ToString() + "-" + ri1.SelectedValue.ToString());
string son = TextBox1.Text.Trim().ToString();
string Sostr=ConfigurationManager.ConnectionStrings["WHConn"].ConnectionString;
SqlConnection Scon = new SqlConnection(Sostr);
Scon.Open();
string TSstr = "select * from gzhz where 维修时间 Between '"dt1"' and '"dt2"'";
string SSstr = "select * from gzhz where 故障地点 like '" % son % "' or 系统类别 like '" % son % "'' or 设备类型 like '" % son % "' or 故障现象 like '" % son % "' or 故障原因 like '" % son % "' or 处理办法 like '" % son % "' or 处理结果 like '" % son % "' or 参与人员 like '" % son % "'";
}
...全文
197 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gootey 2010-05-25
  • 打赏
  • 举报
回复
再顶一下,呵呵
gootey 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuyq11 的回复:]
string sql="select * from Tb where 1=1";
sql+=string.isNullOrEmpty(son)?"":"and 故障地点 like '%"+ son +"%'";
[/Quote]
对了,像这种单引号双引号的使用,有什么规律么?我老是搞不懂,呵呵
gootey 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuyq11 的回复:]
string sql="select * from Tb where 1=1";
sql+=string.isNullOrEmpty(son)?"":"and 故障地点 like '%"+ son +"%'";
[/Quote]
感谢mayonglong,你的语句可以用。
如梦大哥 的语句有点意思,能解释一下么?
wuyq11 2010-05-25
  • 打赏
  • 举报
回复
string sql="select * from Tb where 1=1";
sql+=string.isNullOrEmpty(son)?"":"and 故障地点 like '%"+ son +"%'";
mayonglong 2010-05-25
  • 打赏
  • 举报
回复

string TSstr = "select * from gzhz where 维修时间 Between '"+dt1+"' and '"+dt2+"'";
gootey 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hpzius 的回复:]
如果你想直接用,起码得用 dt1.tostring() 和 dt2.tostirng()

但是最好是用参数化的方式

sql = "select * from gzhz where 维修时间 Between @dt1 and @dt2"
然后再传入具体的值。
[/Quote]
嗯?不明白哦
hpzius 2010-05-25
  • 打赏
  • 举报
回复
如果你想直接用,起码得用 dt1.tostring() 和 dt2.tostirng()

但是最好是用参数化的方式

sql = "select * from gzhz where 维修时间 Between @dt1 and @dt2"
然后再传入具体的值。
gootey 2010-05-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zjtpiaoxue 的回复:]
string TSstr = @"select * from gzhz where 维修时间 between '"+dt1+@" and '"+"dt2'"
[/Quote]
就是这个地方不明白,不过好像你写的也不对呢
zjtpiaoxue 2010-05-25
  • 打赏
  • 举报
回复
string TSstr = @"select * from gzhz where 维修时间 between '"+dt1+@" and '"+"dt2'"
gootey 2010-05-25
  • 打赏
  • 举报
回复
顶一下

110,538

社区成员

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

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

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