You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PolyglotNotebooksStudy/Docs/多语言笔记.2.1.连接数据库.ipynb

412 lines
7.7 KiB
Plaintext

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"连接数据库\n",
"============================== "
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"连接到SQL Server,并操作数据。"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 引用操作类库"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#r \"nuget:Microsoft.Dotnet.Interactive.SqlServer,*-*\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 连接到SQL Server"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 数据库连接命令"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 命令概述: 使用帮助参数"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"数据库连接命令为 `#!connect` 查看连接帮助 `#!connect -h`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#!connect -h"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 连接到本机SQL Server"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"1. 建立连接"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"> 连接到本机SQL Server, 特别注意的是 --kernel-name 指定名称后面的SQL操作均需要使用sql-{{kernel-name}}的值\n",
"> 比如 --kernel-name Demo, 则该SQL连接的kernel名称为 sql-Demo, 使用 #!sql-Demo 进行引用"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#!connect mssql \"server=.\\SQL2008;uid=sa;pwd=gly-bicijinlian;database=Study;Encrypt=True;TrustServerCertificate=True;\" --kernel-name Demo\n",
"//重复执行会抛出错误,目前没有找到解决方法"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"2. 使用连接: 方式一"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"> 在右下角,切换 Kernel, 由 `csharp - C# Script` 换成 由--kernel-name指定值生成的 `sql-Demo - T-SQL`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "sql-Demo"
},
"polyglot_notebook": {
"kernelName": "sql-Demo"
}
},
"outputs": [],
"source": [
"SELECT * from Student"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"2. 使用连接:方式二(推荐)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"> 使用 `#!sql-Demo` ,后面直接写SQL语句不用切换 kernel "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#!sql-Demo\n",
"SELECT * from Student"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 使用 EF Core"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 新连接: 添加 --create-dbcontext 参数"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#!connect mssql \"server=.\\SQL2008;uid=sa;pwd=gly-bicijinlian;database=Study;Encrypt=True;TrustServerCertificate=True;\" --create-dbcontext --kernel-name DemoEF "
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"上面的连接操作,会执行下面的任务:\n",
"+ 搭建EFCore基架,并初始化 DBContext 的实例: DemoEF\n",
"+ 安装包相关Nuget包,详情见输出\n",
"+ 添加新的子内核 #!sql-DemoEF"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 使用EFCore连接: 操作数据"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 类SQL脚本方式"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#!sql-DemoEF\n",
"SELECT * from Student"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### C#程序中使用EF Core"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"//连接建立后执行环境中就有了相关的类DBContext等\n",
"DemoEFContext context = new DemoEFContext();\n",
"var t = context.Students.ToList<Student>();\n",
"display(t);\n",
"\n",
"FormattableString fs = $\"select * from Student;\";\n",
"var c = context.Database.ExecuteSql(fs);\n",
"display(c);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "sql-DemoEF"
},
"polyglot_notebook": {
"kernelName": "sql-DemoEF"
}
},
"outputs": [],
"source": [
"#!sql-DemoEF\n",
"\n",
"var data = from "
]
}
],
"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": [],
"name": "value"
},
{
"aliases": [
"frontend"
],
"name": "vscode"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}