PDA

View Full Version : connect to sql server



raphaelf
26th February 2006, 14:44
Hi everybody,

QT: 4.1.1
Compiler: MINGW 4.1
OS: WINXP

I am trying to connect to my local sql server, and i was not able:


QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("DRIVER={SQL Server};SERVER=localhost;DATABASE=inventar;UID=rap ha;PWD=raphaelf");
db.setUserName("rapha");
db.setPassword("raphaelf");
if(!db.open())
{
QMessageBox::information(this, "Application name",
"not connected");
return false;

}
else
QMessageBox::information(this, "Application name",
"connected");
return true;

Postgres connection works perfect:


QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("DRIVER={PostgreSQL};SERVER=localhost;DATABASE=inve ntar;UID=rapha;PWD=raphaelf");
db.setUserName("rapha");
db.setPassword("raphaelf");
if(!db.open())
{
QMessageBox::information(this, "Application name",
"not connected");
return false;

}
else
QMessageBox::information(this, "Application name",
"connected");
return true;


What need i to connect to my sql server?

wysota
26th February 2006, 14:59
What kind of SQL server is that?

raphaelf
26th February 2006, 15:05
Hi,
Microsoft SQL Server 2000 Version 8.0

wysota
26th February 2006, 15:58
Did you try skipping that DRIVER parameter in your database name?

raphaelf
26th February 2006, 16:35
hi wisota,
yes it not works


QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("inventar");
db.setUserName("rapha");
db.setPassword("raphaelf");


with the QT example SQL Browser, i was also not able to connect using ODBC

if i configure a "inventar.dsn" file in administrative tools (control panel winxp). I was able to connect OVER the SQL Browser Example but not from my app.
I will have in the future my database in a server. So i must be shure that on clients are no configurations needed or required. At my office where i work i have a app running that i can connect from client to server without to configure something special. (This program was programmed with QT3.3.4.)
I am using the same strategy to solve my problem, so i am not understanding whats wrong

wysota
26th February 2006, 18:44
Did you make an entry in the odbc manager for your database?

raphaelf
26th February 2006, 19:15
hi wysota..
with my old program (QT3). I could connect me without to configure anything on the client and on the server like this:


bool MainWindow::Verbinden()
{ QSqlDatabase *db = QSqlDatabase::addDatabase("QODBC3");
db->setHostName("pcpsr5");
db->setDatabaseName("DRIVER={SQL Server};SERVER=PCPSR5;DATABASE=inventar;UID=AdminC I;PWD=xxxxx");
db->setUserName("AdminCI");
db->setPassword("xxxxx");
if(!db->open()){
db->lastError().showMessage();
return false;
}
return true;
}


Why should the same strategy not work with qt4

dimitri
26th February 2006, 19:57
Has Qt been built with ODBC support? What is the output of QSqlDatabase::drivers() (http://doc.trolltech.com/4.1/qsqldatabase.html#drivers)?

The name of the driver is QODBC, not QODBC3.

raphaelf
26th February 2006, 20:18
Hi Dimitri.
I have installed QT4.1.1 not from binary..from the exe file

My output of QSqlDatabase::drivers() is: QODBC, QODBC3, QSQLITE

My last Post was a example for QT3 thats why QODBC3...

Example SQL Browser show me QSQLITE and QODBC and not QODBC3

wysota
26th February 2006, 21:36
Do you get any messages from Qt? What exactly happens?

raphaelf
26th February 2006, 22:15
hi,
with following code i ca not read the message complete, but it say something about Error on connecting user "sa". Reason: no security SQL Server connection..


QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("DRIVER={SQL Server};SERVER=localhost;DATABASE=inventar;UID=sa; PWD=raphaelf");
db.setUserName("sa");
db.setPassword("raphaelf");
if(!db.open()){
QMessageBox::information(this,"",db.lastError().text());
return false;
}
return true;


If i try like following i get this error: Data Source Name not found and no default driver specified QODBC3 : Unable to connect


..
db.setDatabaseName("inventar");
..

wysota
26th February 2006, 23:20
So the problem lies not in Qt or your application but in the database engine configuration. Try to connect with a different user, MS SQL may have non-secure connections for "sa" (system-admin) user disabled.

sungaoyong
27th February 2006, 03:06
/************************************************** ******
**连接sqlserver
**
************************************************** *******/
bool UpgradeWizardPage::ConnectionSqlServer(QString user,QString password,QString host,QString database)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC","QODBC1");//and connectionName

// db= QSqlDatabase::addDatabase("QODBC","QODBC1");
db.setDatabaseName("DRIVER={SQL Server};Server="+host+";Database="+database+";Uid="+user+";Pwd="+password+";");
db.setHostName(host);

db.setUserName(user);
db.setPassword(password);
db.setConnectOptions("SQL_ATTR_CONNECTION_TIMEOUT=60");

if (!db.open()) {
return false;
}else{
return true;
}
}


it's worked well,why you have programe?have you installed the sqlserver's odbc driver???

raphaelf
27th February 2006, 15:42
hi everybody,

I have tried at my Office to connect from client to our Server and it run Perfekt! We have MS SQL Server 7.0 installed there..hmmm..could by that ODBC doesent support the newer Version?

Thanks for all replies

here the code:


QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("pcpsr5");
db.setDatabaseName("DRIVER={SQL Server};SERVER=pcpsr5;DATABASE=inventar;UID=sa;PWD =xxxxx");
db.setUserName("sa");
db.setPassword("xxxxx");
if(!db.open())
{
QMessageBox::information(this, "Application name",
"not connected");
return false;

}
else
QMessageBox::information(this, "Application name",
"connected");
return true;

wysota
27th February 2006, 17:11
It could be that in the office the server is configured differently. Try to connect to the server without Qt, using regular sql console provided with the dbms (if there is one) to see if you can connect to the server at all.

raphaelf
27th February 2006, 19:06
Hi Wysota!

You are right..It was not a QT Problem. It was on my SQL Server at my Machine..
It was a Authentification Setting. I had to choose: "SQL Server and Windows"

Thank you very much...

Thanks for open my mind ;)

This is the best forum!