无法使用CDatabase打开SQL数据库 [英] Can not open SQL database with CDatabase

查看:203
本文介绍了无法使用CDatabase打开SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用SQL 2008 express创建了一个sql数据库,数据库保存在一个文件夹(不是网络)中。在我的MFC应用程序中,我希望打开此数据库,但它始终会提示SQL Server登录对话框。我想知道为什么它不能打开数据库?



 CDatabase数据库; 
CString SqlString;
CString sDriver =SQL Server;
CString sDsn;
CString sFile =C:\\MyFolder \\Libraries.mdf;

sDsn.Format(ODBC; DRIVER = {%s}; DSN =''''; DBQ =%s,sDriver,sFile);

//打开数据库
if(database.Open(NULL,false,false,sDsn))
AfxMessageBox(数据库打开!);
else {
.....
}



有人可以告诉我原因吗?

解决方案

你不能通过MDF文件访问SQL DB,你是通过SQL服务器访问的,所以你的连接字符串是错误的



您需要在本地计算机上安装SQL2008才能访问该数据库



查看 [ ^ ] for实例

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?

解决方案

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


这篇关于无法使用CDatabase打开SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆