0

I am in need to Concatenating multiple rows, this need is already discussed and answered in this thread Ms Access Query: Concatenating Rows through a query

In the above thread, the specified need can be achievable only through VBA function..but I want to achieve it via C# function. Is there any possibilities to concatenate multiple rows through ms access query and that query should be executed from C#.

I want to achieve this complete functionality through MS Access query as like SQL query's For XML PATH.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Kevin M
  • 5,436
  • 4
  • 44
  • 46
  • Since you're using C# I was use Linq Group By e.g. http://stackoverflow.com/questions/614542/ – Conrad Frix Jun 14 '16 at 14:11
  • Why would Access ACE/JET Db *understand* C# or CLR? but .net has enough tools for achieving this task. [DataReader](https://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx) is one powerful option. – marlan Jun 14 '16 at 14:26
  • 1
    See also related question here: [Create a concatenated list of child table values from an Access database without using VBA](http://stackoverflow.com/q/30076586/2144390). – Gord Thompson Jun 14 '16 at 14:49

1 Answers1

0

You can query your ms db with an OleDbConnection like that :

        var con = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;");

        string stbQuery = "SELECT * FROM [Table]";
        OleDbDataAdapter adp = new OleDbDataAdapter(stbQuery, con);
        DataSet dsXLS = new DataSet();
        adp.Fill(dsXLS);

        var groups = dsXLS.Tables[0].Rows.OfType<DataRow>().GroupBy(r => r.ItemArray[0]);
        System.Data.DataTable t = new System.Data.DataTable();
        t.Columns.Add("Key");
        t.Columns.Add("Value");

        foreach (var grp in groups)
        {
            t.Rows.Add(grp.Key, grp.ToList().Select(r => r.ItemArray[1]).Aggregate((a, b) => a + "," + b));
        }

        Console.ReadLine();

Tutorial on msdn : https://msdn.microsoft.com/en-us/library/aa288452(v=vs.71).aspx

Have a great day !

Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
  • Hi @Quentin Roger, I want to achieve this complete functionality through MS Access query as like SQL query's For XML PATH. – Kevin M Jun 15 '16 at 04:42
  • Hi @kombsh, I edited my answer to show you how you can query your xml file using `OleDbConnection` – Quentin Roger Jun 15 '16 at 08:05
  • Hi @Quentin Roger, thanks for your answer.. but I need to query data from a table in MS Access database , not from Excel file . – Kevin M Jun 15 '16 at 09:28
  • My bad, sorry. To connect to MS Access db its the same way with an OleDbConnection.The connection string should be something like this ` var conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\emp.mdb;");` – Quentin Roger Jun 15 '16 at 09:31