How to connect to SQL Server 2008 with qt?

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 also added qtsql4 and qtsqld4 libs, but now the problem is that I get an error that the database could not be opened ... I am very new to qt. I do not know how to do that? So, any help on how to connect to the database, what else needs to be added or where am I mistaken?

+5
source share
2 answers

After long attempts, 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(); 
+9
source

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") 
+1
source

All Articles