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.

3

1 Answer 1

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 !

Sign up to request clarification or add additional context in comments.

4 Comments

Hi @Quentin Roger, I want to achieve this complete functionality through MS Access query as like SQL query's For XML PATH.
Hi @kombsh, I edited my answer to show you how you can query your xml file using OleDbConnection
Hi @Quentin Roger, thanks for your answer.. but I need to query data from a table in MS Access database , not from Excel file .
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;");`

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.