QSQL: Data source name too long QODBC3: Unable to connect
-
Hello,
I'm very new to database programming and seem to have a problem getting my db to connect. I'm not even sure if my project can even find it. I was wondering if you could help me. I get the error:bq. [Microsoft][ODBC Driver Manager]Data source name too long QODBC3: Unable to connect
My db file name is "SpellingWords.sdf" I kind of followed along with a tutorial but was still pretty confused on how i make the database info match mine. I am using SQL Server 2008 Compact Edition.
@
WordEditor::WordEditor(QWidget *parent) :
QDialog(parent),
ui(new Ui::WordEditor)
{
ui->setupUi(this);
QString servername = "LocalHost\SQLEXPRESS";
QString dbname = "SpellingWords";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
//QString dsn = QString("DRIVER=(SQL Native Client);SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").arg(servername).arg(dbname);
QString dsn = QString("..\..\SpellingBee\SpellingWords.sdf");
db.setDatabaseName(dsn);if(db.open()) { QMessageBox mBox; mBox.setText("Open!"); mBox.exec();
}
else
{
QMessageBox mBox;
mBox.setText(db.lastError().text());
mBox.exec();
}
}
@ -
Why the backslashes in the dsn QString? The name is hard-coded anyway, you're not performing any search for a file this way. Leave it as just "SpellingWords.sdf", which, according to "QSqlDatabase dcoumentation":http://qt-project.org/doc/qt-5.0/qtsql/qsqldatabase.html#setDatabaseName, is one of the recommended names for an ODBC-connected database:
bq. For the QODBC driver, the name can either be a DSN, a DSN filename (in which case the file must have a .dsn extension), or a connection string.