简体   繁体   English

如何使用C#显示asp.net中数据库中的数据

[英]how to show data from database in asp.net using c#

I am trying to show data from Mysql database base on value of textbox entered by user as in example shown below 我试图根据用户输入的文本框的值显示来自Mysql数据库的数据,如下例所示

what I need now since I am begginer in asp.net what is the fastest aproche to use to display that data ? 现在我需要什么,因为我在asp.net中是乞gg,什么是最快的显示数据的方式?

which is betteer using div or table tags for the layout ? 哪个使用div或table标签作为布局?

is their any bettwer code ? 他们的投注者代码是什么吗? like using label to display output is it good idea? 喜欢使用标签显示输出,这是个好主意吗? or their is something better ? 还是他们更好?

图片

code markup 代码标记

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AccountInquiry.aspx.cs" Inherits="BankingDemo.AccountInquiry" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Banking Site (Demo)</title>
</head>
<body>
    <form ID="from" runat="server">
        <div>
            <asp:Label ID="lbl_ID" runat="server">Enter ID</asp:Label>
        </div>

        <asp:TextBox ID="ID" runat="server"></asp:TextBox>
        <asp:Button ID="QurAcc" runat="server" Text="Query" OnClick="QurAcc_Click" />
        <asp:Label ID="lbl_CityName" runat="server"></asp:Label>
    </form>
</body>
</html>

c# code C#代码

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.Configuration;
using MySql;
using MySql.Data;
using MySql.Data.Types;
using MySql.Data.MySqlClient;

namespace BankingDemo
{
    public partial class AccountInquiry : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void get_info()
        {
            string connstr = ConfigurationManager.ConnectionStrings["ConnConfig"].ToString();
            string cmdtxt = @"select Name,CountryCode,District,Population from world.city where id = @ID_param";
            string temp = null;

            try
            {
                using (MySqlConnection conn = new MySqlConnection(connstr))
                using (MySqlCommand cmd = new MySqlCommand(cmdtxt))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@ID_param", ID.Text);
                    cmd.Connection = conn;
                    conn.Open();

                    MySqlDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        temp += "<br />";
                        temp += reader["Name"].ToString();
                        temp += reader["CountryCode"].ToString();
                        temp += reader["District"].ToString();
                        temp += reader["Population"].ToString();
                        temp += "<br />";
                    }
                    conn.Close();
                }

                lbl_CityName.Text = temp;
            }
            catch (Exception ex)
            {
                //ex.Message;
            }
        }

        protected void QurAcc_Click(object sender, EventArgs e)
        {
            get_info();
        }
    }
}

The correct way to do that is to create four labels, like that: 正确的方法是创建四个标签,如下所示:

<br/>
<asp:Label ID="lblCityName" runat="server"></asp:Label>
<br/>
<asp:Label ID="lblCountry" runat="server"></asp:Label>
<br/>
<asp:Label ID="lblDistrict" runat="server"></asp:Label>
<br/>
<asp:Label ID="lblPopulation" runat="server"></asp:Label>

C#: C#:

while (reader.Read())
{
    lblCityName.Text = "Name: " + reader["Name"].ToString();
    lblCountry.Text = "Country Code: " + reader["CountryCode"].ToString();
    lblDistrict.Text = "District " + reader["District"].ToString();
    lblPopulation.Text = "Population: " + reader["Population"].ToString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM