【发布时间】:2014-10-12 19:50:42
【问题描述】:
如果这个问题看起来很奇怪或很明显,请提前抱歉,但我是 c# 的新手。
我正在 Visual Studio 中使用 C# 制作一个 Web 应用程序,并且我有一个 html table。我想用来自sql server 的表格填充它。
这是我的 HTML 表格:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="MRAApplication.test" %>
<!DOCTYPE html>
<body>
<form runat="server">
<asp:Button ID="populateButton" runat="server" Text="Table" onClick="populateButton_Click" />
</form>
<table id="table1">
<thead>
<tr>
<th>AlertID</th>
<th>AccountID</th>
<th>Alert</th>
<th>Active</th>
<th>IsShow</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
当我单击 button 时,我希望表格中填充我在此处创建的数据表:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
namespace MRAApplication
{
public partial class test : System.Web.UI.Page
{
protected void populateButton_Click(object sender, EventArgs e)
{
GetDataTable();
}
public static System.Data.DataTable GetDataTable()
{
string queryString = "Select * FROM Alerts";
string conn = ConfigurationManager.ConnectionStrings["UATConnectionString"].ToString();
SqlDataAdapter adapter = new SqlDataAdapter(queryString, conn);
System.Data.DataTable dt = new System.Data.DataTable();
dt.TableName = "Table";
using (System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(queryString, conn))
{
da.Fill(dt);
}
return dt;
}
}
}
UATConnectionString 正在工作(我几乎是肯定的),所以现在我只需要将要连接的 SQLtable 中的数据获取到 HTMLtable 中。
如果有人可以帮助我,我将不胜感激。如果需要更多信息,我很乐意提供帮助。
【问题讨论】:
-
@NicholasV。谢谢,我之前遇到过,但是用我们的数据表调用 ConvertDataTableToHTML() 函数的最佳方法是什么?我试着把“ConvertDataTableToHTML(dt);”在 sqlDataAdapter 之后和“return dt;”之前当我点击按钮时它仍然没有做任何事情。
标签: c# html sql datatable connection-string