Click here to Skip to main content
16,008,075 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 10-Jun-12 22:32pm
v2

1 solution

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
 
Share this answer
 
Comments
sally8998 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 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 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900