简体   繁体   中英

c# Export Database to HTML formatted table

I am writing a simple application in C# and WPF for reporting. I have following table in a database of sqlite:

Table1:
    id
    name
    amntOne
    amntTwo
    entryDate

Table2:
    id
    name
    amntOne
    amntTwo
    location
    entryDate

Table3:
    id
    name
    amntOne
    amntTwo
    location
    entryDate

First i want to retrive data from Table1 and Table2. 2nd, i want to accumulate each columns that has same name(For example Table1.amntOne+Table2+amntOne). 3rd, After accumulation insert all columns to the table 3. And finally, I need to create html table to fill with Table3 data.

I don't have any idea where i should start with c#

Okay, so I think I have enough to go on now. Your SQL query would look like this:

SELECT
  ids.nameId as id,
  t1.name,
  IFNULL(t1.amntOne, 0) + IFNULL(t2.amntOne,0) as amntOne,
  IFNULL(t1.amntTwo, 0) + IFNULL(t2.amntTwo,0) as amntTwo,
  t2.location
FROM
  NameIdTable as ids LEFT JOIN Table1 as t1 ON t1.Id = ids.nameId
                     LEFT JOIN Table2 as t2 ON t2.Id = ids.nameId
WHERE
  t1.Id is not null or t2.Id is not null

This will return results where there's at least one value in Table1 (t1) or Table2 (t2). It will add the amounts. Table2 (t2) is the only table capable of having a location so the value is taken from there.

The results will be your Table3. You could then use the results to generate your HTML. Let's say you put your results in a DataTable. That could look like this:

public string GetHtml(DataTable table3)
{
    var sb = new StringBuilder();
    sb.AppendLine("<table>");
    foreach(var row in table3.Rows) 
    {
        sb.AppendLine("    <tr>");
        sb.AppendLine($"        <td>{row["id"]}</td>");
        sb.AppendLine($"        <td>{row["name"]}</td>");
        sb.AppendLine($"        <td>{row["amntOne"]}</td>");
        sb.AppendLine($"        <td>{row["amntTwo"]}</td>");
        sb.AppendLine($"        <td>{row["location"]}</td>");
        sb.AppendLine("    </tr>");
    }
    sb.AppendLine("</table>");
    return sb.ToString();
}

Modify according to your need. The values for name and location may also need to be escaped for HTML with the above approach. You may also want to use an XmlDocument object to build your HTML. This answer is just to point you in the right direction.

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