{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"连接数据库\n",
"==============================\n",
"连接到SQL Server,并操作数据。 "
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"+ 引用操作类库"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"#r \"nuget:Microsoft.Dotnet.Interactive.SqlServer,*-*\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"+ 连接到SQL Server\n",
" > 数据库连接命令为 `#!connect` 查看连接帮助 `#!connect -h`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"#!connect -h\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"> 连接到本机SQL Server, 特别注意的是 --kernel-name 指定名称,后面的SQL操作均需要使用:sql-{{kernel-name}}的值\n",
"> 比如 --kernel-name Demo, 则该SQL连接的kernel名称为 sql-Demo, 使用 #!sql-Demo 进行引用"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Kernel added: #!sql-Demo"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#!connect mssql \"server=.\\SQL2008;uid=sa;pwd=gly-bicijinlian;database=Demo;Encrypt=True;TrustServerCertificate=True;\" --kernel-name Demo\n",
"\n",
"//重复执行会抛出错误,目前没有找到解决方法\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"+ 使用连接\n",
" 一种方法是:在右下角,切换 Kernel, 由 `csharp - C# Script` 换成 由--kernel-name指定值生成的 `sql-Demo - T-SQL`"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"dotnet_interactive": {
"language": "sql-Demo"
},
"polyglot_notebook": {
"kernelName": "sql-Demo"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(4 行受到影响)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/csv": [
"Id,Name,Age\r\n",
"1,张三,20\r\n",
"2,李四,30\r\n",
"3,三丰,300\r\n",
"4,小龙子,50\r\n"
],
"text/html": [
"
"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"SELECT * from Test"
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"> 推荐的是下面的方法:即使用 `#!sql-Demo` ,后面直接写SQL语句,不用切换 kernel "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(4 行受到影响)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/csv": [
"Id,Name,Age\r\n",
"1,张三,20\r\n",
"2,李四,30\r\n",
"3,三丰,300\r\n",
"4,小龙子,50\r\n"
],
"text/html": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#!sql-Demo\n",
"SELECT * from Test"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"## 使用 EF Core"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"+ 新连接,添加 --create-dbcontext 参数"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"data": {
"text/markdown": [
"Scaffolding a `DbContext` and initializing an instance of it called `DemoEF` in the C# kernel."
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Installed Packages- Humanizer, 2.14.1
- humanizer.core, 2.14.1
- Microsoft.EntityFrameworkCore.Design, 7.0.0
- microsoft.entityframeworkcore.sqlserver, 7.0.0
- microsoft.identity.client, 4.48.1
"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Security Warning: The negotiated TLS 1.0 is an insecure protocol and is supported for backward compatibility only. The recommended protocol version is TLS 1.2 and later.\r\n"
]
},
{
"data": {
"text/plain": [
"Kernel added: #!sql-DemoEF"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#!connect mssql \"server=.\\SQL2008;uid=sa;pwd=gly-bicijinlian;database=Demo;Encrypt=True;TrustServerCertificate=True;\" --create-dbcontext --kernel-name DemoEF "
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"上面的连接操作,会执行下面的任务:\n",
"搭建EFCore基架,并初始化 DBContext 的实例: DemoEF\n",
"安装包相关Nuget包,详情见输出\n",
"添加新的子内核 #!sql-DemoEF"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"+ 使用EFCore连接,操作数据"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(4 行受到影响)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/csv": [
"Id,Name,Age\r\n",
"1,张三,20\r\n",
"2,李四,30\r\n",
"3,三丰,300\r\n",
"4,小龙子,50\r\n"
],
"text/html": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#!sql-DemoEF\n",
"SELECT * from Test"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"+ 程序中使用EF Core连接"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [
{
"data": {
"text/html": [
"index | value |
---|
0 | Submission#4+Test
|
1 | Submission#4+Test
|
2 | Submission#4+Test
|
3 | Submission#4+Test
|
"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"//连接建立后,执行环境中就有了相关的类:DBContext等\n",
"DemoEFContext context = new DemoEFContext();\n",
"var t = context.Tests.ToList();\n",
"display(t);\n",
"\n",
"FormattableString fs = $\"select * from Test;\";\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": [],
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}