简体   繁体   English

如何以列表形式从数据库中检索文本(SQL Server和ASP.NET MVC)

[英]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.

相关问题 如何在ASP.NET MVC中将表单提交到数据库中 - How do I submit a form into a database in asp.net MVC 如何从 SQL Server 数据库绑定 ASP.NET MVC 中的下拉列表? - How to bind dropdown list in ASP.NET MVC from SQL Server database? 如何在 ASP.NET MVC 5 中列出来自 SQL Server 的所有数据库名称? - How to list all database names from SQL Server in ASP.NET MVC 5? ASP.NET MVC无法从本地SQL Server Express数据库检索数据 - ASP.NET MVC unable to retrieve data from local SQL Server Express database 如何从ASP.NET MVC中的SQL Server数据库向多个收件人发送电子邮件? - How can I send email to multiple recipients from SQL Server database in ASP.NET MVC? 如何从sql数据库中检索asp.net mvc中的行数? - How to retrieve the count of the number of rows in asp.net mvc from sql database? SQL Server数据库中ASP.NET MVC中的Bootstrap滑块 - Bootstrap slider in ASP.NET MVC from SQL Server database 如何从 SQL Server 2005 中检索 asp.net 表单中的值 - How to retrieve the values in asp.net form from SQL Server 2005 如何使用 ASP.NET 从 SQL 服务器数据库中检索和显示 GridView 中的值 - How to retrieve and display values in GridView from SQL Server database in ASP.NET using C# 如何从Html.TextBox MVC 2 asp.net C#检索值? - How do I retrieve values from Html.TextBox MVC 2 asp.net C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM