Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a winform app connected to access database as data source ,

but in access i was using this below SQL to generate a query table called trend:

SQL
TRANSFORM Count(Complaint_Number) AS [Total Numbers]
SELECT Nature_of_problem, Count(Complaint_Number) AS [Total Numbers of Issues]
FROM master_table
GROUP BY Nature_of_problem
PIVOT Format(Complaint_Received_On,"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");


What I have tried:

But i do not know how execute this & populate the same in datagrid box on form load event(dataset:CSCdataset) , please help me.
Posted
Updated 11-Aug-20 23:43pm
Comments
Sandeep Mewara 12-Aug-20 5:20am    
You want to execute the above SQL queries, right?

A stored procedure define with above and you call this SP from your C# code. ?
Member 14898617 12-Aug-20 5:23am    
Thank you for your response , the problem is i am using access database
(csc.accdb in my desktop)as datasource not MYSQL which can be stored procedures , so could you please help me in executing directly by coding ? and populate the same in data grid box n form load event.

1 solution

Quote:
Thank you for your response , the problem is i am using access database
(csc.accdb in my desktop)as datasource not MYSQL which can be stored procedures , so could you please help me in executing directly by coding ? and populate the same in data grid box n form load event.

Access (from the 2010 version at least) has stored procedures: How do I make a stored procedure in MS Access? - Stack Overflow[^] shows you how to create them.
 
Share this answer
 
Comments
Member 14898617 12-Aug-20 7:09am    
Hello inline with your suggestion , i have made the below code but agin i am struck in formatting this sql to include double quotes , could you please help me :
private readonly OleDbConnectionStringBuilder _builder = new OleDbConnectionStringBuilder
        {
            Provider = "Microsoft.ACE.OLEDB.12.0",
            DataSource = "S:\\Customer_Service\\Wires\\Database for CSC\\Backend data of databse do not open\\CSC.accdb"
        };

        public DataTable Read()
        {
            var dt = new DataTable();

            using (var cn = new OleDbConnection { ConnectionString = _builder.ConnectionString })
            {

                using (var cmd = new OleDbCommand { Connection = cn })
                {
                    cmd.CommandText = @"TRANSFORM Count(Complaint_Number) AS [Total Numbers]
                                       SELECT Nature_of_problem, Count(Complaint_Number) AS[Total Numbers of Issues]
                                       FROM master_table
                                       GROUP BY Nature_of_problem
                                       PIVOT Format(Complaint_Received_On, "mmm") In("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); 
                    cn.Open();
                    dt.Load(cmd.ExecuteReader());
                }
            }

            return dt;

        }
    }
OriginalGriff 12-Aug-20 7:23am    
To include double quotes in a escaped string literal, just double them:
string s = @"This is a double quote: "" this is a single quote: '";
Member 14898617 12-Aug-20 7:45am    
Thank you so much it is working now
OriginalGriff 12-Aug-20 7:50am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900