Execute query statement in Kentico 8

Mr Dam asked on March 4, 2015 05:20

I don't know how to execute query statement in kentico. I used ExecuteQuery of ConnectionHelper to run stored procedure and success, but it can not run with query statement. Please help me soon. Thanks!

Recent Answers

Charles Matvchuk answered on March 4, 2015 05:28

Where are you trying to execute the query ? What are you exactly trying to do ?

0 votesVote for this answer Mark as a Correct answer

Mr Dam answered on March 4, 2015 13:04 (last edited on March 4, 2015 13:04)

Thanks Charles. example: I want to run "select F1, F2, F3 from Table where ID = @id" so how do I execute this query by ConnectionHelper?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 4, 2015 14:17

Also, have you looked into the following articles? Reason I ask is because there are a lot of other ways to access data through the API without having to create custom SQL database objects. I personally take advantage of page type queries. I'll create a query in there even if it is only SELECT * FROM External_Table1 WHERE ##WHERE## ORDER BY ##ORDERBY## or EXEC SP_Name param1. This way it leaves the code open for me to edit in the UI and access easily in code without having to change the C# code if the query changes

http://devnet.kentico.com/articles/kentico-8-technology-dataquery https://docs.kentico.com/display/K8/Working+with+database+queries+in+the+API

This code should get you going in the right direction:

QueryDataParameters parameters = new QueryDataParameters();
parameters.AddGuid("@ItemGuid", Guid);
GeneralConnection cn = ConnectionHelper.GetConnection();
QueryParameters qp = new QueryParameters("DELETE " + customTable.ClassTableName + " WHERE ItemGUID =    @ItemGuid", parameters, QueryTypeEnum.SQLQuery, false);
0 votesVote for this answer Mark as a Correct answer

Richard Sustek answered on March 10, 2015 11:25

Hi all,

To Brenden's response I would add that it is recommended to use a ConnectionHelper class to run custom queries. In some cases its possible to have SQL deadlocks. To answer the initial question - to run a simple SQL query use this code:

DataSet ds = ConnectionHelper.ExecuteQuery("select * from CMS_User", null, QueryTypeEnum.SQLQuery);

Kind regards,

Richard Sustek

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.