html已创建table,想在空白处填写从mysql读取到的数据,如何实现?
我直接给出代码,望采纳!点击左下方的解决即可采纳!
要在HTML中显示来自MySQL数据库的数据,你需要使用C#代码连接到数据库并读取数据,然后在页面上使用C#代码将数据写入HTML表格。
下面是一个示例,假设你已经在HTML中创建了一个表格:
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</table>
在C#代码中,你可以使用MySQL连接器(如MySql.Data)来连接到数据库并读取数据。例如:
using MySql.Data.MySqlClient;
// 连接到数据库
string connStr = "server=localhost;user=root;database=testdb;port=3306;password=123456";
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
// 执行SQL查询
string sql = "SELECT * FROM users";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader reader = cmd.ExecuteReader();
// 读取数据并写入HTML表格
while (reader.Read())
{
int id = reader.GetInt32("id");
string name = reader.GetString("name");
int age = reader.GetInt32("age");
Response.Write("<tr><td>" + id + "</td><td>" + name + "</td><td>" + age + "</td></tr>");
}
// 关闭连接
reader.Close();
conn.Close();
这样就可以在HTML表格中显示来自MySQL数据库的数据了。
mysql读取到的数据从后台传过来,在html页面循环显示即可。