使用 pyodbc

import pandas.io.sql
import pyodbc
import pandas as pd

指定引數

# Parameters
server = 'server_name'
db = 'database_name'
UID = 'user_id'

建立連線

# Create the connection
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db + '; UID = ' + UID + '; PWD = ' + UID + 'Trusted_Connection=yes')

查詢 pandas 資料幀

# Query into dataframe
df= pandas.io.sql.read_sql('sql_query_string', conn)