{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "连接到 SQLite 数据库\n", "============================== " ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "连接到 SQLite 数据库,并操作数据。注意:此功能在 Microsoft.DotNet.Interactive.ExtensionLab 扩展库中,要先引用 Nuget 包。" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 设置Nuget包源" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "data": { "text/html": [ "
Restore sources
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#i \"https://api.nuget.org/v3/index.json\"" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 引用 Nuget 包" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "data": { "text/html": [ "
Restore sources
Installed Packages
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "Loading extensions from `C:\\Users\\ruyu\\.nuget\\packages\\microsoft.dotnet.interactive.extensionlab\\1.0.0-beta.23165.2\\interactive-extensions\\dotnet\\Microsoft.DotNet.Interactive.ExtensionLab.dll`" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Create strongly-typed dataframes using#!linqify.\r\n", "

The #!linqify magic command replaces a Microsoft.Data.Analysis.DataFrame variable with a generated, strongly-typed data frame, allowing the use of LINQ operations over the contained data.

\r\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
ExplainCode\r\n", "

This extension generates Sequence diagrams from csharp code using Mermaid kernel.

\r\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Inspect code compilation details using the #!inspect magic command.\r\n", "

The #!inspect magic command allows you to see the C# decompilation, IL, and JIT Asm for the code in a C# cell.

\r\n", " \r\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Use the #!record magic command to keep a transcript of the code you run.\r\n", "

Once you enable transcripts using #!record, each code submission (including re-running cells) is recorded in the specified file. The JSON format used is the same format recognized by the .NET Interactive stdio and http APIs and can be used to replay an interactive session via automation.

\r\n", " \r\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Query SQLite databases.\r\n", "

This extension adds support for connecting to SQLite databases using the #!connect sqlite magic command. For more information, run a cell using the #!sql magic command.

\r\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "Loading extensions from `C:\\Users\\ruyu\\.nuget\\packages\\microsoft.data.analysis\\0.20.0\\interactive-extensions\\dotnet\\Microsoft.Data.Analysis.Interactive.dll`" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#r \"nuget: Microsoft.DotNet.Interactive.ExtensionLab,*-*\"" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 连接到 SQLite 数据库" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "引用扩展库后,使用 #!connect sqlite 命令连接到SQLite数据库!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "data": { "text/plain": [ "Kernel added: #!sql-MySQLiteDemo" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#!connect sqlite --kernel-name MySQLiteDemo \"Data Source=sqlite\\\\SQLiteDemo.db\"\n", "\n", "/*\n", " 相对目录位置\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=Database\\SQLiteDemo.db;\"\n", " \n", " 缓存共享\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=Database\\SQLiteDemo.db;Cache=Shared;\"\n", "\n", " 使用带密码\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=SQLiteDemo.db;Cache=Shared;Password=MyEncryptionKey;\"\n", " \n", " 只读模式\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=SQLiteDemo.db;Mode=ReadOnly\"\n", "\n", " 读写创建模式\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=SQLiteDemo.db;Mode=ReadWriteCreate\"\n", "\n", " 读写模式\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=SQLiteDemo.db;Mode=ReadWrite\"\n", "\n", " 私有内存模式\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=:memory:\"\n", "\n", " 共享内存模式\n", " #!connect sqlite --kernel-name MySQLiteDemo \"Data Source=Sharable;Mode=Memory;Cache=Shared\"\n", "*/" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 使用连接操作数据库" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "dotnet_interactive": { "language": "sql-MySQLiteDemo" }, "polyglot_notebook": { "kernelName": "sql-MySQLiteDemo" } }, "outputs": [ { "data": { "text/csv": [ "Id,Name,Age\r\n", "1,张三,66\r\n", "2,李四,55\r\n" ], "text/html": [ "
IdNameAge
1
张三
66
2
李四
55
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#!sql-MySQLiteDemo\n", "\n", "select * from person;" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## C#中使用" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "xxxxxxxx\r\n" ] } ], "source": [ "//直接引用包,使用类库操作就可以了,不需要使用 上下文连接" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "name": "polyglot-notebook" }, "polyglot_notebook": { "kernelInfo": { "defaultKernelName": "csharp", "items": [ { "aliases": [], "name": ".NET" }, { "aliases": [ "C#", "c#" ], "languageName": "C#", "name": "csharp" }, { "aliases": [ "F#", "f#" ], "languageName": "F#", "name": "fsharp" }, { "aliases": [], "languageName": "HTML", "name": "html" }, { "aliases": [], "languageName": "KQL", "name": "kql" }, { "aliases": [], "languageName": "Mermaid", "name": "mermaid" }, { "aliases": [ "powershell" ], "languageName": "PowerShell", "name": "pwsh" }, { "aliases": [], "languageName": "SQL", "name": "sql" }, { "aliases": [], "languageName": "T-SQL", "name": "sql-Demo" }, { "aliases": [], "languageName": "T-SQL", "name": "sql-DemoEF" }, { "aliases": [], "languageName": "SQLite", "name": "sql-MySQLiteDemo" }, { "aliases": [], "name": "value" }, { "aliases": [ "frontend" ], "name": "vscode" } ] } } }, "nbformat": 4, "nbformat_minor": 2 }