将数据库数据加载到asp.net页面

时间:2013-10-07 11:04:57

标签: asp.net

我想知道如何将数据库中的数据加载到asp.net表单中。

所以我使用代码通过查询“Select * From ... Where ... = ...

获取数据

然后我将它加载到阅读器中

While (reader.read)
{   string sProductName = Convert.ToString(reader[1]);
    /*
    Now I need to display this name and all the other data 
    (selling price, etc) on the form
    But as I do not know how many products there will be it (the form) has to change 
    as the database information does (more products get added or removed). 
    */
}

我不知道怎么做最后一部分。如何在屏幕上显示找到的数据。

谢谢!

我需要显示的数据是标题下面的产品名称,产品描述和产品销售价格,以及所有这些名称。

2 个答案:

答案 0 :(得分:0)

对于SQL数据库,

使用id =“gridview1”的gridview控件(无论你喜欢什么,但在代码中使用相同的id)

    SqlConnection sql= new SqlConnection("your data base connection");
    SqlCommand cmd = new SqlCommand("select * from your_table_name", sql);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

        gridview1.DataSource = ds;
        gridview1.DataBind();

答案 1 :(得分:0)

         SqlConnection sql= new SqlConnection("your data base connection");
        SqlCommand cmd = new SqlCommand("select * from your_table_name", sql);
        sql.open();
        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            Textbox1.Text=dr[0].ToString();
            Textbox2.Text=dr[1].ToString();
            Textbox3.Text=dr[2].ToString();
            ......................;

        }
      con.close();

注意:

      While working with datareader sql connection must be open.

我使用文本框来显示值。你可以在任何必要的地方使用.dr [0] .ToString(),. dr [1] .ToString().....包含数据库表的值< / p>