简体   繁体   中英

Postgresql odbc driver error c# [IM002] [Microsoft][ODBC Driver Manager] Data source name not found

I am setting up an database application to be database agnostic, and when testing with postgresql I get the standard dsn error:

[IM002] [Microsoft][ODBC Driver Manager] Data source name not found

I usually use SQL server and MySQL so I'm new to postgres, I tried the standard recommended Connection string:

"Driver = {PostgreSQL}; Server = localhost; Database = postgres; Port = 5432; Uid = postgres; Pwd = XXXXXX;"

I also tried the name of the odbc driver that I installed after installing postrgesql:

"Driver = {PostgreSQL ODBC Driver(UNICODE)}; Server = localhost; Database = postgres; Port = 5432; Uid = postgres; Pwd = XXXXXX;"

Setting up a DSN in odbc manager also works perfectly using the unicode driver, so I cant understand why i cant connect in my application, is there an error in the driver name that i am using in the connection string?

Your error message looks very strange. It tells about DSN not found. Are you sure you use connect string with Driver=... ?

You can use ODBC connect string in several forms. At first you have created DSN, so you can use it:

DSN=mn_test; Uid=postgres; Pwd=postgres;

Then you can use other form of connect string:

Driver={PostgreSQL UNICODE};Server=127.0.0.1; Port=5493; Database=mn_test; Uid=postgres; Pwd=postgres;

Both work on my old 32 bit Windows environment. I test them with simple Python script (I use ActiveState Python in which there is simple odbc module):

import odbc

def test_odbc(connect_string):
    print(connect_string)
    db = odbc.odbc(connect_string)
    c = db.cursor()
    rs = c.execute("SELECT version()")
    for txt in c.fetchall():
        print('%s' % (txt[0]))
    print('-----')

test_odbc('Driver={PostgreSQL UNICODE};Server=127.0.0.1; Port=5493; Database=mn_test; Uid=postgres; Pwd=postgres;')
test_odbc('DSN=mn_test; Uid=postgres; Pwd=postgres;')

When you created the DSN, did you create it with the correct odbcad tool? With the 64bit version found in C:\\Windows\\System32 if your application is 64bit and with the 32bit version found in C:\\Windows\\SysWOW64 if your application is 32bit?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM