如何将DataSet返回到View

bob*_*zzo 8 asp.net-mvc asp.net-mvc-4

我通过SqlDataAdapter向我的Sql框发送标准的Sql select语句,然后填充DataSet对象.

我可以访问生成的DataSet中的行,但是如何将DataSet转换为可以返回到MVC视图的List.即我假设一个List对象是处理这个问题的最佳方法.

这是我的控制器c#代码:

public class QAController : Controller
{

    private readonly static string connString = ConfigurationManager.ConnectionStrings["RegrDBConnection"].ToString();
    private readonly static SqlConnection sqlConn = new SqlConnection(connString);
    private readonly static SqlCommand sqlComm = new SqlCommand();

    public ActionResult Index()
    {
        DbRegressionExec();
        return View();
    }
    public static void DbRegressionExec()
    {
        // SELECT TABLE CONTENTS FROM SQL !!
        RegressDB_TableList regresDB = new RegressDB_TableList();
        string sqlStr = "select * from [RegressionResults].[dbo].[Diff_MasterList] order by TableName";

        // POPULATE DATASET OBJECT
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sqlStr, sqlConn);
        da.SelectCommand.CommandType = CommandType.Text;
        sqlConn.Open();
        try
        {
            da.Fill(ds, "RegresDB");
        }
        catch (Exception e)
        {
            throw;
        }
        finally
        {
            sqlConn.Close();
        }

       // I can iterate thru rows here, but HOW DO CONVERT TO A LIST OBJECT ????

        int numRows = ds.Tables["RegresDB"].Rows.Count;
        for (int i = 0; i < numRows; i++)
        {
            string tblName = ds.Tables["RegresDB"].Rows[i].Field<string>("TableName");
        }

        //List<RegressDB_TableList> masterList = regresDB.RegresTableList.ToList(); //not working !!
        //var masterList = regresDB.TableName.ToList(); //

    }

}
Run Code Online (Sandbox Code Playgroud)

我可能需要一个简单的课程来实现这一目标:

namespace RegressionMvc.Models
{
  public class RegresDB_TableName
  {
     public string TableName { get; set; }
  }
  public class RegressDB_TableList
  {
     public List<RegresDB_TableName> RegresTableList { get; set; }
  }
Run Code Online (Sandbox Code Playgroud)

}

最后,我试图找出从Sql Server处理DataSet结果的最佳方法,以及如何使它们回到MVC View.

我可以使用jQuery和Json,这意味着只需将数据字段转换为Json并返回JQuery,但我确信有几种方法可以处理基于Sql的结果集.

提前感谢您的建议......

最好,鲍勃

Dee*_*u T 5

在您的控制器中输入这样的代码

[HttpGet]
public ActionResult View(Modelclass viewmodel)
{
    List<Modelclass> employees = new List<Modelclass>();
    DataSet ds = viewmodel.GetAllAuthors();
    var empList = ds.Tables[0].AsEnumerable().Select(dataRow => new Modelclass{
       AuthorId = dataRow.Field<int>("AuthorId"),
        Fname = dataRow.Field<string>("FName"),
       Lname = dataRow.Field<string>("Lname")
    });
    var list = empList.ToList();



    return View(list);
}
Run Code Online (Sandbox Code Playgroud)

并在视野中

@{
var gd = new WebGrid(Model, canPage: true, rowsPerPage: 5, selectionFieldName: "selectedRow",ajaxUpdateContainerId: "gridContent");
    gd.Pager(WebGridPagerModes.NextPrevious);}
@gd.GetHtml(tableStyle: "table",

        columns: gd.Columns(
                 gd.Column("AuthorId", "AuthorId"),
                 gd.Column("Fname", " Fname"),
                 gd.Column("Lname", "Lname", style: "description")

 )) 
Run Code Online (Sandbox Code Playgroud)


Cha*_*ino 4

简短回答

直接回答你的问题:

var tableList = new List<RegresDB_TableName>();
int numRows = ds.Tables["RegresDB"].Rows.Count;
for (int i = 0; i < numRows; i++)
{
    string tblName = ds.Tables["RegresDB"].Rows[i].Field<string>("TableName");
    tableList.Add(new RegresDB_TableName() { TableName = tblName };
}

return View(tableList);
Run Code Online (Sandbox Code Playgroud)

长答案(实际上更短)

尝试dapper-dot-net

您的代码可能会更改为以下内容:

string sqlStr = "SELECT * FROM [RegressionResults].[dbo].[Diff_MasterList] ORDER BY TableName";
return sqlConn.Query<RegresDB_TableName>(sqlStr);
Run Code Online (Sandbox Code Playgroud)