2

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?

1 Answer 1

6

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

which namespace should i be importing in order to u sqldataadpter
SqlClient is the namespace you would want to import.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.