PDA

View Full Version : How to dynamicly create a DSN by using Qt code?



bangqianchen
29th December 2009, 15:47
I had developed a database on window platform with SQL Server as background database, now I want to restore the database in the program. As is known, we must close current connection before the operation. I know the SQL Server had a master database for default, I don't want to create this DSN by hand, Can I dynamicly create a dsn for the program? I find a example for Access database, but no example for SQL Server!



...
db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
if (db.open()) {
// success!
}
...

wysota
30th December 2009, 00:15
You have to fill a QSqlDatabase object with appropriate data (hostname, port, login, pass, database name) using the dedicated methods.