开发者

How to connect to sql server 2008 via an IP address using pyodbc in Python

I am able to connect to server with mssql management studio but not able to connect using python I think some problem in connection string please help below is string I am using.

import pyodbc as p

connStr = ( r'DRIVER={SQL Server};Server=ip; Network=DBMSSOCN;Initial Catalog=' + database + ';User ID=' + id +';Password=' + pass1 +';Trusted_Connection=True' +';')

conn = p.connect(connStr)

error is like below

 conn = p.connect(connStr)
pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][TCP/IP Sock
ets]SQL Server does not exist or access denied. (17) (SQLDriver开发者_C百科ConnectW); [01000
] [Microsoft][ODBC SQL Server Driver][TCP/IP Sockets]ConnectionOpen (Connect()).
 (10060); [01S00] [Microsoft][ODBC SQL Server Driver]Invalid connection string a
ttribute (0)')


After lots of trial and error this string ended up working:

connStr = ('DRIVER={SQL Server Native Client 10.0};Server=ip;port=port;Network Library=DBMSSOCN;Database=TEST;uid=id;pwd=pass;')


There are actually two or three SQL Server drivers written and distrubuted by Microsoft: one referred to as "SQL Server" and the other as "SQL Native Client" and "SQL Server Native Client 10.0}".

DRIVER={SQL Server};SERVER=cloak;DATABASE=test;UID=user;PWD=password

DRIVER={SQL Native Client};SERVER=dagger;DATABASE=test;UID=user;PWD=password

DRIVER={SQL Server Native Client 10.0};SERVER=dagger;DATABASE=test;UID=user;PWD=password

The "SQL Server" one works for all versions of SQL Server, but only enables features and data types supported by SQL Server 2000, regardless of your actual server version.

For SQL Server 2005 installations, use "SQL Native Client" to enable 2005 features and types. Note that this version is not provided in all SQL Server 2008 installations!

Finally, you need "SQL Server Native Client 10.0" for SQL Server 2008 features and types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜