{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "data": { "text/html": [ "
Installed Packages
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Installed Packages
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "//全局初始化\n", "#!import \"./Base.ipynb\"\n", "\n", "#r \"nuget:Microsoft.Identity.Client,4.66.2\"\n", "using Microsoft.DotNet.Interactive;\n", "using Microsoft.DotNet.Interactive.Commands;" ] }, { "cell_type": "code", "execution_count": null, "id": "a118a9b7", "metadata": { "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "data": { "text/html": [ "
Installed Packages
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "Loading extension script from `C:\\Users\\ruyu\\.nuget\\packages\\microsoft.dotnet.interactive.sqlserver\\1.0.0-beta.25177.1\\interactive-extensions\\dotnet\\extension.dib`" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Query Microsoft SQL Server databases.\r\n", "

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

\r\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "Scaffolding a `DbContext` and initializing an instance of it called `SqlServerKernelWithEF` in the C# kernel." ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Installed Packages
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "Kernel added: #!sql-SqlServerKernelWithEF" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "//引入 SqlServer 的 NuGet 包\n", "#r \"nuget:Microsoft.DotNet.Interactive.SqlServer,*-*\"\n", "//#r \"nuget:Microsoft.Identity.Client,4.66.2\"\n", "\n", "//优化用法\n", "{\n", " //内核名:魔法命令中的内核名,执行后会自动加 sql- 前缀,做为内核名被使用\n", " string magicCommandKernelName = \"SqlServerKernelWithEF\";\n", " string completeKernelName = \"sql-\" + magicCommandKernelName;\n", "\n", " //引入内核:可重复执行\n", " if(Microsoft.DotNet.Interactive.Kernel.Root.FindKernelByName(completeKernelName) == null)\n", " {\n", " var connectKernelCode = $\"#!connect mssql --kernel-name {magicCommandKernelName} \\\"Server=.\\\\SQL2019;Database=study;User Id=sa;Password=gly-bicijinlian;TrustServerCertificate=true;\\\" --create-dbcontext\";\n", " await Kernel.Root.SendAsync(new SubmitCode( connectKernelCode, \"csharp\"));\n", " }\n", " else\n", " {\n", " Console.WriteLine($\"名为 {completeKernelName} 的内核已存在。需要新内核时,请为--kernel-name参数使用不同的值, 本次执行不做任何更改!\");\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [ { "ename": "Error", "evalue": "(6,1): error CS0246: 未能找到类型或命名空间名“SqlServerKernelWithEFContext”(是否缺少 using 指令或程序集引用?)\r\n(7,19): error CS0246: 未能找到类型或命名空间名“SqlServerKernelWithEF”(是否缺少 using 指令或程序集引用?)\r\n(6,38): error CS0246: 未能找到类型或命名空间名“SqlServerKernelWithEFContext”(是否缺少 using 指令或程序集引用?)", "output_type": "error", "traceback": [ "(6,1): error CS0246: 未能找到类型或命名空间名“SqlServerKernelWithEFContext”(是否缺少 using 指令或程序集引用?)\r\n", "(7,19): error CS0246: 未能找到类型或命名空间名“SqlServerKernelWithEF”(是否缺少 using 指令或程序集引用?)\r\n", "(6,38): error CS0246: 未能找到类型或命名空间名“SqlServerKernelWithEFContext”(是否缺少 using 指令或程序集引用?)" ] } ], "source": [ "//连接建立后,执行环境中就有了相关的类:DBContext等\n", "using Microsoft.EntityFrameworkCore;\n", "using Microsoft.EntityFrameworkCore.SqlServer;\n", "using Microsoft.EntityFrameworkCore.Scaffolding;\n", "\n", "SqlServerKernelWithEFContext a = new SqlServerKernelWithEFContext();\n", "var context = new SqlServerKernelWithEF();\n", "var t = context.Students.ToList();\n", "display(t);\n", "\n", "FormattableString fs = $\"select * from Student;\";\n", "var c = context.Database.ExecuteSql(fs);\n", "display(c);" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "polyglot_notebook": { "kernelInfo": { "defaultKernelName": "csharp", "items": [ { "aliases": [], "languageName": "csharp", "name": "csharp" } ] } } }, "nbformat": 4, "nbformat_minor": 5 }