时间:2021-07-01 10:21:17 帮助过:11人阅读
# -*- coding: utf-8 -*- import clr clr.AddReference('System.Data') from System.Data import * from functools import wraps conn_str = "server=localhost;database=DB_TEST;uid=sa;password=sa2008" def mssql(sql): def handler_result(rs): rst = [] while rs.Read(): rst.Add(rs[0]) return rst def decorator(fn): @wraps(fn) def wrapper(*args, **kwargs): TheConnection = SqlClient.SqlConnection(conn_str) TheConnection.Open() try: MyAction = SqlClient.SqlCommand(sql, TheConnection) MyReader = MyAction.ExecuteReader() except Exception,ex: raise AssertionError(ex) rst_data = handler_result(MyReader) kwargs["sql_rst"] = rst_data result = fn(*args, **kwargs) MyReader.Close() TheConnection.Close() return result return wrapper return decorator @mssql(sql="Select getdate()") def get_data(sql_rst=""): print sql_rst[0] get_data()
算是为了好玩吧,回看了下,可能实际用的机会不多