Subscribe to this thread
Home - General / All posts - create a data source via script
artlembo


3,400 post(s)
#16-May-18 19:42

Just wondering how one might go about creating a data source from VBScript, instead of using SQL. I want to create a dialogue box that will allow a user to navigate to a particular file (i.e. like the node_pressure below), an then load the data source. From there, the script will keep running, doing more interesting things.

The SQL code looks like this, but I don't want the user to have to work with the SQL, just my dialogue interface:

CREATE DATASOURCE [nodepressure] (

  PROPERTY 'Source' '{ "Source": "C:\\\\files\\\\node_pressure_1424v2.csv", "SourceForceText": true }',

  PROPERTY 'Type' 'csv'

);

tjhb
10,094 post(s)
#16-May-18 21:47

API example in Application.CreateDatabaseForFile.

artlembo


3,400 post(s)
#16-May-18 22:44

Thanks, Tim. I had tried that, it ran, but nothing happened. I wonder if there is another object that needs to get called to actually perform the import.

Going to take another shot tonight, and will let you know if I make more progress.

tjhb
10,094 post(s)
#17-May-18 00:03

Groping around a bit more has yielded better fruit...

It's explained under Database.GetChild(), which also explains how to use Database.Insert() for the case of a child data source.

See the second example, which explains everything for adding a .map data source, but I think .csv should be very similar. E.g. the component Type for Database.Insert() will still be "datasource".

It would be nice to have a list of component type names somewhere, e.g. under Database.Insert(). I don't think there is one. In a way this is a consequence of the .NET model being so clean and flat, and perhaps functional rather than primarily OO. There is no Component class, no Drawing subclass; component types are just strings. An object model with minimal objects.)

tjhb
10,094 post(s)
#17-May-18 06:27

With an extra ), sorry about that. Now need two extra ((.

By the way, although I’m not enough of a a programmer to really appreciate it, I love the move to a flat API.

(There is probably a proper academic word to describe this structure better than “flat”, but if so I don’t know it.)

Much less “what-what-what-what”, in favour of a simple “how”. Just the facts Ma’am. Cool.

adamw


10,447 post(s)
#17-May-18 09:42

We will add the list of component types. We also need lists of properties / subproperty names in composite properties like connection strings.

adamw


10,447 post(s)
#17-May-18 09:41

Shortening the example mentioned by Tim above and adopting it to your case:

'VBScript

Sub Main

  Set app = Manifold.Application

  Set db = app.GetDatabaseRoot()

 

  ' create data source

  Set connProperties = app.CreatePropertySet()

  connProperties.SetProperty "Source""c:\files\node_pressure_1424v2.csv"

  connProperties.SetProperty "SourceForceText"True

  conn = connProperties.ToJson()

  inserted = db.Insert("nodepressure""datasource")

  db.SetProperty inserted, "Type""csv"

  db.SetProperty inserted, "Source", conn

 

  app.Log "Created data source"

  app.OpenLog

End Sub

You don't have to compose the connection string via a PropertySet object (connProperties above), you can put it verbatim into the call to Database.SetProperty if you want.

Hope this helps.

artlembo


3,400 post(s)
#17-May-18 15:28

holy cow. That was easy. We need to bookmark this, or have it as an easily accessible fragment in the Help manual.

Manifold User Community Use Agreement Copyright (C) 2007-2021 Manifold Software Limited. All rights reserved.