1

I have a SharpScript .ss script file with some small code that polls a database and formats things to display. The output is getting too unruly for command line output so I wanted to generate html and view things like that. Generating the html works fine with htmlDump in the .ss file, but when I create a small web project from one of their templates, the database connection stops working?

Is there any difference in how to specify the connection string for a website vs. a .ss script file?

I just have the regular args specification at the beginning of the file

<!--
db mssql
db.connection Server=ble\bla;Database=blu;user id=blo;password=#blingblong#
-->

This works fine in .ss script file, I can then do something like

```code
{{ 
    "select * from View drv
        join [Project] p on drv.ProjectId = p.ProjectId
        where DocumentId = 'GUID' " 
    | dbSelect
    | map => {it.RecordId, it.Text, it.Name}
    | to => sqlMap 
}}

sqlMap | count
sqlMap | textDump
```

and get output like the count (21) and a table from the textDump.

I then created a "bare-webapp" from a template with web new bare-webapp Name and added a new html file with the same content, but that doesn't work? If I do

{{dbTableNames | count }}

{{db}}

I can see that the db argument is rendered in the browser as "mssql" like in the argument input, but the table names are not listed, and no sql queries work. I don't see any error messages or anything, so I have no idea what is going on? I thought SharpScript would be able to render the html page similarly to how .ss script files can access the database?

gakera
  • 3,589
  • 4
  • 30
  • 36
  • If I try to access the "connection" property of the "db" argument, I get an error, but I just think that's because I don't know the correct syntax. If I do `{{db.connection}}` I get error about `'String' does not have a 'connection' property or field` - I get the same error when trying that in the .ss script file. – gakera Jan 29 '20 at 16:05
  • I was running this against a remote SQL server, but also tried running against a local SQL server, and same thing, works in .ss file but not when served to html – gakera Jan 29 '20 at 16:27

1 Answers1

0

They are very different contexts. The stand-alone .ss Sharp Scripts are executed within the context of a Sharp App that's executed by the dotnet tools which have access to all ServiceStack implementation assemblies. So when the script sees:

<!--
db mssql
db.connection Server=ble\bla;Database=blu;user id=blo;password=#blingblong#
-->

It creates a ScriptContext pre-configured with #Script Database Scripts and an OrmLiteConnectionFactory configured with the ServiceStack.OrmLite.SqlServer Provider and your connection string.

By contrast #Script Pages executing in a web page only has access to the SharpPagesFeature context that's configured in your App. So you'll need to configure OrmLite in your AppHost as normal, e.g:

container.AddSingleton<IDbConnectionFactory>(() => 
    new OrmLiteConnectionFactory(connectionString, SqlServer2012Dialect.Provider));

Then make the Database Scripts available to your SharpPagesFeature, e.g:

Plugins.Add(new SharpPagesFeature {
    ScriptMethods = {
        new DbScriptsAsync()
    }
});

Which will give your pages access to the Database Scripts which will use your registered OrmLiteConnectionFactory.

Sharp Apps

Sharp Apps are a way to rapidly develop Web Apps created entirely using #Script which enables an instant feedback live development model as your App doesn't require re-compilation or restarts. Sharp Apps, just like your Sharp Scripts are both run

$ x new bare-webapp ProjectName

Then you can start your Sharp App with x run in your App's directory, e.g:

$ cd ProjectName
$ x run

The same functionality in Sharp Scripts is also available to Sharp Apps, but instead of adding your App configuration to the top of your script you'd instead add it to your app.settings, e.g:

db mssql
db.connection Server=ble\bla;Database=blu;user id=blo;password=#blingblong#

Please note from v5.7 #Script is transitioning to use the JS Pipeline Operator Syntax, so it's now recommended to write:

```code
{{ 
    "select * from View drv
        join [Project] p on drv.ProjectId = p.ProjectId
        where DocumentId = 'GUID' " 
    |> dbSelect
    |> map => {it.RecordId, it.Text, it.Name}
    |> to => sqlMap 
}}

sqlMap |> count
sqlMap |> textDump
```
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Ok, thanks for the great info. But when I create `web new bare-webapp wata` I don't see any way to add C# code, all the files are just html files? Is it not possible to configure in the bare-webapp? What template should I be using? I really like the way to run the bare-webapp with just `web` in the base folder, so I'd like to be able to do that still, if possible. – gakera Jan 30 '20 at 09:52
  • @gakera ‘webapp’ are Sharp App Templates, create a new ‘script’ project to create a new website configured with #Script Pages – mythz Jan 30 '20 at 13:19
  • ok thanks. Is it correct understanding that Sharp Apps can't be configured to talk to a database? – gakera Jan 31 '20 at 10:25
  • @gakera What made you think that? Your Sharp Script is a Sharp App which connects to a DB and there are a number of Sharp App examples that use DB Scripts https://sharpscript.net/docs/sharp-apps – mythz Jan 31 '20 at 10:55
  • So it's just the `bare-webapp` that can't be configured to connect to a DB? Ok that makes more sense, I guess it's very "bare" haha – gakera Jan 31 '20 at 11:07
  • It’s a Sharp App so it can be configured like any Sharp App where app configuration is maintained in app.settings, please read the Sharp App docs I linked to there’s ample docs and examples that use DB Scripts – mythz Jan 31 '20 at 11:18
  • 1
    ok, great then that is what I want to do, to configure the bare Sharp App to talk to the database using app.settings. It seems I can just add the `db mssql db.connection Server=ble\bla;Database=blu;user id=blo;password=#blingblong#` stuff to app.settings and everything works! – gakera Jan 31 '20 at 12:52
  • @gakera Right because the page variables at the top of your stand-alone Sharp Script is an alternative available to Sharp Scripts instead of specifying them in your `app.settings`, I've updated my question to mention Sharp Apps, – mythz Jan 31 '20 at 22:24