Accessing SQL Server
From IronPython Cookbook
This simple example shows to use IronPython to access a SQL Server database such as the free SQL 2005 Express edition.
import clr
clr.AddReference('System.Data')
from System.Data import *
TheConnection = SqlClient.SqlConnection("server=yourserver;database=News;uid=sa;password=password")
TheConnection.Open()
MyAction = SqlClient.SqlCommand("Select Headline from News", TheConnection)
MyReader = MyAction.ExecuteReader()
while MyReader.Read():
print MyReader[0]
MyReader.Close()
TheConnection.Close()
More detail on the Reader class is available on MSDN.
Back to Contents.

