0
votes

My question is pretty simple. I am working on Kentico 9 with its SQL Server database which contains several tables which had been added directly from the SQL Management Studio by an external contractor. The fact is that those tables are being used to store custom content which will be displayed for a site, but, in the code they don't have the code for making queries. I mean, they don't have Info and Provider classes.

https://docs.kentico.com/display/K82/Retrieving+database+data+using+ObjectQuery+API

According with this, all tables into the Kentico database can be accessed by invoking methods on these classes, but I don't have it this time.

Something like this, it will not work if I use my table name:

var user = UserInfoProvider.GetUserInfo("administrator");
var items = CustomTableItemProvider.GetItems("MyTable")
.TopN(10)
.WhereEquals("ItemCreatedBy", user.UserID)
.OrderBy("ItemCreatedWhen");

My question is:

can I query any table by its name?

One last thing: I cannot declared those table as "custom table" because it seems to be a bug in the CMS.

4

4 Answers

2
votes

Or you can pull data using your own SQL query:

var ds = ConnectionHelper.ExecuteQuery("select ....", null, QueryTypeEnum.SQLQuery);

Nevertheless I would recommend to create a custom class inside a custom module (much more robust than custom tables) instead and use the generated Info and InfoProvider classes to get and manipulate data.

1
votes

I think an object has to be registered within the system (created through Kentico UI or API) in order to be pulled from DB with object query.

So I'd choose one of the following options:

  1. Use Entity Framework or something similar to work with that data
  2. Create appropriate custom tables or even custom module and push data there. Not sure why you can't create a custom table... What is an error you're getting?
  3. If you need to present data on the UI only (without processing on the back end) - use just custom queries

Hope this helps.

1
votes

If you are accessing in code then you could do it the good old fashioned way. If you want to pull data from the database to display on the website you could also do so by creating a custom query and using a transformation to display the fields, then use a repeater on the page to display the transformed data. Alternatively you can use a SQL datasource with a basic repeater, but you still have to create a transformation to display the data. Both methods allow you to access the data in the tables from within the CMS UI, no need to touch any code behind.

1
votes

If your objective is to read data from these database tables to transform on webpage e.g. using CMS Repeater webpart, you can simply create custom query(s) in Kentico itself and load data using it. You can find the detail here on how to create custom custom queries and load data using it.

On the other hand you can also write your custom classes and define the custom methods where you can pull data using your own SQL query like this: var ds = ConnectionHelper.ExecuteQuery("select ....", null, QueryTypeEnum.SQLQuery);

Lastly I don't think there should be any issue to create custom table instead of those direct DB tables, only thing we have to ensure code name of custom table should be unique means don't try to use exact same name because it'll cause exception due to same table name already exist in DB. You can please share exception you getting while creating custom table so that I can help you out further.