Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL MFC
I had created a sql database with SQL 2008 express and the database is saved in a folder (not network). In my MFC application, I wish to open this database but it always prompts SQL server login dialog. I wonder why it cant get the database open?
 
CDatabase database;
	CString SqlString;
	CString sDriver = "SQL Server";
	CString sDsn;
	CString sFile = "C:\\MyFolder\\Libraries.mdf";
	
	sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
 
	// Open the database
	if(database.Open(NULL,false,false,sDsn))
	  AfxMessageBox("database opened!");
        else{
         .....
        }

Can someone tell me why?
Posted 10-Jun-12 23:25pm
Edited 10-Jun-12 23:32pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you don't access a SQL DB through the MDF file, you do it through SQL server, so your connection string is wrong
 
You'll need SQL2008 installed on your local machine to access that DB
 
Have a look at this[^] for examples
  Permalink  
Comments
sally8998 at 11-Jun-12 20:56pm
   
Dear barneyman, I had got SQL2008 express installed in my system. I checked the web you suggested, if the database is in my local machine, what should i put for the "server" for the connection string? Thanks!
barneyman at 11-Jun-12 20:58pm
   
your machine name, or 'localhost' if you want to deploy this on other machines without having to change the DSN
sally8998 at 11-Jun-12 21:47pm
   
It still failed even I changed it to:
 
CDatabase database;
CString SqlString;
CString sDriver = "SQL Server";
CString sDsn, sMc;
sMc.Format("localhost");
CString sFile = "C:\\MyFolder\\Libraries.mdf";

sDsn.Format("ODBC;DRIVER={%s};Server=%s;Database=%s;Trusted_Connection=yes",sDriver,sMc,sFile);
 

// Open the database
if(database.Open(NULL,false,false,sDsn))
AfxMessageBox("database opened!");
barneyman at 11-Jun-12 21:59pm
   
sFile should be the name of the database, as SQL sees it (Libraries, I'd guess), *not* the pathname to the file
sally8998 at 12-Jun-12 0:12am
   
Finally I make it! The server and database were not correct. Thanks, barneyman!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 436
1 Maciej Los 249
2 BillWoodruff 199
3 /\jmot 180
4 Suraj Sahoo | Coding Passion 170
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 6 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100