我有全部用代码写的例子,包括建立数据源在内 -------------- 先建立数据库 在建立数据源 ----------------- 部分代码:sql2000 void CMainFrame::Create_database_DSN() { /* --------------------------------- 你需要事先在F盘上建立一个目录:F:\csdn_db\data 用来存放sql2000的数据库文件和日志文件 --------------------------------- */ RETCODE retcode; char *szDriver = "SQL Server"; char *szAttributes = "DSN=Master_DSN\0DESCRIPTION=DSN used for the Visual C++ database test!\0"//数据源的名称为:TEST_word "SERVER=YPCPSU\\psusong\0"//改成你的sql2000的实例名(服务器名) "DATABASE=master\0";//先建立master 数据库的数据源,然后用此数据源建立自己的数据库,涉及到权限问题 retcode = SQLConfigDataSource(NULL, ODBC_ADD_DSN, szDriver, szAttributes); if(retcode) MessageBox("you have created the data source used for the MASTER database!"); else { MessageBox("failed to create the data source ! check it!"); }
CStdioFile db_scriptFile; CStdioFile table_scriptFile; CString str_db_script,str_table_script; CString strError; CString strTemp; str_db_script=""; db_scriptFile.Open("F:\\csdn_db\\db_script\\word_databse.sql",CFile::modeRead); while(db_scriptFile.ReadString(strTemp)) { str_db_script+=strTemp; str_db_script+='\n'; } db_scriptFile.Close(); table_scriptFile.Open("F:\\csdn_db\\db_script\\word_table.sql",CFile::modeRead); str_table_script=""; while(table_scriptFile.ReadString(strTemp)) { str_table_script+=strTemp; str_table_script+='\n'; } table_scriptFile.Close(); //打开刚才创建的master数据源 if(m_gDatabase.OpenEx( _T( "DSN=Master_DSN;UID=sa;PWD=psusong" ), CDatabase::openReadOnly | CDatabase::noOdbcDialog )) { try { MessageBox("begin creating database"); m_gDatabase.ExecuteSQL(str_db_script); MessageBox("end of creating database"); MessageBox("begin creating table"); m_gDatabase.ExecuteSQL(str_table_script); MessageBox("end of creating table"); } catch(CDBException e) { strError.Format("When created database ,have occured error: %d ",e.m_nRetCode); MessageBox(strError); } } //为上面创建的数据库建立数据源 char *szSQLDriver = "SQL Server"; char *szSQLAttributes = "DSN=WORD_DSN\0DESCRIPTION=DSN used for the Visual C++ database test!\0" "SERVER=YPCPSU\\psusong\0" "DATABASE=csdn_db\0"; retcode = SQLConfigDataSource(NULL, ODBC_ADD_DSN, szSQLDriver, szSQLAttributes); if(retcode) MessageBox("you have created the data source used for the WORD database!"); else { MessageBox("failed to create the data source ! check it!"); } }
这里有很多例子,看看吧。
http://www.codeproject.com/database/
在STDAFX.H中加入#INCLUDE "AFXDAO.H"
然后在程序中建好相应的CRecordSset类等等,就能用了
如果呢使用ODBC API,只要在ODBC连接字符串中指定数据文件也可以
--------------
先建立数据库
在建立数据源
-----------------
部分代码:sql2000
void CMainFrame::Create_database_DSN()
{
/*
---------------------------------
你需要事先在F盘上建立一个目录:F:\csdn_db\data 用来存放sql2000的数据库文件和日志文件
---------------------------------
*/
RETCODE retcode;
char *szDriver = "SQL Server";
char *szAttributes =
"DSN=Master_DSN\0DESCRIPTION=DSN used for the Visual C++ database test!\0"//数据源的名称为:TEST_word
"SERVER=YPCPSU\\psusong\0"//改成你的sql2000的实例名(服务器名)
"DATABASE=master\0";//先建立master 数据库的数据源,然后用此数据源建立自己的数据库,涉及到权限问题 retcode = SQLConfigDataSource(NULL,
ODBC_ADD_DSN,
szDriver,
szAttributes);
if(retcode)
MessageBox("you have created the data source used for the MASTER database!");
else
{
MessageBox("failed to create the data source ! check it!");
}
CStdioFile db_scriptFile;
CStdioFile table_scriptFile;
CString str_db_script,str_table_script;
CString strError;
CString strTemp; str_db_script="";
db_scriptFile.Open("F:\\csdn_db\\db_script\\word_databse.sql",CFile::modeRead);
while(db_scriptFile.ReadString(strTemp))
{
str_db_script+=strTemp;
str_db_script+='\n';
}
db_scriptFile.Close(); table_scriptFile.Open("F:\\csdn_db\\db_script\\word_table.sql",CFile::modeRead);
str_table_script="";
while(table_scriptFile.ReadString(strTemp))
{
str_table_script+=strTemp;
str_table_script+='\n';
}
table_scriptFile.Close(); //打开刚才创建的master数据源
if(m_gDatabase.OpenEx( _T( "DSN=Master_DSN;UID=sa;PWD=psusong" ),
CDatabase::openReadOnly |
CDatabase::noOdbcDialog ))
{
try
{
MessageBox("begin creating database");
m_gDatabase.ExecuteSQL(str_db_script);
MessageBox("end of creating database");
MessageBox("begin creating table");
m_gDatabase.ExecuteSQL(str_table_script);
MessageBox("end of creating table");
} catch(CDBException e)
{
strError.Format("When created database ,have occured error: %d ",e.m_nRetCode);
MessageBox(strError);
}
} //为上面创建的数据库建立数据源
char *szSQLDriver = "SQL Server";
char *szSQLAttributes =
"DSN=WORD_DSN\0DESCRIPTION=DSN used for the Visual C++ database test!\0"
"SERVER=YPCPSU\\psusong\0"
"DATABASE=csdn_db\0";
retcode = SQLConfigDataSource(NULL,
ODBC_ADD_DSN,
szSQLDriver,
szSQLAttributes);
if(retcode)
MessageBox("you have created the data source used for the WORD database!");
else
{
MessageBox("failed to create the data source ! check it!");
}
}
2.在stdafx.h最后面加:#include "afxdb.h"
3.在你的C**DLG的CPP文件里,在INCLUDE之后添加你的CDATABASE DB;
并在INTIDLG函数里面把它跟数据库建立连接,假如你的数据源已经配好:MYDB
4.if(!db.OpenEx(_T("DSN=mydb;UID=sa;PWD=pwd"), CDatabase::noOdbcDialog ))
{
AfxMessageBox("数据库打开失败");
return FALSE;
}如果没有报错,数据库已经连上;你就
db.ExecuteSQL("CREATE DATABASE....");
或者
Recordset rs(&db);
try{
rs.Open(.....);
rs.GetFieldValue
}catch...