从mysql数据库获取表以填充datalist

时间:2014-02-25 16:12:45

标签: c# mysql asp.net database datalist

到目前为止,我确实从数据库中获取了值。我的问题是无法将datialist中的数据填充为表格。我获取简单值的代码如下:

try
   {
    using (OdbcConnection connection = new OdbcConnection(ConfigurationManager.ConnectionStrings["MySQLConnStr"].ConnectionString))
       {
           connection.Open();
           using (OdbcCommand command = new OdbcCommand("SELECT * FROM datas", connection))
            using (OdbcDataReader dr = command.ExecuteReader())
             {
                while (dr.Read())
                 {
                     switch (dr["namen"].ToString())
                      {
                          case "gt": Label2.Text = dr["dest"].ToString(); break;
                          case "gp1": Image1.ImageUrl = dr["dest"].ToString(); break;
                          case "gp2": Image2.ImageUrl = dr["dest"].ToString(); break;
                          case "gp3": Image3.ImageUrl = dr["dest"].ToString(); break;
                       }
                 }
                    dr.Close();
              }
                connection.Close();
          }
        }
        catch (Exception ex)
        {
            Response.Write("An error occured: " + ex.Message);
        }

1 个答案:

答案 0 :(得分:1)

使用DataAdapter,将其填入数据表

http://www.codeproject.com/Questions/358631/how-to-bind-data-from-sql-server-to-datalist-using

List <string> datalist1 = new List<string>();

datalist1.DataSource = _dtTable1;

datalist1.DataBind();