- 已关闭**。此问题需要details or clarity。当前不接受答案。
- 想要改进此问题?**添加详细信息并通过editing this post阐明问题。
3小时前关门了。
Improve this question
这是微件photo of the widget
我想从数据库添加数据到小部件,而不是手动。我正在使用visual studio 2022为这个项目。
我已经准备好了html和数据库,但从数据库连接是困难的一部分。
如果有人能帮上忙,我将不胜感激。
下面是aspx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace widgetdis
{
public partial class WebForm1 : System.Web.UI.Page
{
string connectionString = ConfigurationManager.ConnectionStrings["WidgetdbConnectionString"].ConnectionString;
private string GenerateWidgetHtml()
{
DataTable dataTable = GetDataFromDatabase();
string html = "";
foreach (DataRow row in dataTable.Rows)
{
string PTS = row["PTS"].ToString();
string REB = row["REB"].ToString();
string AST = row["AST"].ToString();
string FG = row["FG%"].ToString();
html += "<div class='widget'>";
html += "<div class='widget__header'>2022-23 season statss</div>";
html += "<div class='widget__content'>";
html += "<div class='widget__row'>";
html += "<div class='widget__label'>PTS:</div>";
html += "<div class='widget__value'>" + PTS + "</div>";
html += "</div>";
html += "<div class='widget__row'>";
html += "<div class='widget__label'>REB:</div>";
html += "<div class='widget__value'>" + REB + "</div>";
html += "</div>";
html += "<div class='widget__row'>";
html += "<div class='widget__label'>AST:</div>";
html += "<div class='widget__value'>" + AST + "</div>";
html += "</div>";
html += "<div class='widget__row'>";
html += "<div class='widget__label'>FG%:</div>";
html += "<div class='widget__value'>" + FG + "</div>";
html += "</div>";
html += "</div>";
html += "</div>";
html += "</div>";
}
return html;
}
private DataTable GetDataFromDatabase()
{
string connectionString = "widgetdbConnectionString";
string selectQuery = "SELECT * FROM Table_1";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(selectQuery, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
DataTable dataTable = new DataTable();
dataTable.Load(reader);
return dataTable;
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
}
}
}
下面是一个aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="widgetd.aspx.cs" Inherits="widgetdis.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="widstyle.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<div id="widget" class="widget">
<div class="widget__header">2022-23 season stats</div>
<div class="widget__content">
<div class="widget__row">
<div class="widget__label">PTS</div>
<div class="widget__value">30.0</div>
</div>
<div class="widget__row">
<div class="widget__label">REB</div>
<div class="widget__value">6.6</div>
<div class="widget__position">Tied-11th</div>
</div>
<div class="widget__row">
<div class="widget__label">AST</div>
<div class="widget__value">6.8</div>
<div class="widget__position">1st</div>
</div>
<div class="widget__row">
<div class="widget__label">FG%</div>
<div class="widget__value">50.0</div>
<div class="widget__position">42nd</div>
</div>
</div>
</div>
</form>
</body>
</html>
1条答案
按热度按时间nbnkbykc1#
尝试使用MySql. Data库
https://www.nuget.org/packages/MySql.Data/
将其添加到后,您可以按如下方式使用它
然后可以重写GetDataFromDatabase()方法
你可以在这里找到更多信息:
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldataadapter?view=dotnet-plat-ext-7.0
最后,确保根据服务器名称和设置正确地设置connectionString。
示例:
如果您的DB服务器没有使用集成安全性,它应该有用户ID和密码。