如何从对 Oracle 执行的选择查询中获取数据表并将其显示在 MVC 的视图中?

发布于 2024-11-08 23:54:13 字数 1731 浏览 0 评论 0原文

我希望能够返回 Oracle 数据库中的一个表并将其发送到我的 MVC 结构中的视图中显示。对于我的模型,我的代码看起来像这样:

        public DataTable Show(int Poll_ID)
    {

        OleDbDataReader myOleDBDataReader = DBConn("SELECT * FROM MCQ_QUESTIONS WHERE Poll_ID = 1");

        DataSet dataSet = new DataSet();

        DataTable schemaTable = myOleDBDataReader.GetSchemaTable();
        DataTable dataTable = new DataTable();

        for (int cntr = 0; cntr < schemaTable.Rows.Count; ++cntr)
        {
            DataRow dataRow = schemaTable.Rows[cntr];
            string columnName = dataRow["ColumnName"].ToString();
            DataColumn column = new DataColumn(columnName, dataRow.GetType());
            dataTable.Columns.Add(column);
        }
        myOleDBDataReader.Close();
        myOleDbConnection.Close();
        return dataTable;

但它不起作用。任何帮助将不胜感激!我的控制器类的代码如下:

public ActionResult Details(int id)
    {
        PollModel poll = new PollModel();
        DataTable dt = new DataTable();
        dt = poll.Show(1);
        //ViewData["Poll"] = poll.Show();
        ViewData["Data"] = dt;
        //ViewData["Poll"] = "Testing";
        return View(dt);
    }

我认为的代码如下:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace = "MvcApplication3.Models" %>
<%@ Import Namespace = "System.Data" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.Encode(ViewData["Data"])%>

</asp:Content>

I want to be able to return a table in my oracle db and send it to be displayed in my view in my MVC structure. My code looks something like this for my model:

        public DataTable Show(int Poll_ID)
    {

        OleDbDataReader myOleDBDataReader = DBConn("SELECT * FROM MCQ_QUESTIONS WHERE Poll_ID = 1");

        DataSet dataSet = new DataSet();

        DataTable schemaTable = myOleDBDataReader.GetSchemaTable();
        DataTable dataTable = new DataTable();

        for (int cntr = 0; cntr < schemaTable.Rows.Count; ++cntr)
        {
            DataRow dataRow = schemaTable.Rows[cntr];
            string columnName = dataRow["ColumnName"].ToString();
            DataColumn column = new DataColumn(columnName, dataRow.GetType());
            dataTable.Columns.Add(column);
        }
        myOleDBDataReader.Close();
        myOleDbConnection.Close();
        return dataTable;

But its not working. Any help is appreciated guys!! Code from my Controller class is as follows:

public ActionResult Details(int id)
    {
        PollModel poll = new PollModel();
        DataTable dt = new DataTable();
        dt = poll.Show(1);
        //ViewData["Poll"] = poll.Show();
        ViewData["Data"] = dt;
        //ViewData["Poll"] = "Testing";
        return View(dt);
    }

Code in my view is as follows :

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace = "MvcApplication3.Models" %>
<%@ Import Namespace = "System.Data" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.Encode(ViewData["Data"])%>

</asp:Content>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如痴如狂 2024-11-15 23:54:13

我建议您的第一件事是使用 ODP.NET 驱动程序 访问 Oracle 数据库而不是 ODBC。我建议您的第二件事是使用模型而不是经典 WebForms 中使用的 DataSetDataTables (即使它们不应该有)但在 ASP.NET MVC 中没有。

因此,一旦您从 Oracle 下载了正确的驱动程序,就可以开始编码了。首先描述什么是问题。例如:

public class Question
{
    public int Id { get; set; }
    public string Title { get; set; }
}

然后编写一个存储库方法,该方法将返回问题列表:

public IEnumerable<Question> GetQuestions()
{
    using (var conn = new OracleConnection("put your CS string here or fetch from app.config"))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT MCQ_ID, MCQ_TITLE FROM MCQ_QUESTIONS WHERE Poll_ID = 1";
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return new Question
                {
                    Id = reader.GetInt32(reader.GetOrdinal("MCQ_ID")),
                    Title = reader.GetString(reader.GetOrdinal("MCQ_TITLE"))
                };
            }
        }
    }
}

现在我们可以转到控制器:

public ActionResult Index()
{
    var questions = repository.GetQuestions().ToArray();
    return View(questions);
}

最后是相应的强类型视图:

<%@ Page Title="" 
         Language="C#" 
         MasterPageFile="~/Views/Shared/Site.Master" 
         Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication3.Models.Question>>" 
%>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>Title</th>
            </tr>
        </thead>
        <tbody>
            <%= Html.DisplayForModel() %>
        </tbody>
    </table>
</asp:Content>

最后在相应的显示模板中(~/Views/Shared/ DisplayTemplates/Question.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<MvcApplication3.Models.Question>" 
%>
<tr>
    <td><%= Html.DisplayFor(x => x.Id) %></td>
    <td><%= Html.DisplayFor(x => x.Title) %></td>
</tr>

您还可以在此处查看有关使用实体框架的教程 ORM 以避免在代码中编写 SQL 查询。

The first thing I would recommend you is to use the ODP.NET driver to access an Oracle database instead of ODBC. The second thing I would recommend you is to use models instead of DataSet and DataTables which were used in classic WebForms (even if they shouldn't have) but not in ASP.NET MVC.

So once you have downloaded the proper driver from Oracle let's get to coding. Start by describing what a Question is. For example:

public class Question
{
    public int Id { get; set; }
    public string Title { get; set; }
}

then write a repository method which will return a list of questions:

public IEnumerable<Question> GetQuestions()
{
    using (var conn = new OracleConnection("put your CS string here or fetch from app.config"))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT MCQ_ID, MCQ_TITLE FROM MCQ_QUESTIONS WHERE Poll_ID = 1";
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return new Question
                {
                    Id = reader.GetInt32(reader.GetOrdinal("MCQ_ID")),
                    Title = reader.GetString(reader.GetOrdinal("MCQ_TITLE"))
                };
            }
        }
    }
}

Now we can move on to the controller:

public ActionResult Index()
{
    var questions = repository.GetQuestions().ToArray();
    return View(questions);
}

And finally the corresponding strongly typed view:

<%@ Page Title="" 
         Language="C#" 
         MasterPageFile="~/Views/Shared/Site.Master" 
         Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication3.Models.Question>>" 
%>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>Title</th>
            </tr>
        </thead>
        <tbody>
            <%= Html.DisplayForModel() %>
        </tbody>
    </table>
</asp:Content>

And finally in the corresponding display template (~/Views/Shared/DisplayTemplates/Question.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<MvcApplication3.Models.Question>" 
%>
<tr>
    <td><%= Html.DisplayFor(x => x.Id) %></td>
    <td><%= Html.DisplayFor(x => x.Title) %></td>
</tr>

You may also checkout the tutorials here about using Entity Framework as an ORM to avoid writing SQL queries in your code.

终陌 2024-11-15 23:54:13

尝试查看此处:

在 MVC 中显示标准 DataTables

这可能很有趣:

 <% foreach(System.Data.DataRow row in Model.Rows) { %>
    <tr>
        <% foreach (var cell in row.ItemArray) {%>
            <td><%=cell.ToString() %></td>
        <%} %>
    </tr>
<%} %>         

try looking here:

Displaying standard DataTables in MVC

this can be interesting:

 <% foreach(System.Data.DataRow row in Model.Rows) { %>
    <tr>
        <% foreach (var cell in row.ItemArray) {%>
            <td><%=cell.ToString() %></td>
        <%} %>
    </tr>
<%} %>         
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文