Read SQL Table into C# DataTable

Here, give this a shot (this is just a pseudocode) using System; using System.Data; using System.Data.SqlClient; public class PullDataTest { // your data table private DataTable dataTable = new DataTable(); public PullDataTest() { } // your method to pull data from database to datatable public void PullData() { string connString = @”your connection string here”; … Read more

How to export DataTable to Excel

I would recommend ClosedXML – You can turn a DataTable into an Excel worksheet with some very readable code: XLWorkbook wb = new XLWorkbook(); DataTable dt = GetDataTableOrWhatever(); wb.Worksheets.Add(dt,”WorksheetName”); The developer is responsive and helpful. The project is actively developed, and the documentation is superb.

Simple way to copy or clone a DataRow?

You can use ImportRow method to copy Row from DataTable to DataTable with the same schema: var row = SourceTable.Rows[RowNum]; DestinationTable.ImportRow(row); Update: With your new Edit, I believe: var desRow = dataTable.NewRow(); var sourceRow = dataTable.Rows[rowNum]; desRow.ItemArray = sourceRow.ItemArray.Clone() as object[]; will work

How can I turn a DataTable to a CSV?

The following shorter version opens fine in Excel, maybe your issue was the trailing comma .net = 3.5 StringBuilder sb = new StringBuilder(); string[] columnNames = dt.Columns.Cast<DataColumn>(). Select(column => column.ColumnName). ToArray(); sb.AppendLine(string.Join(“,”, columnNames)); foreach (DataRow row in dt.Rows) { string[] fields = row.ItemArray.Select(field => field.ToString()). ToArray(); sb.AppendLine(string.Join(“,”, fields)); } File.WriteAllText(“test.csv”, sb.ToString()); .net >= 4.0 And … Read more

Get all column names of a DataTable into string array using (LINQ/Predicate)

Try this (LINQ method syntax): string[] columnNames = dt.Columns.Cast<DataColumn>() .Select(x => x.ColumnName) .ToArray(); or in LINQ Query syntax: string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>() select dc.ColumnName).ToArray(); Cast is required, because Columns is of type DataColumnCollection which is a IEnumerable, not IEnumerable<DataColumn>. The other parts should be obvious.

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