0

I connected MSSQL (on windows) via odbc on linux redhat 64bit.
Below is odbc and freetds configuration files.

freetds.conf:

[FreeTDS]  
host=172.21.32.20  
port=1433  
tds version=7.2  
client charset = GB2312   

odbcinst.ini:

[TDS]  
Description = TDS  
Driver = /usr/local/lib/libtdsodbc.so  
Trace = Yes  
TraceFile=/tmp/odbcinst.log  
UsageCount = 1  

odbc.ini:

[SQLSERVER]  
Driver = TDS  
Description = sql server dsn  
Trace = NO  
Server = 172.21.32.20  
Database = Live  
Port = 1433  
TDS_Version = 7.2  
TraceFile=/tmp/sqlserver.log  

Snippets:

QString f3ErpDsn = QString::fromLocal8Bit("Driver={SQLSERVER};server=172.21.32.20;database=Live;uid=inp;pwd=inp;");
QSqlDatabase f3ErpDb= QSqlDatabase::addDatabase("QODBC");
f3ErpDb.setDatabaseName(f3ErpDsn);
// f3ErpDb.setDatabaseName("SQLSERVER");
f3ErpDb.setUserName("inp");
f3ErpDb.setPassword("inp");
QString sqlCmdStr = "select * from dbo.Table_Drilling_Data01 where status=1";
if ( ! f3ErpDb.open() )
{
        qDebug() << f3ErpDb.lastError().text();
        return 1;
}
QSqlTableModel model;
model.setTable("dbo.Table_Drilling_Data01");
model.setFilter("status=3");
model.select();
qDebug() << model.lastError().text();

Error messages:

  1. f3ErpDb.setDatabaseName(f3ErpDsn);
    If setDatabaseName is f3ErpDsn error message is:

    "QODBC3: Unable to connect", "[unixODBC][Driver Manager]Data source name not found, and no default driver specified")

    I had defined SQLSERVER in odbc.ini. Why it can not find it?

  2. But if I changed f3ErpDb.setDatabaseName("SQLSERVER");, error message is:

    "[FreeTDS][SQL Server]The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 1 (""): Data type 0x00 is unknown. QODBC3: Unable to execute statement"

It makes me confounded, could anybody help me?

2 Answers 2

0

I'm not sure what QSqlDatabase is. But it seems that you have improper SQL connection string.

Try something like this:

UID=inp;PWD=inp;DSN=SQLSERVER;database=Live;SERVER=AMIN\SQLEXPRESS;charset=UTF-8

DSN=SQLSERVER because SQLSERVER is the only DSN which defined in your odbc.ini

SERVER=AMIN\SQLEXPRESS - try Windows name of SQL Server, not host's IP address (I'm not sure that IP is not allowed here, but Windows name is 100% working)

10
  • but this code runs successful on windows. and i connected database via freetds perfectly.
    – ken
    Oct 30, 2012 at 0:44
  • hmm, could you provide your windows ODBC settings? You see, when you write "Driver={SQLSERVER};server=172.21.32.20;database=Live;uid=inp;pwd=inp;", you don't provide DSN at all, so you don't use odbc.ini settings. On the other hand, you didn't specify driver named SQLSERVER in odbcinst.ini, so your connection string will not work. Specify DSN=SQLSERVER or Driver={TDS} in connection string
    – Yanushko
    Oct 30, 2012 at 4:56
  • ------------- ODBCINST.ini ------------- [SQL Server (32 λ)] \n Driver=C:\WINDOWS\system32\sqlsrv32.dll Setup=C:\WINDOWS\system32\sqlsrv32.dll 32Bit=1 there is not any setting about sqlserver in ODBC.INI file.
    – ken
    Oct 30, 2012 at 6:26
  • by the way, do you know how to create new line in comment? when i press "Enter" key, it saves the comment.
    – ken
    Oct 30, 2012 at 6:32
  • anybody know? my odbc driver problem
    – ken
    Nov 1, 2012 at 0:32
0

I had the same problem and I solve it when I changed TDS protocol to 8.0, it was at 7.0 before. I've used freetds, unixodbc, via Qt to connect to MSSQL database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.