Should I Dispose() DataSet and DataTable?

Here are a couple of discussions explaining why Dispose is not necessary for a DataSet. To Dispose or Not to Dispose ?: The Dispose method in DataSet exists ONLY because of side effect of inheritance– in other words, it doesn’t actually do anything useful in the finalization. Should Dispose be called on DataTable and DataSet … Read more

How to create a DataTable in C# and how to add rows?

Here’s the code: DataTable dt = new DataTable(); dt.Clear(); dt.Columns.Add(“Name”); dt.Columns.Add(“Marks”); DataRow _ravi = dt.NewRow(); _ravi[“Name”] = “ravi”; _ravi[“Marks”] = “500”; dt.Rows.Add(_ravi); To see the structure, or rather I’d rephrase it as schema, you can export it to an XML file by doing the following. To export only the schema/structure, do: dt.WriteXMLSchema(“dtSchemaOrStructure.xml”); Additionally, you can … Read more

Convert generic List/Enumerable to DataTable?

Here’s a nice 2013 update using FastMember from NuGet: IEnumerable<SomeType> data = … DataTable table = new DataTable(); using(var reader = ObjectReader.Create(data)) { table.Load(reader); } This uses FastMember’s meta-programming API for maximum performance. If you want to restrict it to particular members (or enforce the order), then you can do that too: IEnumerable<SomeType> data = … Read more

LINQ query on a DataTable

You can’t query against the DataTable‘s Rows collection, since DataRowCollection doesn’t implement IEnumerable<T>. You need to use the AsEnumerable() extension for DataTable. Like so: var results = from myRow in myDataTable.AsEnumerable() where myRow.Field<int>(“RowNo”) == 1 select myRow; And as @Keith says, you’ll need to add a reference to System.Data.DataSetExtensions AsEnumerable() returns IEnumerable<DataRow>. If you need … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)