不通过 pyodbc 中的 sp_detach_db 分离数据库
Does not detach database by sp_detach_db in pyodbc
我正在尝试分离数据库,但由于某种原因它没有正确分离,我是不是遗漏了什么?
import pyodbc
params = 'DRIVER={SQL Server};SERVER=.\RISKSPEC_PSA2012;DATABASE=master;UID=sa;Pwd=sa_pasw;Trusted_Connection=yes;'
try:
with pyodbc.connect(params) as cnxn:
with cnxn.cursor() as cursor:
cnxn.autocommit = True
cursor.execute(
'''
USE [master];
ALTER DATABASE [sss] SET SINGLE_USER WITH NO_WAIT;
EXEC sp_detach_db @dbname=sss;
''')
except pyodbc.Error as ex:
QMessageBox.warning(self, 'pyodbc', ex.args[0])
SQLServer 2012版本:11.0.2100
pyodbc 版本:4.0.31
感谢 Gord Thompson 的小费。
修复:
- 用 NO_WAIT 设置 SINGLE_USER -> SET TRUSTWORTHY ON
- 从连接参数中删除“Trusted_Connection=yes;”,它关联系统用户而不是“sa”用户。
import pyodbc
params = 'DRIVER={SQL Server};SERVER=.\RISKSPEC_PSA2012;DATABASE=master;UID=sa;Pwd=sa_pasw;'
try:
with pyodbc.connect(params) as cnxn:
cnxn.autocommit = True
with cnxn.cursor() as cursor:
cursor.execute(
'''
USE [master];
ALTER DATABASE [sss] SET TRUSTWORTHY ON;
EXEC sp_detach_db @dbname=sss;
''')
except pyodbc.Error as ex:
QMessageBox.warning(self, 'pyodbc', ex.args[0])
我正在尝试分离数据库,但由于某种原因它没有正确分离,我是不是遗漏了什么?
import pyodbc
params = 'DRIVER={SQL Server};SERVER=.\RISKSPEC_PSA2012;DATABASE=master;UID=sa;Pwd=sa_pasw;Trusted_Connection=yes;'
try:
with pyodbc.connect(params) as cnxn:
with cnxn.cursor() as cursor:
cnxn.autocommit = True
cursor.execute(
'''
USE [master];
ALTER DATABASE [sss] SET SINGLE_USER WITH NO_WAIT;
EXEC sp_detach_db @dbname=sss;
''')
except pyodbc.Error as ex:
QMessageBox.warning(self, 'pyodbc', ex.args[0])
SQLServer 2012版本:11.0.2100
pyodbc 版本:4.0.31
感谢 Gord Thompson 的小费。
修复:
- 用 NO_WAIT 设置 SINGLE_USER -> SET TRUSTWORTHY ON
- 从连接参数中删除“Trusted_Connection=yes;”,它关联系统用户而不是“sa”用户。
import pyodbc
params = 'DRIVER={SQL Server};SERVER=.\RISKSPEC_PSA2012;DATABASE=master;UID=sa;Pwd=sa_pasw;'
try:
with pyodbc.connect(params) as cnxn:
cnxn.autocommit = True
with cnxn.cursor() as cursor:
cursor.execute(
'''
USE [master];
ALTER DATABASE [sss] SET TRUSTWORTHY ON;
EXEC sp_detach_db @dbname=sss;
''')
except pyodbc.Error as ex:
QMessageBox.warning(self, 'pyodbc', ex.args[0])