C#怎么从SQL数据库通过时间和指定内容查询数据

until_xx 2017-09-21 03:25:07
如题啊如题啊
//这个是指定时间查找的
if ((pn_textBox.Text == "") && (sn_textBox.Text == "") && (startdate_dateTimePicker.Text != "") && (enddate_dateTimePicker.Text != ""))
{
command = command1;
}
//这个是指定sn内容查询
if ((sn_textBox.Text != "") && (pn_textBox.Text == ""))
{
command = command2;
}
//这个是指定pn内容查询--------------------------但是这个查询就是不受时间控制会把所有PN内容相同的查出来,现在的问题就是我想查询我指定日期的这个PN内容的数据
if ((sn_textBox.Text == "") && (pn_textBox.Text != ""))
{
command = command3;
}
listView1.Items.Clear();
...全文
616 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
until_xx 2017-09-22
  • 打赏
  • 举报
回复
也很谢谢大家的帮助。很感激的
until_xx 2017-09-22
  • 打赏
  • 举报
回复
额 解决了,还是老板出手解决的。我C#都 没学过直接就丢项目给我了。我昨天才知道SQL这个东西的。
吉普赛的歌 2017-09-22
  • 打赏
  • 举报
回复
#8是对的, 另外建议你用 SQLHelper, 做到参数化。
及时雨送浆 2017-09-22
  • 打赏
  • 举报
回复
引用 9 楼 until_xx 的回复:
额 解决了,还是老板出手解决的。我C#都 没学过直接就丢项目给我了。我昨天才知道SQL这个东西的。
这么怎么开发呢?
大然然 2017-09-21
  • 打赏
  • 举报
回复
string sql = "select * from FirstTest_Record_Table where 1=1 "; if(pn_textBox.Text != "") { sql = sql + " and pn = '" + pn_textBox.Text +"'"; } if(sn_textBox.Text != "") { sql = sql + " and sn = '" + sn_textBox.Text +"'"; } if( (startdate_dateTimePicker.Text != "") && (enddate_dateTimePicker.Text != "")) { sql = sql + " and [TestDate] between '" + startdate_dateTimePicker.Text + “ and ‘’” + enddate_dateTimePicker.Text; }
until_xx 2017-09-21
  • 打赏
  • 举报
回复
怎么样拼接啊。小白啊
until_xx 2017-09-21
  • 打赏
  • 举报
回复
补充一点 command1 = string.Format("select [TestDate],[FibertopBN],[FibertopSN],[FibertopPN],[SN],[PN],[VN],[Date],[TxPower],[TxBias],[TxER],[TxCrossing],[TxJitterRMS],[TxJitterPP],[Sensitivity],[sn(f)] from FirstTest_Record_Table where [TestDate] between '{0}' and '{1}' ORDER BY TestDate Asc", startdate, enddate); command2 = string.Format("select [TestDate],[FibertopBN],[FibertopSN],[FibertopPN],[SN],[PN],[VN],[Date],[TxPower],[TxBias],[TxER],[TxCrossing],[TxJitterRMS],[TxJitterPP],[Sensitivity],[sn(f)] from FirstTest_Record_Table where [SN]='{0}'", sn); command3 = string.Format("select [TestDate],[FibertopBN],[FibertopSN],[FibertopPN],[SN],[PN],[VN],[Date],[TxPower],[TxBias],[TxER],[TxCrossing],[TxJitterRMS],[TxJitterPP],[Sensitivity],[sn(f)] from FirstTest_Record_Table where [PN]='{0}'", pn);
until_xx 2017-09-21
  • 打赏
  • 举报
回复
楼上的大然然,你是让我加一句 string sql = select * from table where 1=1; ? 然后把 if ((sn_textBox.Text == "") && (pn_textBox.Text != "")) { command = command3; } 改成 if(pn_textBox.Text != "") { sql =sql + " and pn =" +pn_textBox.Text; }
until_xx 2017-09-21
  • 打赏
  • 举报
回复
snlixing 2017-09-21
  • 打赏
  • 举报
回复
根据不同条件拼接sql吧
大然然 2017-09-21
  • 打赏
  • 举报
回复
string sql = select * from table where 1=1; 然后根据你的条件 也就是 txtbox是否为空在后面加 后续条件 比如 if(pn_textBox.Text != "") { sql =sql + " and pn =" +pn_textBox.Text; } 以此类推
CheapTrick 2017-09-21
  • 打赏
  • 举报
回复
把sql语句发出来?单独把sql语句在数据库中能查到自己想要的数据不?

110,524

社区成员

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

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

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