Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions FirstSteps/00-Index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
"| [Practice Lists ]( 11-Practice_Lists.ipynb ) |\n",
"| [Functions and Procedures ]( 12-Functions_and_Procedures.ipynb ) |\n",
"| [Objects and Classes ]( 13-Objects_and_Classes.ipynb ) |\n",
"| [Methods and Members ]( 14-Methods_and_Members.ipynb ) |\n",
"| [Methods and Exceptions ]( 15-Methods_and_Exceptions.ipynb ) |\n",
"| [Files ]( 16-Files.dib ) |\n",
"| [ByteStream ]( 17-ByteStream.dib ) |\n",
"| [TextStream ]( 18-TextStream.dib ) |\n",
"| [BankAccount and CSV File ]( 19-BankAccount_and_CSVFile.dib ) |\n",
"\n",
"\n",
" \n"
Expand Down
6 changes: 3 additions & 3 deletions FirstSteps/17-ByteStream.dib
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ The first time you run it, you won't see any change, but if you try a second tim

#!markdown

## Variable lifetime: `using` statement
## Variable lifetime: `BEGIN USING` instruction

The `using` instruction provides a convenient syntax that ensures the correct use of objects by specifying their lifetime.
The `BEGIN USING` instruction provides a convenient syntax that ensures the correct use of objects by specifying their lifetime.

> The code below performs the same function as the previous one, but specifies when to _create_ and _delete_ the flow variable.
> The code below performs the same function as the previous one, but specifies when to _create_ and _delete_ the stream variable.
> The _deletion_ of the stream causes the stream to be closed, as the `FileStream` class is written that way.

#!xsharp
Expand Down
7 changes: 7 additions & 0 deletions FirstSteps/19-BankAccount_and_CSVFile.dib
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,10 @@ FOR VAR i:= 1 TO list:Length
var am := string.Format("{0,15:N2}", list[i]:Amount)
? dt + am + " " + list[i]:Notes
NEXT

#!markdown

Great !!

You can now read and write files.
Now, have a look at the [Simple SQL Notebooks](../Simple%20SQL/01-SQLite%20Library.ipynb) to use X# to read and write data on a SQL Server.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ I hope you will enjoy your journey with **XSharp Interactive** !
| Notebooks | Description| X# Dialect | Level |
| -------- |--- | ------- |---|
| [First Steps](./FirstSteps/00-Index.ipynb) | Start learning X# language | Core | Beginner
| [Simple SQL](./WorkInProgress.ipynb) | Using SQLite in your X# Application, and change for MariaDB, PostgreSQL, ... | Core | Intermediate
| [Simple SQL](./SimpleSQL/00-Index.dib) | Using SQLite in your X# Application, and change for MariaDB, PostgreSQL, ... | Core | Intermediate
| [First Steps VFP](./WorkInProgress.ipynb) | Start X# with your VFP background | VFP | Beginner


Expand Down
22 changes: 22 additions & 0 deletions SimpleSQL/00-Index.dib
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!meta

{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"languageName":"csharp","name":"csharp"}]}}

#!markdown

# Index

| Lesson |
|-----|
| [Index ]( 00-Index.dib ) |
| [SQLite Library ]( 01-SQLite_Library.ipynb ) |
| [Connectionv vvvv ]( 02-Connection.ipynb ) |
| [Command NonQuery ]( 03-Command_NonQuery.dib ) |
| [Command Reader ]( 04-Command_Reader.dib ) |
| [Command Scalar ]( 05-Command_Scalar.dib ) |
| [DataTable ADO.NET ]( 06-DataTable_ADO.Net.dib ) |
| [PostgreSQL ]( 07-PostgreSQL.dib ) |




Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
Expand Down
170 changes: 26 additions & 144 deletions Simple SQL/02-Connection.ipynb → SimpleSQL/02-Connection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"outputs": [],
"source": [
"// <-= Press on the Arrow to run Me\n",
"#r \"nuget: Microsoft.Data.Sqlite, 8.0.5\"\n",
"#r \"nuget:Microsoft.Data.Sqlite, 8.0.5\"\n",
"#r \"nuget:XSharpInteractive\""
]
},
Expand All @@ -31,7 +31,7 @@
"source": [
"<span style=\"color:red\">\n",
"<h2>Warning !! Dirty Hack !!</h2>\n",
"As XSharp scripting engine doesn't support Nuget package load for now, we will have to force the load of the .DLL itself.<br />\n",
"As XSharp scripting engine doesn't support Nuget package load for now, we will have to force the load of the .DLL itself in the XSharp Interactive memory.<br />\n",
"When you load a package, it is placed in your profile folder in the path :<br />\n",
"</span>\n",
"\"C:\\Users\\your login name\\.nuget\\packages\\microsoft.data.sqlite.core\\8.0.5\\lib\\net8.0\\Microsoft.Data.Sqlite.dll\"\n",
Expand Down Expand Up @@ -130,12 +130,15 @@
"The `System.IO` namespace contains a lot of tools in file management, and `Delete` is one of them.\n",
"\n",
"> Try to delete the file. \n",
"> On the first run of this Notebook, you shouldn't have a DB file, but if you try it several times you might need it."
"> On the first run of this Notebook, you shouldn't have a DB file, but if you try it several times you might need it.\n",
"\n",
"*Be aware that, even if you close it, the Notebook might keep a handle on it, and deletion may not be possible \n",
"If so, the only solution is to close and reopen the current Notebook.*"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {
"dotnet_interactive": {
"language": "xsharp"
Expand All @@ -146,38 +149,28 @@
},
"outputs": [],
"source": [
"//\n",
"// Check if the File Exist\n",
"IF (File.Exists(file))\n",
" // If so, remove the .db file\n",
"\n",
" File.Delete(file)\n",
" ? i\"File {file} deleted.\"\n",
"ENDIF\n",
"//"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "xsharp"
},
"polyglot_notebook": {
"kernelName": "xsharp"
}
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###"
"## Open and Close\n",
"\n",
"In order to interact with our SQL engine, we will have to Open the connection and Close it when no more needed. "
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {
"dotnet_interactive": {
"language": "xsharp"
Expand All @@ -189,81 +182,21 @@
"outputs": [],
"source": [
"var connection := SqliteConnection{connectionString:ToString()}\n",
" ? \"Opening...\"\n",
" connection:Open()\n",
" ? connection:State\n",
"? \"Opening...\"\n",
"connection:Open()\n",
"? connection:State\n",
"connection:Close()\n",
"? connection:State"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "xsharp"
},
"polyglot_notebook": {
"kernelName": "xsharp"
}
},
"outputs": [],
"source": [
"BEGIN USING var shortConnection := SqliteConnection{connectionString:ToString()}\n",
" ? \"Opening...\"\n",
" shortConnection:Open()\n",
" ? shortConnection:State\n",
"END USING\n",
"? shortConnection"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "xsharp"
},
"polyglot_notebook": {
"kernelName": "xsharp"
}
},
"outputs": [],
"source": [
"? connection:State"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "xsharp"
},
"polyglot_notebook": {
"kernelName": "xsharp"
}
},
"outputs": [],
"cell_type": "markdown",
"metadata": {},
"source": [
"var name := \"Fabrice\" \n",
"? i\"Welcome {name}.\""
"But it's also a good practice to release the Connection object at the end : To do so, we will use the `BEGIN USING` instruction. \n",
"And what is nice here, is that at the `END USING`, not only the object will be released, but the connection will be Closed first !\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "xsharp"
},
"polyglot_notebook": {
"kernelName": "xsharp"
}
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -277,64 +210,13 @@
},
"outputs": [],
"source": [
"BEGIN USING var connection := SqliteConnection{connectionString:ToString()}\n",
"BEGIN USING var shortconnection := SqliteConnection{connectionString:ToString()}\n",
" ? \"Opening...\"\n",
" connection:Open()\n",
" ? \"Create DataBase...\"\n",
" BEGIN USING var command := connection:CreateCommand()\n",
" command:CommandText = ;\n",
" \" CREATE TABLE user (\" +;\n",
" \" id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\"+;\n",
" \" name TEXT NOT NULL\"+;\n",
" \" );\"+;\n",
" \" INSERT INTO user\"+;\n",
" \" VALUES (1, 'Diana'),\"+;\n",
" \" (2, 'Bruce'),\"+;\n",
" \" (3, 'Peter'),\"+;\n",
" \" (4, 'Natasha');\"\n",
" command:ExecuteNonQuery()\n",
" END USING\n",
" shortconnection:Open()\n",
" ? shortconnection:State\n",
"END USING\n",
"\n",
"/*\n",
"This is a Work-in-progress\n",
"I leave it here to not forget\n",
"\n",
"\n",
"#include \"Xsharpdefs.xh\"\n",
"\n",
"LOCAL myText AS STRING\n",
"TEXT TO myText\n",
"hello\n",
"ENDTEXT\n",
"? myText\n",
"*/"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "xsharp"
},
"polyglot_notebook": {
"kernelName": "xsharp"
}
},
"outputs": [],
"source": [
"BEGIN USING var connection := SqliteConnection{connectionString:ToString()}\n",
" BEGIN USING var command := connection:CreateCommand()\n",
" // Add yourself to the Table\n",
" ? \"Adding user...\"\n",
" command:CommandText = ;\n",
" \" INSERT INTO user (name)\"+;\n",
" \" VALUES (@name)\"\n",
" command:Parameters:AddWithValue(\"@name\", name)\n",
" command:ExecuteNonQuery()\n",
" END USING\n",
"END USING"
"// Try to remove the comment here : The connection var doesn't exist\n",
"//? shortconnection"
]
}
],
Expand Down
Loading