如何从对 Oracle 执行的选择查询中获取数据表并将其显示在 MVC 的视图中?
我希望能够返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您的第一件事是使用 ODP.NET 驱动程序 访问 Oracle 数据库而不是 ODBC。我建议您的第二件事是使用模型而不是经典
WebForms
中使用的DataSet
和DataTables
(即使它们不应该有)但在 ASP.NET MVC 中没有。因此,一旦您从 Oracle 下载了正确的驱动程序,就可以开始编码了。首先描述什么是
问题
。例如:然后编写一个存储库方法,该方法将返回问题列表:
现在我们可以转到控制器:
最后是相应的强类型视图:
最后在相应的显示模板中(
~/Views/Shared/ DisplayTemplates/Question.ascx
):您还可以在此处查看有关使用实体框架的教程 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
andDataTables
which were used in classicWebForms
(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:then write a repository method which will return a list of questions:
Now we can move on to the controller:
And finally the corresponding strongly typed view:
And finally in the corresponding display template (
~/Views/Shared/DisplayTemplates/Question.ascx
):You may also checkout the tutorials here about using Entity Framework as an ORM to avoid writing SQL queries in your code.
尝试查看此处:
在 MVC 中显示标准 DataTables
这可能很有趣:
try looking here:
Displaying standard DataTables in MVC
this can be interesting: