简体   繁体   中英

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.

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

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. No mention of an ORM or DB schema means I am writing this in 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

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. 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.

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. You will need to write your own header line and HTML. This will be using the ASPX view engine, and if you are Razor or other you will need to transpose it.

<ul>
    <% foreach (string line in Model) { %>
        <li><% =line %></li>
    <% } %>
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM