相关疑难解决方法(0)

使用Dapper执行插入和更新

我对使用Dapper很感兴趣 - 但据我所知它只支持Query和Execute.我没有看到Dapper包含一种插入和更新对象的方法.

鉴于我们的项目(大多数项目?)需要进行插入和更新,在插入和更新的同时进行插入和更新的最佳做法是什么?

最好我们不必采用参数构建的ADO.NET方法等.

我现在能想出的最佳答案是使用LinqToSQL进行插入和更新.有更好的答案吗?

c# orm dapper

175
推荐指数
6
解决办法
18万
查看次数

Dapper-Dot-Net示例代码

这是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的方法.小巧玲珑显然效果很好,所以我做错了什么?

sample dapper

7
推荐指数
2
解决办法
5855
查看次数

从存储过程结果创建对象

我们有一个现有的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# linq sql-server stored-procedures

7
推荐指数
1
解决办法
4765
查看次数

仅当我指定 commandType 时,错误找不到 Dapper 和 c# 的存储过程

我在 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)

c# dapper

6
推荐指数
1
解决办法
1949
查看次数

像 DataAdapter 这样的 Dapper 命令

拜托,我可以将此方法转换为使用 Dapper 吗?

  • “BuscarUltimasLeituras”是一个程序
  • “@Last”是过程的参数(int)

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)

c# stored-procedures sqldataadapter dapper

1
推荐指数
1
解决办法
1966
查看次数

标签 统计

c# ×4

dapper ×4

stored-procedures ×2

linq ×1

orm ×1

sample ×1

sql-server ×1

sqldataadapter ×1