如何从SQL数据库中选择数据,然后从C#中的反向代码中将其显示为表格?

我一直试图找出从C#中的反向代码显示所选的sql数据,但是我没有弄清楚如何在变量中显示存储的数据以及如何使用html writer(嵌入)。任何帮助将不胜感激。 (对不起,我是新手,但是我可以很快学习)

感谢您的帮助

这是我走多远

public partial class Catalog : System.Web.UI.Page
{
    string connString = "Data Source=LAPTOP-TGN70FFC\\SQLEXPRESS;Initial Catalog = TaiwanCarCatalog; Integrated Security = True";

    protected void Page_Load(object sender, EventArgs e)
    {
        {
            if(!this.IsPostBack)
            {
                SqlConnection conn = new SqlConnection(connString);
                string sqlSTD = "SELECT * FROM [TblMaker]";
                SqlDataAdapter DatAdapter = new SqlDataAdapter(sqlSTD, conn);
                DataSet DatSet = new DataSet();
                DatAdapter.Fill(DatSet);

                DropDownList1.DataSource = DatSet.Tables[0];
                DropDownList1.DataTextField = "BrandName";
                DropDownList1.DataValueField = "BrandName";
                DropDownList1.DataBind();
                genData();

            }
        }

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        genData();
    }

    public void genData()
    {

        DataTable dt = this.GetData();

        StringBuilder html = new StringBuilder();
        html.Append("<table style='width:100%; text-align:center; padding:2px; border-spacing:2px; border:0px; background-color:#EAEAEA;'>");

        html.Append("<tr>");
        foreach (DataColumn column in dt.Columns)
        {
            html.Append("<th>");
            html.Append(column.ColumnName);
            html.Append("</th>");
        }
        html.Append("</tr>");

        foreach (DataRow row in dt.Rows)
        {
            html.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                html.Append("<td>");
                html.Append(row[column.ColumnName]);
                html.Append("</td>");
            }
            html.Append("</tr>");
        }

        html.Append("</table>");

        PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });
    }

    private DataTable GetData()
    {
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand join = new SqlCommand("SELECT TblMaker.BrandName, TblCars.* FROM TblMaker JOIN TblCars ON TblMaker.BrandID = TblCars.Brand", conn);
        SqlDataAdapter joinTable = new SqlDataAdapter(join);
        DataTable InJoin = new DataTable();
        joinTable.Fill(InJoin);

        string BrandName = DropDownList1.SelectedItem.Value;
        if (!string.IsNullOrEmpty(BrandName))
        {
            var result = from row in InJoin.AsEnumerable()
                         where row.Field<string>("BrandName") == BrandName
                         select new
                         {
                             CarModel = row.Field<string>("Car Model"),
                             Brand = row.Field<string>("BrandName"),
                             PriceRange = row.Field<string>("Price Range"),

                         };
            DataTable dt = new DataTable();


            return InJoin;

        }
        else
        {
            return InJoin;
        }

    }


}

}

评论