An example of using the decorator to execute SQL operations in ironpython

Source: Internet
Author: User
This article mainly introduces the example of using the decorator to execute SQL operations in ironpython. taking MySQL as an example, if you need a decorator, you can refer to the decorator that prefers python, use the decorator to pass the SQL statement and run the returned result.
This application should be relatively small
For convenience, ironpython is used directly to connect to the mssql server.

# -*- coding: utf-8 -*-import clrclr.AddReference('System.Data')from System.Data import *from functools import wrapsconn_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()

It's a good joke. I looked back and found that there may not be many practical opportunities.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.