Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在多个标签中打印选定的数据库列?_C#_Mysql - Fatal编程技术网

C# 如何在多个标签中打印选定的数据库列?

C# 如何在多个标签中打印选定的数据库列?,c#,mysql,C#,Mysql,请帮助我以不同的标签显示数据库中选定的列数据 protected void Page_Load(object sender, EventArgs e) { con.Open(); MySqlCommand countcmd = new MySqlCommand("select count(*) from category", con); int temp = Convert.ToInt32(countcmd.ExecuteScalar().T

请帮助我以不同的标签显示数据库中选定的列数据

protected void Page_Load(object sender, EventArgs e)
{
    con.Open();       
        MySqlCommand countcmd = new MySqlCommand("select count(*) from category", con);
        int temp = Convert.ToInt32(countcmd.ExecuteScalar().ToString());
        MySqlCommand inscmd = new MySqlCommand("select catname from category where cid > 0", con);
        string temp1 = inscmd.ExecuteScalar().ToString();
        MySqlDataReader dr = inscmd.ExecuteReader();
        dr.Read();
        for (int i = 0; i < temp; i++){
            ("label" & i).text = dr[i].ToString();
        }       
}
受保护的无效页面加载(对象发送方,事件参数e)
{
con.Open();
MySqlCommand countcmd=新的MySqlCommand(“从类别中选择计数(*),con);
int temp=Convert.ToInt32(countcmd.ExecuteScalar().ToString());
MySqlCommand inscmd=new MySqlCommand(“从cid>0的类别中选择catname”,con);
字符串temp1=inscmd.ExecuteScalar().ToString();
MySqlDataReader dr=inscmd.ExecuteReader();
里德博士();
对于(int i=0;i
使用page FindControl方法

一开始就完全错了,您不能使用(string).Text访问控件

下一个问题是如何创建这些标签,如果它们是在设计时预先创建的,您如何知道您有足够的标签,您将如何处理额外的标签

PS您应该为每个查询建立一个新的连接,并让连接池处理缓存,而不是传递实例化的一轮,除非在极少数情况下

您还应该处理查询对象,使用这一点很好,而不是在内存不足时依赖GC进行整理

此外,查询返回的行只有一列,因此需要一个while循环

我要做的第一件事是从查询中返回一个
IEnumerable

e、 差不多

private IEnumerable<String> GetCategoryNames(String argConnectionString)
{
   using(SqlConnection con = new SqlConnection(argConnnectionString))
   {
       con.Open()
       using(SqlCommand com = new SqlCommand("Select CatName From Category Where cid > 0", con))
       {
           using(SqlDataReader reader = com.ExecuteReader())
           {
              while (reader.Read())
              {
                  yield reader[0].ToString();
              }
           }
       }
   }
}
private void AddNewLabels(argConnectionString)
{
  int count = 0;
  Point startPoint = new Point(0,0) // assuming this is where you want the first label to be in the scroll box
  labelSpacing = 20; // how far apart vertically should your column of labels be.
  foreach(String labelText in GetCatgoryNames(argConectionString))
  {
      Label label = new Label();
      label.parent = myScrollBox;
      label.Left = StartPoint.X;
      label.Top = Count * LabelSpacing + StartPoint.Y;
      label.Name = String.Concat'MyDynamicLabel'
      // etc
      label.Text = labelText;
      count++;
  }
}

protected void Page_Load(object sender, EventArgs e)
{
   DestroyPreviousLabels();
   AddNewLabels(conn.ConnectionString);
}
DestroyNewLabels将使用
FindControl
,来查找名称以“MyDynamicLabel”开头的控件。 或者你可以变得更聪明一点,只摧毁你不需要的东西,如果你想创造更多。如果你想这样做,考虑一个<代码>列表>代码,然后添加和删除它,那么你将有一个循环的计数,并且你不必继续寻找它们,因为这不是BRILE性能明智的。 总之,有些想法,还有一些重构的机会,一旦你开始工作了


注意,这是我脑子里想不出来的,所以可能有一两个愚蠢的想法。

这确实是问题的一部分,但它本身没有多大用处