VS2017一步一步断点调试解决Dapper语句出现的Bug
2018-04-19 18:55
357 查看
最近再做一个项目,出现一个小bug,bug虽小,但是却要命啊。下面我show下我解决问题的方法。
View层代码:
@model List<mhq.Blog.Model.Blog> <blockquote class="layui-elem-quote">文章管理</blockquote> <div style="padding:10px;"> <div> <a class="layui-btn" href="~/Admin/Blog/Add"> <i class="layui-icon"></i> 添加 </a> </div> <table class="layui-table"> <thead> <tr> <th>发布时间</th> <th>boke标题</th> <th>所属分类</th> <th>访问量</th> <th>排序号</th> <th>管理</th> </tr> </thead> <tbody> @foreach (var blog in Model) { <tr> <td>@blog.createdate.ToString("yyyy-MM-dd HH:mm")</td> <td>@blog.title</td> <td>@blog.caname</td> <td>@blog.visitnum</td> <td>@blog.sort</td> <td> <a style="margin-right:20px" href="~/Admin/Blog/Add/@blog.id"> <i class="layui-icon"></i> 编辑 </a> <a href="javascript:void()0;" onclick="del(@blog.id)"> <i class="layui-icon"></i> 删除 </a> </td> </tr> } </tbody> </table> </div> <script> /* 删除*/ function del(id) { layui.use('layer', function () { var layer = layui.layer; var $ = layui.jquery; layer.confirm("是否确认删除?", function () { var url = "/Admin/Blog/Del/" + id; $.post(url, function (data) { layer.alert(data, function () { location.reload(); }); }) }) }) } </script>
Controller层代码:
public IActionResult Index() { List<Model.Blog> list = dal.GetList(" 1=1 order by sort asc,id desc"); return View(list); }
Dal层代码:
/// <summary> /// 查询 /// </summary> /// <param name="cond">查询条件</param> /// <returns></returns> public List<Model.Blog> GetList(string cond) { // Dapper – Simple List using (var connection = ConnectionFactory.GetOpenConnection()) { string sql = "select * from blog "; if (!string.IsNullOrEmpty(cond)) { sql=sql+$" where{cond}"; } var list = connection.Query<Model.Blog>(sql).ToList(); return list; } }
我这里出现的错误是“System.Data.SqlClient.SqlException:““=”附近有语法错误。”如图:
解决方法:
1、首先在此处方法上添加断点,如图:
2、运行程序到此断点(F5运行程序)
3、单步运行(F11)
对sql、 list添加监视,我们可以清楚的看到list里面的查询语句为:
select * from blogwhere1=1 order by sort asc,id desc
显然这是错误的查询语句。
如图:
正确的应为:
select * from blog where 1=1 order by sort asc,id desc
也就是说blog与where之间和where与1=1之间缺少空格。
所以我们只要在相关的代码中加上空格就OK了。
相关文章推荐
- Think PHP关闭调试模式后出现奇怪的bug的解决方法.
- 程序调试中出现的Bug与解决方法
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法(.net的一个BUG)
- Xcode7以后断点调试会经常出现要调试的对象为nil,但是当你打印时值是存在的,解决办法
- VS2017 启动调试出现 无法启动程序“http://localhost:15613” 操作在当前状态中是非法的。 同时附加进程也是错误的解决方法
- 解决附加进程调试时出现"无法命中断点,还没有为文档加载任何符号这个问题"
- codeblocks出现无法调试或断点失效问题解决办法
- [Xcode]_[解决Xcode设置断点,调试窗口出现反汇编代码的异常情况]
- vs2008调试出现异常时触发断点位置和堆栈不正确的解决
- 解决一个只在调试运行状态下才出现的bug
- 调试Bug的神兵利器:通过WinDbg条件断点收集Log
- 安装IE7 Visual Studio出现调试不够权限的解决办法
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法
- VS2005调试dll时提示:“当前不会命中断点,因为还没有为该文档加载任何符号”解决方法From Eric Qu
- 解决eclipse中断点调试不起作用的问题
- VC6调试时出现的does not contain debugging information提示解决办法!
- 解决VS调试时断点不会命中
- IE6 发送奇数个汉字出现的BUG机器解决之道
- VS调试 不会命中断点,源代码与原始版本不同的解决办法
- XCode调试技巧之EXC_BAD_ACCESS中BUG解决