6 Answers, 1 is accepted
I suppose you want to know how to persist your objects with Telerik OpenAccess forward mapping capabilities. You mention “runtime mapping” and what you probably mean is persisting new objects of an already defined type when you run the application. Here I provide a brief overview of the basic steps you need to follow. The first step which you have already done is to define a class you want to persist. After enabling your project to use Telerik OpenAccess you have two options at your disposal to proceed. You can either mark the class with an attribute [Telerik.OpenAccess.Persistent] or use the Forward Mapping Wizard to select the classes you want to make persistent. In order to interact with the database you need a context to bridge the gap between your application logic and the database storage. Such a context is the ObjectScopeProvider which you can specify to be generated when using the Forward Mapping Wizard. When you have the IObjectScope context ready, you can start transactions and implement your custom logic for handling your objects. Beware that you need to handle your objects through the context, i.e. the objects that need to be changed should be either retrieved with the scope or added to it. Since there is a step by step description in our documentation regarding this and other basic topics, here I provide a link:
http://www.telerik.com/support/documentation-and-tutorials/step-by-step-tutorial-for-openaccess.aspx
The Getting Started section is a good start.
Hope this is the information you need. If you want to know something else or elaborate on the question you have posed, contact us again and we will be available to help.
Sincerely yours,
Petko_I
the Telerik team
Try |
Dim db As DatabaseDatabase = Database.Get("Connection") |
' Testing if database exists |
If db.GetSchemaHandler().DatabaseExists() = False Then |
db.GetSchemaHandler().CreateDatabase() ' conditionally creating it |
End If |
Dim ddl As String = db.GetSchemaHandler().CreateUpdateDDLScript(Nothing) |
If String.IsNullOrEmpty(ddl) = False Then |
db.GetSchemaHandler().ExecuteDDLScript(ddl) ' conditionally correcting it |
End If |
MsgBox("Object Successfully Created") |
Catch ex As Exception |
MessageBox.Show(ex.Message) |
End Try |
Public Class StoredProcedure |
Public Shared Function UpdateSupplier(ByVal scope As IObjectScope,ByVal m_Counter As String,ByVal m_FirstName As String,ByVal m_MiddleName As String,ByVal m_LastName As String) As IQueryResult |
Dim query As IQuery = scope.GetSqlQuery("[UpdateSupplier] ?,?,?,?", Nothing, "LONGVARCHAR m_Counter,LONGVARCHAR m_FirstName,LONGVARCHAR m_MiddleName,LONGVARCHAR m_LastName") |
Dim res As IQueryResult = query.Execute(New Object() {m_Counter,m_FirstName,m_MiddleName,m_LastName}) |
Dim count As Integer = res.Count 'executes the query |
Return res |
End Function |
Public Shared Function ReadSupplier(ByVal scope As IObjectScope,ByVal counter As Nullable(Of Long)) As IQueryResult |
Dim query As IQuery = scope.GetSqlQuery("[ReadSupplier] ?", Nothing, "BIGINT Counter") |
Dim res As IQueryResult = query.Execute(New Object() {counter}) |
Dim count As Integer = res.Count 'executes the query |
Return res |
End Function |
Public Shared Function DeleteSupplier(ByVal scope As IObjectScope,ByVal m_Counter As Nullable(Of Integer)) As IQueryResult |
Dim query As IQuery = scope.GetSqlQuery("[DeleteSupplier] ?", Nothing, "INTEGER m_Counter") |
Dim res As IQueryResult = query.Execute(New Object() {m_Counter}) |
Dim count As Integer = res.Count 'executes the query |
Return res |
End Function |
Public Shared Function CreateSupplier(ByVal scope As IObjectScope,ByVal m_FirstName As String,ByVal m_MiddleName As String,ByVal m_LastName As String) As IQueryResult |
Dim query As IQuery = scope.GetSqlQuery("[CreateSupplier] ?,?,?", Nothing, "LONGVARCHAR m_FirstName,LONGVARCHAR m_MiddleName,LONGVARCHAR m_LastName") |
Dim res As IQueryResult = query.Execute(New Object() {m_FirstName,m_MiddleName,m_LastName}) |
Dim count As Integer = res.Count 'executes the query |
Return res |
End Function |
End Class |
From what I understand you want OpenAccess to create the CUD stored procedures at runtime, similar to the way you are creating the tables at runtime. Am I right?
If you add the following entry to the app.config - <useStoredProceduresForDelete>True</useStoredProceduresForDelete> (similar entries for Insert and Update will be required)
and use the code you provided below, OpenAccess will calculate the required CUD stored procedures, create them (if there are not already present) and will use them for CUD operations.
Hope I have answered your question. Do get back in case you need further clarification
Best wishes,
Ady
the Telerik team
USE [ISSIDEMOUS] |
GO |
/****** Object: StoredProcedure [dbo].[CheckExistingData] Script Date: 06/28/2010 10:02:29 ******/ |
SET ANSI_NULLS ON |
GO |
SET QUOTED_IDENTIFIER ON |
GO |
ALTER PROCEDURE [dbo].[CheckExistingData] |
@TableName NVARCHAR(128) = NULL, |
@ColumnName NVARCHAR(128) = NULL, |
@Value NVARCHAR(30) = NULL, |
@RecordExists BIT OUTPUT |
AS |
SET NOCOUNT ON |
SELECT ChildTableName, ChildColumn,0 AS 'ObjectID' INTO ##TempTable FROM DataDictionaryRelationship WHERE ParentTableName = @TableName ORDER BY ChildTableName |
UPDATE ##TempTable SET ##TempTable.ObjectID = S.ID FROM SYSOBJECTS S WHERE (OBJECTPROPERTY(S.ID, 'IsUserTable') = 1) AND (S.NAME <> 'dtProperties') AND (S.NAME = ##TempTable.ChildTableName) |
SET ROWCOUNT 1 |
DECLARE |
@TempTableName NVARCHAR(128), |
@Query NVARCHAR(500), |
@ObjectID INT, |
@ColName NVARCHAR(128) |
WHILE EXISTS (SELECT 1 FROM ##TempTable) |
BEGIN |
SELECT @TempTableName = ChildTableName, |
@ObjectIDObjectID = ObjectID, |
@ColName = ChildColumn, |
@Query = N'SELECT 1 AS IsRecordExist FROM [' + ChildTableName + '] WHERE ' + ChildColumn + ' = ''' + @Value + '''' |
FROM ##TempTable |
PRINT @TempTableName |
-- Check column existence on the table |
SELECT 1 AS IsColumnExisting |
FROM SYSCOLUMNS C |
WHERE C.ID = @ObjectID |
AND C.Name = @ColName |
IF @@ROWCOUNT > 0 |
BEGIN |
-- Check data existence on the table |
EXEC SP_EXECUTESQL @Query |
SET @RecordExists = @@ROWCOUNT |
IF @RecordExists > 0 |
BEGIN |
PRINT 'Table : ' + @TempTableName |
BREAK |
END |
END |
-- Delete the temp table record one by one |
DELETE |
FROM ##TempTable |
WHERE ChildTableName = @TempTableName |
END |
SET ROWCOUNT 0 |
DROP TABLE ##TempTable |
When you reverse map a stored procedure , the Reversemapping wizard creates a static method(that uses ObjectScope.GetSqlQuery) which can be used to call this stored procedure. OpenAccess does not know/analyze the actual stored procedure content.
If you want to generate your stored procedure when you deploy your application you could use the 'ISchemaHandler.ExecuteDDLScript' method and specify the stored procedure SQL. You can find an example of how to use this method, here.
Hope this answers your query. Do get back in case you need further assistance.
Kind regards,
Ady
the Telerik team