将 DbDataReader 的结果转换为 ASP.NET MVC 4 中的数据库模型,来自使用 ADO.NET 的存储过程

Translate result of DbDataReader to database model in ASP.NET MVC 4, coming from a stored procedure using ADO.NET

我有一个用户定义的存储过程,其中 returns 多个实际表作为结果集:

CREATE PROCEDURE uspDemo
    (@UserID BIGINT = 0,
     @IsAdmin BIT = 0,
     @Title VARCHAR(120) = '')
AS
BEGIN 
    ------Retrieve Posts------
    SELECT * 
    FROM tblPost AS MP 
    INNER JOIN tblUserProfile AS UP ON UP.ID = MP.UserID
    WHERE UP.ID = @UserID 
      AND ((@IsAdmin = 0 AND MP.IsDeleted = 0 AND MP.IsApproved = 1)
           OR (@IsAdmin = 1 OR MP.IsDeleted = 0 OR MP.IsApproved = 1))

    ----- Retrieve Tags------
    SELECT *  
    FROM tblTagMasters AS MT 
    INNER JOIN tblPostTags AS MP ON MT.TagID = MP.TagID
 
    --------Retrieve User likes-----
    SELECT * 
    FROM tblUserLikes AS UV 
    INNER JOIN tblPost AS MP ON MP.PostId = UV.PostId
END

如何在 ASP.NET MVC 中翻译?请帮助我。

基本上,您需要这样的东西:

// define connection string and command for your query
string connectionString = ".....";    // typically load from config
string storedProcedureName = "dbo.uspDemo";

// put your disposable ADO.NET objects into proper "using ()" blocks    
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(storedProcedureName, conn))
{
    cmd.CommandType = CommandType.StoredProcedure;
    
    // define the parameters
    cmd.Parameters.Add("@UserID", SqlDbType.BigInt).Value = .....;
    cmd.Parameters.Add("@IsAdmin", SqlDbType.Bit).Value = .....;
    cmd.Parameters.Add("@Title", SqlDbType.VarChar, 120).Value = ".....";

    // open connection
    conn.Open();

    // execute stored procedure to get the data reader
    using (SqlDataReader reader = cmd.ExecuteReader())
    {
        // read the first set of data returned
        while (reader.Read())
        {
            // read the values, construct an object from it, store the object into a list or something
        }
        
        // get the next set of result data
        reader.NextResult();
        
        // read the second set of data returned
        while (reader.Read())
        {
            // read the values, construct an object from it, store the object into a list or something
        }
        
        // get the next set of result data
        reader.NextResult();
        
        // read the third set of data returned
        while (reader.Read())
        {
            // read the values, construct an object from it, store the object into a list or something
        }
    }

    conn.Close();
}

剩下的细节留给你 - 做一些研究!