8

I am trying to connect to SQL Server 2008 with qt ... I am doing this :

 QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");

 db.setHostName("ITPL_PC1\\SQLEXPRESS");
 db.setDatabaseName("Test");
  db.setUserName("sa");
  db.setPassword("insforia");
  bool ok = db.open();

  //query

  db.close();

I have also added qtsql4 and qtsqld4 libs but now the problem is I am getting an error that database can't be open ... I am very new in qt I don't know how to do this? So any help on how to connect to the database what more should I add or where I am wrong ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Arindam Das
  • 699
  • 4
  • 20
  • 39
  • What does `QSqlDatabase::lastError().text()` say? – hank Jan 28 '13 at 06:12
  • So use it! It will help you to get where the problem is. – hank Jan 28 '13 at 06:22
  • i really don't know where and how to use it – Arindam Das Jan 28 '13 at 06:22
  • when i am using that ... i am getting this error on the run time: `C:\Qt\example\Test1\test1.cpp:48: error: C2352: 'QSqlDatabase::lastError' : illegal call of non-static member function` – Arindam Das Jan 28 '13 at 06:29
  • @ArindamDas a little more precise - this is no **run time** error, but compile time error. Look at the answer to this question - http://stackoverflow.com/questions/846103/runtime-vs-compile-time – borisbn Jan 28 '13 at 07:48
  • after using the `lasterror().text()` i am getting this error... `[Microsoft][ODBC Driver Manager]DataSource not found and no default driver specified QODBC: unable to connect` – Arindam Das Jan 28 '13 at 07:59

2 Answers2

11

After trying for so much time, I finally managed to get this to work ... here's what I did :

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC3");

db.setDatabaseName("DRIVER={SQL Server};Server=ITPL_PC1;Database=Test;Uid=sa;Port=1433;Pwd=*******;WSID=.");

db.open();

QSqlQueryModel *model = new QSqlQueryModel;

QString query = "insert into qttable(PID) values('ARINDAM')";
model->setQuery(query, db);

db.close();
thlik
  • 401
  • 6
  • 12
Arindam Das
  • 699
  • 4
  • 20
  • 39
  • I get "[unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found QODBC3: Unable to connect" when use your code. How do you install SQL Server? I work in Linux. Can you read my question http://stackoverflow.com/questions/29222302/how-can-i-connect-mssql-database-with-qt-4-8-6 – utarid Mar 23 '15 at 23:39
  • I really don't know how to install sql server on a linux os. So you could use some other db like oracle or get some help from net to install sql server on linux – Arindam Das Mar 27 '15 at 17:02
  • @user4757345 you can find the answer in this [link](http://stackoverflow.com/questions/32759022/connecting-to-ms-sqlserver-from-qt-linux-application) – Yehuda G. Jun 28 '16 at 14:25
1

This could be another one:

//2005  
db.setDatabaseName(DRIVER={SQL Server};SERVER=localhost\\SQLExpress;DATABASE=secundaria;UID=sa;PWD=contraseña;WSID=.;Trusted_connection=yes)

//2008
db.setDatabaseName("DRIVER={SQL Server Native Client 10.0};SERVER=localhost\\SQLExpress;DATABASE=myDbName;UID=user;PWD=userPwd;WSID=.;Trusted_connection=yes")

//2012
db.setDatabaseName("DRIVER={SQL Server Native Client 11.0};SERVER=localhost\\SQLExpress;DATABASE=myDbName;UID=user;PWD=userPwd;WSID=.;Trusted_connection=yes")
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
antonio
  • 477
  • 7
  • 18