简体   繁体   中英

formatting the long sql string which contains double quotes

I have a winform app which is connected to access database as data-source, as i can't use stored procedures unlike in Mysql, i am in desperate try of executing the query and populate the same in data-grid view box.(earlier i wan using the same excel in my access database query) But again i am struck how to enter this long sql with correct formatting, please help me how to encapsulate this sql string in my code.

private void FormMainMenu_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'cSCDataSet.master_table' table. You can move, or remove it, as needed.
            this.master_tableTableAdapter.Fill(this.cSCDataSet.master_table);
            dataGridView1.DataSource = Read();
        }

        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;

        }
    }
}

You double double quote. ""

Working Solution

Apparently your problem has nothing to do with Winforms.

just i don't know to how to format it to include double quotes

In SQL string quotes are with single quotes, like this example from SQL IN operator

SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');

So if you need this command text in a c# string:

const string sqlCommandText = @"
    SELECT * FROM Customers
    WHERE Country IN ('Germany', 'France', 'UK')";

cmd.CommandText = sqlCommandText;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM