Tuesday, August 19, 2008

Ironpython - ADO.NET

Here is a very simple example on accessing ADO.NET
Feel free to comment

# Module Init.py

import clr
clr.AddReference("System.Data")
from System.Data import *
from System.Data.SqlClient import *

dbCn = SqlConnection("""Data Source=Python\Sqlexpress;Initial Catalog="C:\SQL SERVER 2000 SAMPLE DATABASES\NORTHWND.MDF";Integrated Security=True""")


dbCn.Open()


ds = DataSet()
dbDa = SqlDataAdapter()


class Table:

def __init__(self,name,command=None):
self.name = name
if command:
self.command = command
else:
self.command = "select * from %s"%self.name
self.sqlCommand()
self.fill_dbDa()

def sqlCommand(self):
self.dbCmd = SqlCommand(self.command,dbCn)
self.CommandType = CommandType.Text
dbDa.SelectCommand = self.dbCmd

def fill_dbDa(self):
dbDa.Fill(ds,self.name)
self.dataTable = ds.Tables[self.name]

dbCn.Close()




# File default.asp.py
import Init

def Page_Load(sender, e):
products = Init.Table("Products")
GridView1.DataSource = products.dataTable
GridView1.DataBind()

No comments:

Post a Comment