小白提问 '=' 附近有语法错误。

olive1223 2018-01-02 10:27:51

想通过修改sql语句查询时间的方式来选定时间查询报表。比如在textbox中输入2013-1-1这样的时间(#滑稽)
这并不是什么好方法,然而这样最直接。
现在的问题是在textBox输入时间之后就会跳到sqldbhelper然后显示 '=' 附近有语法错误。
最终做出来是个饼图。
有知道的大师指点一下不胜感激!!

sqldbhelper部分
public DataTable ExecuteDataTable(string sql, CommandType commandType, SqlParameter[] parameters)
{
DataTable data = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.CommandType = commandType;
if (parameters != null)
{
foreach (SqlParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
}
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
try
{
adapter.Fill(data);
}
catch (Exception e)
{
throw e;
}
}
}
return data;
}

报表部分
private void button1_Click(object sender, EventArgs e)
{
string sql = "select TypeName,IncomeExpendTypeName,AccountMoney,a.Remark from dbo.IncomeExpendDet a join dbo.IncomeExpendType b on a.IncomeExpendTypeid=b.IncomeExpendTypeid where IEDatetime between= '" + textBox1.Text + "'And ='" + textBox2.Text + "'";
SqlDbHelper sdh = new SqlDbHelper();
SqlParameter[] parameters = new SqlParameter[] { };
DataTable dt = sdh.ExecuteDataTable(sql, CommandType.Text, parameters);
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet2", dt));
this.reportViewer1.RefreshReport();
}
...全文
676 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
leo_lesley 2018-01-03
  • 打赏
  • 举报
回复
点击按钮之前你需要先判断一下,textBox1和textBox2 中输入的是否是合法的日期格式值
olive1223 2018-01-03
  • 打赏
  • 举报
回复
我改成datetimepicker.text之后会显示从字符串转换日期和/或时间时,转换失败
怎么解决
cyg17173 2018-01-03
  • 打赏
  • 举报
回复
重要事项 不同的数据库对 BETWEEN...AND 操作符的处理方式是有差异的。某些数据库会列出介于 "Adams" 和 "Carter" 之间的人,但不包括 "Adams" 和 "Carter" ;某些数据库会列出介于 "Adams" 和 "Carter" 之间并包括 "Adams" 和 "Carter" 的人;而另一些数据库会列出介于 "Adams" 和 "Carter" 之间的人,包括 "Adams" ,但不包括 "Carter" 。 所以,请检查你的数据库是如何处理 BETWEEN....AND 操作符的! 一般情况 SQL Server中 between and是包括边界值的,not between不包括边界值,不过如果使用between and 限定日期需要注意,如果and后的日期是到天的,那么默认为00:00:00 例如:and 后的日期为2017年04月06日,就等价于2017-04-06 00:00:00 ,那么2017-04-06 15:10:34的数据就差不到了,需要进行to_char处理。
豆芽儿妹妹 2018-01-03
  • 打赏
  • 举报
回复
between= '" + textBox1.Text + "'And ='" + textBox2.Text + "'" 嗯嗯 between... and... 没有等于号才对
FainSheeg 2018-01-03
  • 打赏
  • 举报
回复
另外,看上去好像 ' 两边还缺少空格
FainSheeg 2018-01-03
  • 打赏
  • 举报
回复
很简单的语法错误,between...and...,根本不需要=
Hesitate_ 2018-01-03
  • 打赏
  • 举报
回复
SQL语法错误,你先把你的SQL在数据库测试好了再说
-小蕾- 2018-01-03
  • 打赏
  • 举报
回复
sql问题一般调试出来整句sql语句之后,复制到数据库执行看看,很容易找到问题所在。
threenewbee 2018-01-03
  • 打赏
  • 举报
回复
And ='" + textBox2.Text + "'"; and是关键字,不能作为字段,或者你忘记写字段了。
秋的红果实 2018-01-02
  • 打赏
  • 举报
回复
where IEDatetime between '" + textBox1.Text + "' And  '" + textBox2.Text + "'";
秋的红果实 2018-01-02
  • 打赏
  • 举报
回复
至少这句有问题 where IEDatetime between= '" + textBox1.Text + "'And ='" + textBox2.Text + "'"; ==> where IEDatetime between '" + textBox1.Text + " And  '" + textBox2.Text + "'";
olive1223 2018-01-02
  • 打赏
  • 举报
回复
'=' 附近有语法错误这句话显示在sqldbhelper { throw e; } 这里

110,500

社区成员

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

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

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