Results 1 to 2 of 2

Thread: Add mysql database records to combobx

  1. #1

    Add mysql database records to combobx

    Hi everyone.


    I am new to c# 2005 and I would like to know how can I add mysql table records to combobox.

    Thanks

  2. #2
    1,627
    posts
    hugeExplosions = true;
    This ones easy enough. You just need to add a SQL data reader to read the rows from your query

    Make sure you have imported the System.Data.SqlClient namespace

    Code:
    // Create a new SQL connection - you can store this in the app.config in the connectionStrings node under appsettings and use ConfigurationManager to retrieve it if needs be. Just remember to add the reference to System.Configuration
    SqlConnection conn = new SqlConnection("server=localhost;uid=username;password=password");
    // Create a new SQL command to select the data using the new connection
                SqlCommand com = new SqlCommand("SELECT * FROM myTable", conn);
    // Create a SQL data reader var which will hold the result of the query
                SqlDataReader dr;
    // Open the connection
                conn.Open();
    // Execute the command into the datareader object
                dr = com.ExecuteReader();
    // Use a while loop to loop through any data rows
                while(dr.Read) {
                    // Get the ordinal for the column called 'ColumnName' and pass this to the GetString method (assuming you know what datatypes to expect from the column/columns) - add this value to the combobox
                    comboBox1.Items.Add(dr.GetString(dr.GetOrdinal("ColumnName")));
                }
    // Close the connection
                conn.Close();
    I may be wrong with the combobox syntax as I just plucked this code out of my head, but apart from that it should work.
    MS Paint FTW!


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012