0

我想将我的 MySQL 查询输出到一个充满表的 li 标记,我的 select 语句正在工作,我只是使用来自 MySQL 世界城市的示例数据库,我的问题是访问单个行和列的最简单方法是什么?我应该创建一个新的类或结构来将数据推送到其中吗?我希望能够将数据表转换为类似于 HashTables 的列表?我应该使用 JSON.net 之类的东西吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using Newtonsoft.Json;

namespace MySQL
{
    public partial class _default : System.Web.UI.Page
    {
        string connStr = "server=localhost;User Id=root;Persist Security Info=True;database=world; Password=root";
        MySqlDataAdapter countries;
        DataSet ds;

        protected void Page_Load(object sender, EventArgs e)
        {

            MySqlConnection conn = new MySqlConnection(connStr);
            string sql = "SELECT * FROM country";
            countries = new MySqlDataAdapter(sql, conn);
            MySqlCommandBuilder cb = new MySqlCommandBuilder(countries);

            ds = new DataSet();
            countries.Fill(ds, "Country");

        }
    }
}

我非常精通 jquery、javascript、angularjs,我不关心 html 输出,但我想知道访问各个列的最简单方法是什么?和行来输出它们?

4

1 回答 1

1

我认为您想按行和列访问所有表。

做这个

 MySqlConnection conn = new MySqlConnection(connStr);
      string sql = "SELECT * FROM country";
      countries = new MySqlDataAdapter(sql, conn);
      MySqlCommandBuilder cb = new MySqlCommandBuilder(countries);

      ds = new DataSet();
      countries.Fill(ds, "Country");
      DataTable dt;
      for (int i = 0; i <ds.Tables.Count; i++)//traverse by each table in dataset
      {
           dt = ds.Tables[i];

           for (int j = 0; j < dt.Rows.Count; j++)//traverse by row
           {
                Response.Write("Current Row--> "+j.ToString()+Environment.NewLine);
                object[] ob = dt.Rows[j].ItemArray; // get array of column from present row
                for (int k = 0; k <= ob.GetUpperBound(0); k++)
                {
                     Response.Write("Current column Number--> "+k.ToString()+" Value = "+ob[k].ToString()+Environment.NewLine);
                }
           }

      }
于 2013-10-31T06:13:57.663 回答