sqlserverconnect
Installation
Review the source and dependencies with a prompt. A package is available to download.
Install with an AI assistant
Copy the prompt so your assistant can review the pinned revision and dependencies first.
View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "sqlserverconnect" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-ad70833517af62448b2b7dec666f5ff082b82e2adfc7188fe97ef4ef631a9a9d. Verify the upstream source and version against https://skillhub.cn/skills/user_91acba48/sql-server-connection before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.
Source and setup
skillhub.cn
Setup guidance pending
See the original source for supported tools, dependencies, and permissions.
Installation test pending
Standardized process to connect to SQL Server (including MES/ERP and other business databases) for read‑only queries, table schema inspection, and CSV export. Includes pyodbc connection string templates, SQL Server 2008 R2 self‑signed certificate compatibility (TrustServerCertificate), read‑only account setup (db_datareader role, no write permissions), and phased troubleshooting for common connection errors (e.g. 18456 login failed, 4060 cannot open database, cascaded errors from invalid connection attributes). Trigger this skill when users mention keywords like “connect SQL Server”, “connect MES”, “read DB table”, “SQL query”, “pyodbc connect”, “read‑only DB account”, or “export SQL table”.
Introduction source: third_party · user_91acba48 · 1.0.0 Reference
SkillPi interpretation
- Tasks
- Translation pending; see source documentation.
- Audience
- Translation pending; see source documentation.
- Inputs and outputs
- Not specified in the source documentation.
- Software, accounts and paid services
- Translation pending; see source documentation.
- Limits and differences
- Translation pending; see source documentation.
- Testing
- Not tested
Browse files
Frontmatter
name: sqlserver-connect
description: 连接 SQL Server(含 MES / ERP 等业务库)进行只读查询、表结构探查与 CSV 导出的标准化流程。覆盖 pyodbc 连接串模板、SQL 2008 R2 自签名证书兼容(TrustServerCertificate)、只读账号配置(db_datareader,无写权限)、以及常见连接错误的分阶段排查(18456 登录失败 / 4060 打不开库 / 无效连接属性连锁报错)。当用户提到"连 SQL Server""连接 MES""读数据库表""SQL 查询""pyodbc 连库""数据库只读账号""导出 SQL 表"时,应使用本 skill。
agent_created: trueSQL Server 连接 skill
连接 SQL Server(MES、ERP 或其他业务库)并执行只读查询、导出数据。重点保证最小权限 + 环境兼容。
适用场景
- 连接 MES / ERP 等 SQL Server 业务库做数据查询、表结构探查、CSV 导出
- 需要创建只读账号(无 UPDATE / DELETE / INSERT)
- 排查 pyodbc 连接报错(18456、4060、TLS 协议不兼容、端口超时、无效连接属性)
前置条件(连接前自检)
- ODBC 驱动:系统需安装
ODBC Driver 17 for SQL Server(旧版SQL Server/SQL Server Native Client 11.0也可用)。- 检查:PowerShell 读
HKLM:\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers
- 检查:PowerShell 读
- Python 包:
pyodbc(已验证 5.3.0 可用)。pymssql非必需。 - 网络:本机需能 TCP 到达服务器 IP:端口(默认 1433)。
标准连接串(pyodbc)
import pyodbc
conn = pyodbc.connect(
"DRIVER={ODBC Driver 17 for SQL Server};"
f"SERVER={server},{port};" # 例如 "SERVER=10.0.0.5,1433"
f"DATABASE={database};"
f"UID={user};PWD={password};"
"TrustServerCertificate=yes;" # ← SQL 2008 R2 必须,否则证书握手失败
"Login Timeout=10;", # ← 注意是 Login Timeout,不是 Timeout(后者不是有效 ODBC 属性)
autocommit=True
)
SQL 2008 R2 关键兼容项:必须加 TrustServerCertificate=yes。若服务器强制加密且客户端 TLS 版本过低,关闭加密(Encrypt=no)或升级驱动。
只读账号配置(SSMS)
- 登录名 → 服务器角色:仅
public - 切换到「用户映射」→ 勾选目标库 → 数据库角色成员身份:
- ✅
db_datareader(允许 SELECT) - ✅
public - ❌ 不要勾
db_datawriter(含 INSERT/UPDATE/DELETE) - ❌ 不要勾
db_owner
- ✅
- 更细粒度:
GRANT SELECT ON dbo.表 TO 用户名;
已知坑与排查(按出现频率)
坑1:连接串里的无效属性会触发连锁报错
- 现象:
Invalid connection string attribute (0)出现在错误里,同时附带18456/4060等看似不相关的错误,极具迷惑性。 - 根因:连接串中写了
Timeout=10(不是有效 ODBC 属性)→ 驱动解析失败,连带导致后续登录/打开库逻辑异常。 - 解决:超时属性必须写成
Login Timeout=10(或Connection Timeout)。pyodbc/ODBC Driver 17 不认裸Timeout。 - 注意:无效属性是「真凶」时,18456/4060 只是连带产物,修掉属性后往往直接连上。
坑2:18456 与 4060 的区别(分阶段定位)
- 18456 = 登录被拒(账号不存在 / 密码错 / 实例不对)。先连
master(不指定 DATABASE)单独验证登录。 - 4060 = 登录成功但打不开目标库(账号未映射到该库 / 库名拼错 / 连错实例)。此时阶段1连 master 会成功,阶段2开目标库才报 4060。
- 模板已内置两阶段:先连 master 验证登录,再连目标库;4060 时自动列出当前账号可访问的库,方便排查映射。
坑3:登录名拼写一致性
- 真实案例:登录名拼写差一个字母(如把
readuser误写成readusr)→ 服务器无此登录 → 18456。 - 务必与 SSMS/VSCode 中实际登录名逐字核对。建议 SSMS 执行
SELECT name FROM sys.server_principals WHERE name LIKE '%关键字%'看真实拼写。
坑4:端口号与版本号别混淆
- SSMS/VSCode 标题栏里的
SQL Server 10.50.1600是产品版本号(SQL Server 2008 R2 SP1 的 build 号),不是端口。 - 默认实例端口是 1433;命名实例才监听非标准端口。端口自查:SQL Server 配置管理器 → TCP/IP → IP 地址 → TCP 端口。
坑5:ODBC Driver 17 不支持 Encrypt=Mandatory
- 用
Encrypt=yes|no而非Mandatory(会报Invalid value specified for connection string attribute 'Encrypt')。
坑6:沙箱/本地脚本报 18456,先查拼写与连接串属性
- 教训:WorkBuddy 沙箱或本地脚本连
IP:1433报 18456,先怀疑登录名拼写 / 连接串属性,而不是「沙箱网络身份被 SQL Server 区分」。 - 验证方法:用本 skill 模板先在沙箱内试连;能连就直接在 WorkBuddy 里跑只读查询,无需用户在外部终端操作。
脚本模板(生成给用户独立运行)
见 scripts/connect_template.py,参数化 server/port/database/user/password,支持:
- 分阶段连接测试(先连 master 验证登录,再连目标库)
- 列出前 20 张表 / 自定义查询 / 导出 CSV
- 密码走环境变量
DB_PWD或交互输入(不落盘)
用法:
python connect_template.py --server {ip} --port 1433 --database {db} --user {readonly_user}
例如:
python connect_template.py --server 10.0.0.5 --port 1433 --database MESDB --user readonly_user
安全约束
- 凭据仅用于本次连接,不写入任何持久化文件或记忆。
- 默认只读账号;除非用户明确要求,不授予写权限。
- 敏感密码不要硬编码在脚本里——用交互输入或环境变量
os.getenv("DB_PWD")。
Current listing version
1.0.0
Safety and assessment details
Quality assessment
Out of 100 · Assessment report from SkillPi LAB.
Areas to improve
-
缺少 When to Use 章节 Reported severity: Minor
文档中未明确设置 When to Use 章节,虽然适用场景部分描述了使用情境,但缺少标准章节划分
Recommendation: 建议在适用场景后增加 When to Use 章节,明确列出典型使用场景和触发条件
Highlights
- 直接解决企业用户连接 SQL Server 进行只读查询、表结构探查与 CSV 导出的核心痛点
- 覆盖 MES/ERP 等典型业务场景,提供标准化流程与常见错误排查方案
- 显著提升日常高频场景效率
Highlights
- 触发词精准完整,覆盖多种变体如“连 SQL Server”“连接 MES”“读数据库表”“SQL 查询”“pyodbc 连库”“数据库只读账号”“导出 SQL 表”
- 边界清晰,不易与其他 Skill 重叠
Areas to improve
-
文档过长 Reported severity: Minor
文档内容较为详尽,可能影响用户快速获取关键信息
Recommendation: 建议适当精简内容,突出核心流程,或将部分内容移至附录
Highlights
- Phase/步骤清晰,有出口条件
- 含端到端示例,frontmatter完整
- 单文件≤500行
Areas to improve
-
缺少网络超时重试策略 Reported severity: Minor
文档中提到了 Login Timeout,但未详细说明网络不稳定时的重试机制
Recommendation: 建议在脚本模板中增加网络超时重试策略,提高连接可靠性
Highlights
- 工具来源明确,参数正确
- 覆盖主要异常如18456/4060/无效连接属性等
- 不可逆操作有确认机制
The arithmetic mean of the first four original scores, rounded to a whole number.
Static text checks
100 / 100 · skillpi-20261123-no-r3-v1
No current rules matched the text.
This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.
Permissions and checks before use
Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.
These common operations were not identified in the main document. This does not mean no permissions or risks exist.
Security rating and access scope
safe · Static evidence review; not a runtime safety guarantee.
Translation pending; see source documentation.
Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.
User discussion
No verified reviews yet.