Skip to content

Instantly share code, notes, and snippets.

@nisar1
Last active August 29, 2015 13:57
Show Gist options
  • Save nisar1/9651598 to your computer and use it in GitHub Desktop.
Save nisar1/9651598 to your computer and use it in GitHub Desktop.
filling a datatable or dataset the quick way
private DataTable GetDataTable()
{
string sql = "SELECT Id, Description FROM MyTable";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
using (SqlCommand myCommand = new SqlCommand(sql, myConnection))
{
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
DataTable myTable = new DataTable();
myTable.Load(myReader);
myConnection.Close();
return myTable;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment