Alex Gordon
Alex Gordon

Reputation: 60811

c# display table from sql server on winform

I am connecting to sql server using c#. How do I display the results of the below query on a winform? I would like to display this data set in a control. I believe it should be a datachart, but it does not matter to me.

// Initialize a connection string    
string myConnectionString = "Provider=SQLOLEDB;Data Source=hermes;" +
   "Initial Catalog=qcvaluestest;Integrated Security=SSPI;";

// Define the database query    
string mySelectQuery = "select top 500 name, finalconc " + 
   "from qvalues where rowid between 0 and 25000";

What is the best way to display the results of this query on a winform?

Upvotes: 2

Views: 7086

Answers (1)

BFree
BFree

Reputation: 103750

Drop a DataGridView on your form, and use this code to populate it

using(var connection = new SqlConnection(myConnectionString))
using(var adapter = new SqlDataAdapter(mySelectQuery, connection))
{
   var table = new DataTable();
   adapter.Fill(table);
   this.dataGridView.DataSource = table;
}

Upvotes: 6

Related Questions