在ASP.NET表中显示SQL Server数据库中的数据

时间:2017-07-17 14:10:27

标签: c# asp.net sql-server

从短时间内搜索互联网,我能够为使用Visual Studio ASP.NET / C#开发的内部应用程序编写此代码。

我正在调用数据库并创建连接,然后根据使用的SQL代码构建下拉列表选择。

作为C#的新手,我很难将其他字段带入我的表格(例如文字字符串和数字,而不只是有一个下拉字段。

理想情况下,我想显示如下数据:

Activity | Hazards | Controls | RiskLevel(Dropdown)|

这是我的前端代码:

<asp:Table ID="Table1" runat="server" Font-Size="Medium" Width="960" BorderWidth="3" CellPadding="5" CellSpacing="5" CssClass="table table-bordered table-striped table-hover table-condensed table-responsive">
    <asp:TableHeaderRow runat="server" Font-Bold="true">
        <asp:TableHeaderCell ID="ActivityID" Width="20%">Activity</asp:TableHeaderCell>
        <asp:TableHeaderCell ID="HazardsID">Hazards</asp:TableHeaderCell>
        <asp:TableHeaderCell ID="RiskID" Width="15%">Risk Level</asp:TableHeaderCell>
    </asp:TableHeaderRow>

    <asp:TableRow ID="TableRow1" runat="server" >
        <asp:TableCell><asp:Label ID="lblActivity" runat="server" Text="Label"></asp:Label></asp:TableCell>
        <asp:TableCell><asp:Label ID="lblHazards" runat="server" Text="Label"></asp:Label></asp:TableCell>
        <asp:TableCell><asp:DropDownList ID="ddl1" runat="server"></asp:DropDownList></asp:TableCell>
    </asp:TableRow>

    <asp:TableFooterRow  runat="server" >
        <asp:TableCell ColumnSpan="3" HorizontalAlign="Right" Font-Italic="true">
            INSERT **TEXTBOX FOR OTHER INFORMATION ATTACHED HERE!**
        </asp:TableCell>
    </asp:TableFooterRow>
</asp:Table>

这是我目前的后端代码:

if (!this.IsPostBack)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd =
            new SqlCommand("SELECT TOP 1000 [MainActivityID],[ID],[Activity],[Hazards],CASE WHEN [Risk_Level] LIKE 'H' THEN 'HIGH' WHEN [Risk_Level] LIKE 'M' THEN 'MEDIUM' WHEN [Risk_Level] LIKE 'L' THEN 'LOW' WHEN[Risk_Level] LIKE 'T' THEN 'TRIVIAL' WHEN[Risk_Level] LIKE 'N' THEN 'N/A' END AS[Risk_Level],[Controls] FROM [STRIDES_SQL].[dbo].[STRIDES_RA_Activities]"))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            con.Open();
            ddl1.DataSource = cmd.ExecuteReader();
            ddl1.DataTextField = "Risk_Level";
            ddl1.DataValueField = "MainActivityID";
            ddl1.DataBind();
            String input = lblActivity.Text;
            con.Close();

        }
    }
    ddl1.Items.Insert(0, new ListItem("--Select Risk Level--", "0"));
}

0 个答案:

没有答案