2014-03-05 20 views
1

我们可以从Iron Python脚本文件访问SQL Server数据吗?如果是这样,我怎么尝试下面的代码?我们可以从Iron Python脚本文件访问SQL Server数据吗?如果是的话我怎么尝试下面的代码?

import sys 

import clr 
clr.AddReference("System.Windows") 
from System.Windows import Application 

import pypyodbc 
constr="DRIVER={SQL Server Native Client 10.0};Data Source=SERVERName;Initial Catalog=DBName;Integrated Security=True" 
cnxn=pypyodbc.connect(constr) 

da=cnxn.cursor("Select Ename from tbl_Empployee where Emp_id=1",cn) 
da.execute(); 

for row in da.fetchall(): 
    for field in row: 
     Application.Current.RootVisual.FindName("textBox2").Text = field 

回答

1

您可以使用.NET原生类型,请参阅:What's the simplest way to access mssql with python or ironpython?

你的代码看起来是这样的:

import clr 
clr.AddReference('System.Data') 
from System.Data.SqlClient import SqlConnection, SqlParameter 

conn_string = 'data source=SRG3SERVER; initial catalog=vijaykumarDB; trusted_connection=True' 
connection = SqlConnection(conn_string) 
connection.Open() 
command = connection.CreateCommand() 
command.CommandText = 'Select Ename from tbl_Empployee where [email protected]' 
command.Parameters.Add(SqlParameter('@employeeId', 1)) 

reader = command.ExecuteReader() 
while reader.Read(): 
    print reader['Ename'] 

connection.Close() 
+0

我使用Microsoft SQL Server retriving数据如何分配给用户的控制之后 – Vijaykumar

相关问题