我对使用Dapper很感兴趣 - 但据我所知它只支持Query和Execute.我没有看到Dapper包含一种插入和更新对象的方法.
鉴于我们的项目(大多数项目?)需要进行插入和更新,在插入和更新的同时进行插入和更新的最佳做法是什么?
最好我们不必采用参数构建的ADO.NET方法等.
我现在能想出的最佳答案是使用LinqToSQL进行插入和更新.有更好的答案吗?
这是Dapper示例中的代码切割:
var p = new DynamicParameters();
p.Add("@a", 11);
p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
cnn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure);
int b = p.Get("@b");
int c = p.Get("@c");
Run Code Online (Sandbox Code Playgroud)
任何人:在上面提供的示例代码中,我收到错误,"无法解析.Execute" - 指的是cnn.Execute.我查看连接对象,没有Execute的方法.小巧玲珑显然效果很好,所以我做错了什么?
我们有一个现有的SQL Server存储过程以及以下查询.我们需要Student从查询结果中创建以下类设计中的对象集合.
从SqlDataReader使用中创建对象的最佳方法是什么LINQ?
注意:我SqlDataReader只使用; 没有ORM
询问
SELECT
S.StudentID, S.StudentName, E.ExamID, E.ExamName, SE.Mark
FROM
StudentExam SE
INNER JOIN
Student S ON S.StudentID = SE.StudentID
INNER JOIN
Exam E ON E.ExamID = SE.ExamID
Run Code Online (Sandbox Code Playgroud)
类
public class ExamMark
{
public int ExamID { get; set; }
public string ExamName { get; set; }
public int Mark { get; set; }
}
public class Student
{
public int StudentID { get; set; }
public string StudentName …Run Code Online (Sandbox Code Playgroud) 我在 C# 和 SQL Server 中使用 Dapper。我在 SQL Server 中有一个存储过程,可以将一个人插入到我的数据库中。问题是,当我在查询中指定它是一个存储过程时,我得到
找不到存储过程
但没有使用commandType,一切正常。
例如:我有以下代码:
var result = connection.Execute("KBunnyGame_Players_InsertPlayer @Name, @Score, @FacebookId", player, commandType: CommandType.StoredProcedure);
Run Code Online (Sandbox Code Playgroud)
我不断收到错误消息,但是在我更改为:
var result = connection.Execute("KBunnyGame_Players_InsertPlayer @Name, @Score, @FacebookId", player);
Run Code Online (Sandbox Code Playgroud)
我没有再收到错误消息,并且播放器被插入到数据库中。我不知道这是否是预期的行为,对我来说似乎很奇怪。有没有我可以遵循的文件?
编辑:
存储过程代码:
ALTER PROCEDURE [dbo].[KBunnyGame_Players_InsertPlayer]
@Name NCHAR(20),
@Score INT,
@FacebookId NCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO KBunnyGame_Players (name, score, facebookId)
VALUES (@Name, @Score, @FacebookId);
END
Run Code Online (Sandbox Code Playgroud) 拜托,我可以将此方法转换为使用 Dapper 吗?
public DataTable GetLeituras ( int ultimas )
{
DataTable listaLeiturasRomaneio = new DataTable();
try
{
_sqlConnection.Open();
_sqlCommand = new SqlCommand();
_sqlCommand.Connection = _sqlConnection;
_sqlCommand.CommandText = "BuscarUltimasLeituras";
_sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
_sqlCommand.Parameters.Add("@Last", SqlDbType.Int).Value = ultimas;
_sqlAdapter = new SqlDataAdapter(_sqlCommand);
_sqlAdapter.Fill(listaLeiturasRomaneio);
}
catch ( SqlException )
{
listaLeiturasRomaneio = null;
}
finally
{
_sqlConnection.Close();
}
return listaLeiturasRomaneio;
}
Run Code Online (Sandbox Code Playgroud)