PDA

View Full Version : How to connect to ms sql server 2008 EXPRESS?



csoapy
2nd April 2010, 13:07
the code:


QString error;
QString conn = "Data Source=.\\SQLEXPRESS;Initial Catalog=master;Integrated Security=False;Persist Security Info=False;"
"User ID=sa;password=[]";

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "MainDBConnection");
db.setDatabaseName(conn);
db.setUserName("sa");
db.setPassword("[]");

if (!db.open())
error = db.lastError().text();

but it not works, error == "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect"

while following run, when i use mssql 2005 enterprise edition.

conn = QString::fromLocal8Bit("DRIVER={SQL SERVER};SERVER=192.168.10.96;DATABASE=master;Uid=s a;Pwd=[]");
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "MainDBConnection");
db.setDatabaseName(conn);
db.setUserName("sa");
db.setPassword("[]");
thx!

ajg85
7th April 2010, 19:22
If you want to use a ODBC connection you'll need to define a DSN and then pass the name of the DSN to setDatabaseName before opening ... you don't need the classic connection string.

For example:


QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "MainDBConnection");
db.setDatabaseName("dsnname"); // define DSN in your /etc/odbc.ini on unix or through ODBC wizard in control panel on windows
// user and pass not needed if you define these in DSN or use windows authentication
db.setUserName("sa");
db.setPassword("sapass");