Solved

c# select data with colum title mysql

Posted on 2010-11-29
1
602 Views
Last Modified: 2012-05-10
i just put this code  for create column title in first line it not work please help

 for (int i = 0; i < Reader.FieldCount; i++)
            {
                Reader.GetName(i);
                string headlist = "";
                headlist += Reader.GetValue(i).ToString() + ",";
                listBox1.Items.Add(headlist);

            }


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Web;


namespace WindowsFormsApplication1
{


   
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string MyConString = "SERVER=localhost;" +
                "DATABASE=webboard;" +
                "UID=root;" +
                "PASSWORD='';";
            MySqlConnection connection = new MySqlConnection(MyConString);
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader Reader;
            command.CommandText = "Select * from faxcomplete limit 20 ";
            connection.Open();
            Reader = command.ExecuteReader();
            
            for (int i = 0; i < Reader.FieldCount; i++)
            {
                Reader.GetName(i);
                string headlist = "";
                headlist += Reader.GetValue(i).ToString() + ",";
                listBox1.Items.Add(headlist);

            }


            while (Reader.Read())
            {
                string thisrow = "";
                for (int i = 0; i < Reader.FieldCount; i++)
                    thisrow += Reader.GetValue(i).ToString() + ",";
           listBox1.Items.Add(thisrow);
                System.IO.File.AppendAllText(@"C:\file.txt", thisrow + Environment.NewLine);
            }
            connection.Close();

        }
    }
}

Open in new window

0
Comment
Question by:teera
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34236258
This looks to be my fault. The code I gave was to show HOW to retrieve the column name--you still have to do something to display it. GetName() returns a string that is the column name at index [i]i[/i]. You need to capture that value and then display it wherever you intend it to go. For example:

    string colName = Reader.GetName(i);

Please don't feel as though you have to accept the first answer you are given for a particular question. If something is unclear, feel free to ask for a clarification or to wait for another expert to participate. :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question