This is a migrated thread and some comments may be shown as answers.

[Solved] Insert statement question

2 Answers 121 Views
Data Storage for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kieran C
Top achievements
Rank 1
Kieran C asked on 13 Aug 2014, 04:23 PM

Hi,

I am new enough to this and have been looking through your data storage documentation particularly in relation to the insert statements.


I see you recommend using the below syntax for an insert i.e. creating a class and then doing the below -
    context.Insert<CarOwners>(new CarOwners() { OwnerID = 1, Name="Paul Grohe", Age=33, Alive = true, DateBorn=new DateTime(1980,7,23) });


Is there anything wrong with using the below to do an insert instead of the above?  I just want to be sure that there is not something I am missing?

Thanks
        
        Dim context = New Telerik.Storage.Extensions.Context("HH_DB", Telerik.Storage.Extensions.DatabaseLocation.Local)
        context.GetScalar(Of Integer)("Insert into payments (Kid, AccountName, AddressLine1) values ('65', '" & revNo & "', '36')")
        context.SaveChanges()
        context.CloseDatabase()

2 Answers, 1 is accepted

Sort by
0
Bulent
Telerik team
answered on 14 Aug 2014, 03:50 PM
Hello Kieran,

Thank you for using Telerik DataStorage.

Yes, there is a problem with your approach for data insertion. It is related to the fact that if your table is not previously created the insert query will fail with "no such table" error. That's why you have to ensure that the table exists in your database before calling any raw Insert/Update query. Then your properly prepared query will be executed successfully. Therefore, your code snippet will fail on first execution for sure. Another thing to notice is that calling the SaveChanges() method in case of raw SQL query execution is not required, since the ORM engine and local cash is not used.
The recommended ORM approach handles table creation and objects initialization implicitly and helps you significantly with coding. 
We don't recommend usage of GetScalar() method for raw query execution but sometimes it is useful to drop and recreate a table.

Let me know if this information helps.

Regards,
Bulent
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Kieran C
Top achievements
Rank 1
answered on 15 Aug 2014, 04:16 PM
thanks Bulent...that makes it clearer.
Tags
Data Storage for XAML
Asked by
Kieran C
Top achievements
Rank 1
Answers by
Bulent
Telerik team
Kieran C
Top achievements
Rank 1
Share this question
or