C#如何从mysql数据库表中获取行头 [英] C# how to get the row header from mysql database table

查看:157
本文介绍了C#如何从mysql数据库表中获取行头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好日子先生和女士。我打算获取所有行标题,如column_id和first_name,last_name等,并使用mysql数据库表中的列填充我的列表框。请赐教我怎么做。我不知道该使用什么 MySqlDataAdapter MySqlDataReader 。我是初学者,也愿意学习。



我尝试过:



Good day sir and ma'am. I'm planning to get all row header like "column_id and first_name, last_name and so on" and populate my listbox with columns from mysql database table. Please enlighten me how to do this. I am not sure what to use MySqlDataAdapter or MySqlDataReader. I'm beginner and willing to learn.

What I have tried:

using(MySqlConnection con = new MySqlConnection(connstring))
{
   //what should I write here?
}

//I named my listbox as columnlistbox and my table's name in mysqlworkbench is table1

推荐答案

如果您使用datareader,则使用GetSchemaTable返回包含所有列元数据的表并枚举.Columns,或者您可以使用GetName和GetType直接从阅读器获取单个详细信息,从而提供列索引每一栏。



您还可以使用DataAdapter获取表格,并在枚举.Columns的表格中提供相同的信息。



很多信息,特别是www.devart.com以及MySQL文档,Google是你的朋友。
If you use a datareader then either use the GetSchemaTable to return a Table with all the column metadata and enumerate the .Columns or you can get individual details direct from the reader using GetName and GetType supplying the column index on each column.

You can also use the DataAdapter to get a Table and the same information is available in the table enumerating the .Columns.

Lots of information particularly on www.devart.com as well as the MySQL documentation, Google is your friend.


使用<$ c $的控制台应用示例c> MySqlDataReader :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using MySql.Data.Common;
using MySql.Data.Types;
using MySql.Data.MySqlClient;

namespace MySQLDatabaseTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string connstring = @"SERVER=localhost;DATABASE=<database_name>;UID=<user_id>;PASSWORD=<password>";
			
            using (MySqlConnection con = new MySqlConnection(connstring))
			{
				MySqlCommand myQuery = con.CreateCommand();
				myQuery.CommandText = @"select * from mytable";
				MySqlDataReader myReader;
				con.Open();
				myReader = myQuery.ExecuteReader();
				DataTable dataTable = new DataTable();
				dataTable.Load(myReader);
				
				foreach (DataRow row in dataTable.Rows)
				{
					Console.WriteLine(row["MyFieldName"].ToString());
				}
			}
            Console.ReadKey();
        }
    }
}


这篇关于C#如何从mysql数据库表中获取行头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆