Files
python-mssql-server-interac…/basic-sql-server-interactions-and-dml/connect_sql_server_exception.py
T
2026-08-26 21:08:20 +01:00

23 lines
426 B
Python

import pyodbc
try:
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes')
cursor = connection.cursor()
print()
print("Successfully connected to SQL Server")
print()
cursor.close()
connection.close()
except pyodbc.Error as ex:
print()
print("exception: ", ex)
print("closing program")
print()
exit()
print()