Dapper的Execute方法在传入sql语句和对象做参数时总是出问题,请求大神支援啊

Fantasy1220 2015-01-28 10:12:28
这个项目是一个.net项目,用的是vs2013 ,Dapper版本是1.38。奇怪的是在另一个小测试项目中执行是没问题的,关键是在我这个正式的项目里执行出问题啊。
以下是关键代码和截图,请求各位帮助!!!
test te = new test
{
name = "fyw",
id = 1
};
const string str1 = @"update test set name = @name where id = @id;";
using (MySqlConnection conn = OpenConnection())
{
//var execute = conn.Execute(str1, new { id = 1,name = "ww" });//这条语句是可以的,没有问题
var execute = conn.Execute(str1, te);//这一条语句总是出问题
}



详细信息如下:
未处理MySql.Data.MySqlClient.MySqlException
HResult=-2147467259
Message=Fatal error encountered during command execution.
Source=MySql.Data
ErrorCode=-2147467259
Number=0
StackTrace:
在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
在 MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
在 Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader) 位置 d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:行号 3310
在 Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) 位置 d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:行号 1310
在 Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) 位置 d:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:行号 1185
在 SmartMining.Program.Main(String[] args)
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException: MySql.Data.MySqlClient.MySqlException
HResult=-2147467259
Message=Parameter '@name' must be defined.
Source=MySql.Data
ErrorCode=-2147467259
Number=0
StackTrace:
在 MySql.Data.MySqlClient.Statement.SerializeParameter(MySqlParameterCollection parameters, MySqlPacket packet, String parmName, Int32 parameterIndex)
在 MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql, MySqlParameterCollection parameters, MySqlPacket packet)
在 MySql.Data.MySqlClient.Statement.BindParameters()
在 MySql.Data.MySqlClient.Statement.Execute()
在 MySql.Data.MySqlClient.PreparableStatement.Execute()
在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
InnerException:


有点着急,迫切请求各位支援啊!!!
...全文
2241 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞荷扬菊_MOL 2017-09-30
  • 打赏
  • 举报
回复
对象的属性,本质是方法。你在IL中看一下,会发现,get,set是两个方法 。
  • 打赏
  • 举报
回复
引用 6 楼 patrickjiang 的回复:
@娃都会打酱油了 @Fantasy1220 楼上的意思是说,Dapper的Execute方法的参数只能是对象的属性,不能是一些字段,这样的理解对吗?
要用于传递DbParameter数据的只能是属性不能是字段,你理解的对的
patrickjiang 2015-07-26
  • 打赏
  • 举报
回复
@Fantasy1220
patrickjiang 2015-07-26
  • 打赏
  • 举报
回复
@娃都会打酱油了 @Fantasy1220 楼上的意思是说,Dapper的Execute方法的参数只能是对象的属性,不能是一些字段,这样的理解对吗?
joyhen 2015-01-28
  • 打赏
  • 举报
回复
注意 对象 和 匿名对象
Fantasy1220 2015-01-28
  • 打赏
  • 举报
回复
引用 3 楼 starfd 的回复:
dapper只认属性,不认字段 你的test类设定的id和name只是字段,不是属性吧? 报的错误就是没有定义Paramter
感谢您的建议,问题确实在于这里。
  • 打赏
  • 举报
回复
class test
{
     public string name{get;set;}
     public int id{get;set;}
}
虽然你是在学习,但建议你还是要了解下C#命名规范
  • 打赏
  • 举报
回复
dapper只认属性,不认字段 你的test类设定的id和name只是字段,不是属性吧? 报的错误就是没有定义Paramter
Fantasy1220 2015-01-28
  • 打赏
  • 举报
回复
能给讲的具体点吗,我应该怎么改呢

62,247

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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