[英]How do I retrieve text from database in list form (SQL Server & ASP.NET MVC)
I have inserted following paragraph in database: 我在数据库中插入了以下段落:
Reviewing current systems.Presenting ideas for system improvements, including cost proposals. 审查当前系统。提出改进系统的想法,包括成本建议。 Working closely with analysts, designers and staff. 与分析师,设计师和员工紧密合作。 Producing detailed specifications and writing the program codes. 产生详细的规范并编写程序代码。 Testing the product in controlled, real situations before going live. 在上线之前,请在受控的真实环境中测试产品。 Preparation of training manuals for users 为用户准备培训手册
Maintaining the systems once they are up and running. 一旦系统启动并运行,便对其进行维护。
And I wanted to retrieve it like this: 我想这样检索它:
• Reviewing current systems •审查当前系统
• Presenting ideas for system improvements, including cost proposals •提出系统改进的想法,包括成本建议
• Working closely with analysts, designers and staff •与分析师,设计师和员工紧密合作
• Producing detailed specifications and writing the program codes •制定详细的规范并编写程序代码
• Testing the product in controlled, real situations before going live •上线之前在受控的真实环境中测试产品
• Preparation of training manuals for users •为用户编写培训手册
• Maintaining the systems once they are up and running •在系统启动并运行后对其进行维护
in an ASP.NET MVC frontend. 在ASP.NET MVC前端中。
Without knowing anything about your application setup, database schema, and exactly what portion of this task you actually need help with; 在不了解有关应用程序设置,数据库架构以及您实际上需要帮助的确切部分的情况下; you are getting a more generic answer which may or may not match your architecture. 您会得到一个更通用的答案,该答案可能与您的体系结构不匹配。
Part 1 will be the database table I am using for this 第1部分将是我为此使用的数据库表
CREATE TABLE dbo.PageContent (
PageID INT IDENTITY(1,1) NOT NULL,
PageText NVARCHAR(MAX) NULL DEFAULT(''),
CONSTRAINT PK_PageContent_PageID PRIMARY KEY CLUSTERED ([PageID] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT PageContent(PageText) VALUES ('Reviewing cur... and running.')
GO
Part 2 will be the (data) Model. 第2部分将是(数据)模型。 No mention of an ORM or DB schema means I am writing this in ADO. 没有提到ORM或DB模式,这意味着我是用ADO编写的。 Please note that as the overloaded method is only retrieving 1 value of data, I have implemented the Scalar
method to save the overhead of a Reader
请注意,由于重载方法仅检索1个数据值,因此我实现了Scalar
方法以节省Reader
的开销
public class PageContent {
public int PageID { get; set; }
public string PageText {get; set; }
public PageContent() {}
public PageContent (int ContentID) {
PageID = ContentID;
using (SqlConnection conn = new SqlConnection(YourConnString)) {
using (SqlCommand cmd = new SqlCommand("SELECT PageText FROM PageContent WHERE (PageID = @PageID)", conn);
cmd.CommandType = CommandType.Text;
cmd.Paramaters.AddWithValue("@PageID", PageID);
try {
conn.Open();
PageText = (string)cmd.ExecuteScalar();
}
catch (Exception ex) {
PageText = "An Error has occurred";
// your error handling here
}
finally { conn.Close(); }
}
}
}
}
Part 3 will be the Controller and a basic Action to get the correct Model based on the ID. 第3部分将是Controller和一个基本的Action,用于根据ID获得正确的模型。 Unless you are going to be sorting or other operations, there is no need to use a List<>
, you can just use an array and save some overhead. 除非您要进行排序或其他操作,否则无需使用List<>
,您可以只使用数组并节省一些开销。
public ActionResult RetrievePageText(int ContentID) {
PageContent PC = New PageContent(ContentID);
string[] PageLines = PC.PageText.split('.');
return View(PageLines);
}
Part 4, the end is near with the View. 第4部分,结束于View。 You will need to write your own header line and HTML. 您将需要编写自己的标题行和HTML。 This will be using the ASPX view engine, and if you are Razor or other you will need to transpose it. 这将使用ASPX视图引擎,如果您是Razor或其他人,则需要对其进行转置。
<ul>
<% foreach (string line in Model) { %>
<li><% =line %></li>
<% } %>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.