Get the column names of a table and store them in a string or var c# asp.net

前端 未结 6 1108
我寻月下人不归
我寻月下人不归 2021-01-03 16:29

I was wondering how I could get the columns of a database table and store each of them in a string or string array. I have the following code but I believe it does not work.

6条回答
  •  情歌与酒
    2021-01-03 17:12

    private void leavestat()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
    
            string query = "SELECT * FROM leaverecord";
            MySqlCommand cmd = new MySqlCommand(query, conn);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
    
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                 listBox1.Items.Add(dt.Columns[i].ToString());
            }
                conn.Close();
        }
    

提交回复
热议问题