我试图从MySQL数据库获取数据,并将数据打印到一个html小部件,但它不工作[已关闭]

b5buobof  于 2023-01-04  发布在  Mysql
关注(0)|答案(1)|浏览(117)

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>
nbnkbykc

nbnkbykc1#

尝试使用MySql. Data
https://www.nuget.org/packages/MySql.Data/
将其添加到后,您可以按如下方式使用它

using MySql.Data.MySqlClient;

然后可以重写GetDataFromDatabase()方法

private DataTable GetDataFromDatabase() {
            DataTable dataTable = new DataTable();
            String connectionString = "Your Connection String";
            String sql = "SELECT * FROM Table_1";
            connection = new MySqlConnection(connectionString);
            connection.Open();
            MySqlCommand command = new MySqlCommand(sql, connection);

            MySqlDataAdapter adapter = new MySqlDataAdapter(command);
            adapter.Fill(dataTable);

            connection.Close();
            return dataTable;
    }
    • SqlDataAdapter**表示用于填充DataSet的一组数据命令和一个数据库连接。

你可以在这里找到更多信息:
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldataadapter?view=dotnet-plat-ext-7.0
最后,确保根据服务器名称和设置正确地设置connectionString。
示例:

connetionString = @"Data Source = YourServerName;Initial Catalog = YourDatabaseName;Integrated Security = True;";

如果您的DB服务器没有使用集成安全性,它应该有用户ID和密码。

connetionString = @"Data Source = YourServerName;Initial Catalog = YourDatabaseName;User ID = userID;Password=password";

相关问题