是的,有这里的问题和这里关于如何使用dapper插入记录.然而,这些答案虽然内容丰富,但似乎并没有给我指明正确的方向。情况如下:将数据从SqlServer移动到MySql。将记录读入IEnumerable<WTUser>是很容易的,但我只是没有从插入中得到一些东西。首先,“移动记录代码”:
// moving data
Dim session As New Session(DataProvider.MSSql, "server", _
"database")
Dim resources As List(Of WTUser) = session.QueryReader(Of WTUser)("select * from tbl_resource")
session = New Session(DataProvider.MySql, "server", "database", _
"user", "p@$$w0rd")
// *edit* - corrected parameter notation with '@'
Dim strInsert = "INSERT INTO tbl_resource (ResourceName, ResourceRate, ResourceTypeID, ActiveYN) " & _
"VALUES (@ResourceName, @ResourceRate, @ResourceType, @ActiveYN)"
Dim recordCount = session.WriteData(Of WTUser)(strInsert, resources)
// session Methods
Public Function QueryReader(Of TEntity As {Class, New})(ByVal Command As String) _
As IEnumerable(Of TEntity)
Dim list As IEnumerable(Of TEntity)
Dim cnn As IDbConnection = dataAgent.NewConnection
list = cnn.Query(Of TEntity)(Command, Nothing, Nothing, True, 0, CommandType.Text).ToList()
Return list
End Function
Public Function WriteData(Of TEntity As {Class, New})(ByVal Command As String, ByVal Entities As IEnumerable(Of TEntity)) _
As Integer
Dim cnn As IDbConnection = dataAgent.NewConnection
// *edit* if I do this I get the correct properties, but no data inserted
//Return cnn.Execute(Command, New TEntity(), Nothing, 15, CommandType.Text)
// original Return statement
Return cnn.Execute(Command, Entities, Nothing, 15, CommandType.Text)
End Functioncnn.Query和cnn.Execute调用dapper扩展方法。现在,WTUser类(注意:列名从SqlServer中的“WindowsName”更改为MySql中的“ResourceName”,因此两个属性指向相同的字段):
Public Class WTUser
// edited for brevity - assume the following all have public get/set methods
Public ActiveYN As String
Public ResourceID As Integer
Public ResourceRate As Integer
Public ResourceType As Integer
Public WindowsName As String
Public ResourceName As String
End Class我收到了Dapper的一个异常:“Dapper不支持WTUser。”这种方法在DataMapper (Dapper)中:
private static Action<IDbCommand, object> CreateParamInfoGenerator(Type OwnerType)
{
string dmName = string.Format("ParamInfo{0}", Guid.NewGuid());
Type[] objTypes = new[] { typeof(IDbCommand), typeof(object) };
var dm = new DynamicMethod(dmName, null, objTypes, OwnerType, true); // << - here
// emit stuff
// dm is instanced, now ...
foreach (var prop in OwnerType.GetProperties().OrderBy(p => p.Name))此时OwnerType =
System.Collections.Generic.List`1[CRMBackEnd.WTUser,CRMBE,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null],Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089
看来OwnerType应该是CRMBackEnd.WTUser ..。不是List<CRMBackEnd.WTUser> .?因为正在发生的是正在迭代集合属性:计数、容量等。,我缺少什么?
更新
如果我将session.WriteData修改为:
Public Function WriteData(Of TEntity As {Class, New})(ByVal Command As String, _
ByVal Entities As IEnumerable(Of TEntity)) _
As Integer
Dim cnn As IDbConnection = dataAgent.NewConnection
Dim records As Integer
For Each entity As TEntity In Entities
records += cnn.Execute(Command, entity, Nothing, 15, CommandType.Text)
Next
Return records
End Function记录插入得很好..。但我认为没有必要举这样的例子:
connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
).IsEqualTo(3); // 3 rows inserted: "1,1", "2,2" and "3,3" ..。来自脱衣舞网
发布于 2011-06-28 02:05:47
我刚加入了一个测试:
class Student
{
public string Name {get; set;}
public int Age { get; set; }
}
public void TestExecuteMultipleCommandStrongType()
{
connection.Execute("create table #t(Name nvarchar(max), Age int)");
int tally = connection.Execute(@"insert #t (Name,Age) values(@Name, @Age)", new List<Student>
{
new Student{Age = 1, Name = "sam"},
new Student{Age = 2, Name = "bob"}
});
int sum = connection.Query<int>("select sum(Age) from #t drop table #t").First();
tally.IsEqualTo(2);
sum.IsEqualTo(3);
}就像广告上说的那样。我对multi的工作方式做了一些修改(所以它更快一些,并且支持object[])。
我猜您有问题,因为您在WTUser上的所有字段中都缺少一个getter属性。所有params都必须有读取器属性,我们不支持从字段中提取它,它需要一个复杂的解析步骤才能保持效率。
另一个引起问题的点是通过不支持映射的param传递dapper。
例如,以下类不支持param:
class Test
{
public int Id { get; set; }
public User User {get; set;}
}
cnn.Query("select * from Tests where Id = @Id", new Test{Id = 1}); // used to go boom 问题是,dapper没有对进行解析,而是假定所有道具都可以作为params设置,但无法解决User的SQL类型。
最新的rev解决了这一问题。
https://stackoverflow.com/questions/6387904
复制相似问题