Можем ли мы получить доступ к данным SQL Server из файла сценария Iron Python, если да, то как я пробую этот следующий код?

Можем ли мы получить доступ к данным SQL Server из файла сценария Iron Python, если да, то как я пробую этот следующий код?

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

person Vijaykumar    schedule 05.03.2014    source источник


Ответы (1)


Вы можете использовать собственные типы .net, см .: Какой самый простой способ получить доступ к mssql с помощью python или 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 Emp_id=@employeeId'
command.Parameters.Add(SqlParameter('@employeeId', 1))

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

connection.Close()
person Boklucius    schedule 05.03.2014
comment
Я использую сервер Microsoft SQL после получения данных, как назначить элемент управления пользователем - person Vijaykumar; 05.03.2014