如何用vc访问access数据库

twins222 2009-09-27 02:51:57
我的任务是将程序中得到的一个字符串“1001EY125X02511025”写入到一个数据库中,我想采用ODBC方式,利用vc去访问access数据库,哪位给我讲讲方法,给我提供一个网址也行,在网上查找了很多东西,说的都比较乱。谢谢大家。
...全文
190 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
upldel 2009-10-18
  • 打赏
  • 举报
回复
TOP
ACMAIN_CHM 2009-09-27
  • 打赏
  • 举报
回复
通过SQL语句操作数据库。
twins222 2009-09-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 acmain_chm 的回复:]
access 的 connection string

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
[/Quote]
您下面的几句话是什么意思啊?
_bstr_t strSQLChange("UPDATE Titles SET Type = "
"'self_help' WHERE Type = 'psychology'");
_bstr_t strSQLRestore("UPDATE Titles SET Type = "
"'psychology' WHERE Type = 'self_help'");
_bstr_t strCnn("Provider='sqloledb';Data Source='MySqlServer';"
"Initial Catalog='pubs';Integrated Security='SSPI';");

ACMAIN_CHM 2009-09-27
  • 打赏
  • 举报
回复
access 的 connection string

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
twins222 2009-09-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acmain_chm 的回复:]
C++ 通过ADO来操作ACCESS,利用ADO.connection.execute 来执行SQL语句。

Execute、Requery 和 Clear 方法范例 (VC++)。。。。
[/Quote]
您的这个程序很好。我要是添加我的数据源的时候是不是把Provider='sqloledb';Data Source='MySqlServer';修改成Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb(test.mdb)是我的数据库名称。
ACMAIN_CHM 2009-09-27
  • 打赏
  • 举报
回复
C++ 通过ADO来操作ACCESS,利用ADO.connection.execute 来执行SQL语句。

Execute、Requery 和 Clear 方法范例 (VC++)
本范例演示从 Command 和 Connection 对象运行 Execute 方法。范例中还使用 Requery 方法检索 Recordset 中的当前数据,并使用 Clear 方法清除 Errors 集合的内容。运行本范例需要 ExecuteCommand 和 PrintOutput 函数。

// BeginExecuteCpp
#include <ole2.h>
#include <stdio.h>

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")

// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void ExecuteX(void);
void ExecuteCommand(_CommandPtr pCmdTemp, _RecordsetPtr pRstTemp);
void PrintOutput(_RecordsetPtr pRstTemp);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

////////////////////////////////
// Main Function //
////////////////////////////////

void main()
{
if(FAILED(::CoInitialize(NULL)))
return;

ExecuteX();

::CoUninitialize();
}

///////////////////////////////////
// ExecuteX Function //
///////////////////////////////////

void ExecuteX(void)
{
HRESULT hr = S_OK;

// Define string variables.
_bstr_t strSQLChange("UPDATE Titles SET Type = "
"'self_help' WHERE Type = 'psychology'");
_bstr_t strSQLRestore("UPDATE Titles SET Type = "
"'psychology' WHERE Type = 'self_help'");
_bstr_t strCnn("Provider='sqloledb';Data Source='MySqlServer';"
"Initial Catalog='pubs';Integrated Security='SSPI';");

// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_ConnectionPtr pConnection = NULL;
_CommandPtr pCmdChange = NULL;
_RecordsetPtr pRstTitles = NULL;

try
{
// Open connection.
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
pConnection->Open (strCnn, "", "", adConnectUnspecified);

// Create command object.
TESTHR(pCmdChange.CreateInstance(__uuidof(Command)));
pCmdChange->ActiveConnection = pConnection;
pCmdChange->CommandText = strSQLChange;

// Open titles table, casting Connection pointer to an
// IDispatch type so converted to correct type of variant.
TESTHR(pRstTitles.CreateInstance(__uuidof(Recordset)));
pRstTitles->Open ("Titles", _variant_t((IDispatch *) pConnection,
true), adOpenStatic, adLockOptimistic, adCmdTable);

// Print report of original data.
printf(
"\n\nData in Titles table before executing the query: \n");

// Call function to print loop recordset contents.
PrintOutput(pRstTitles);

// Clear extraneous errors from the Errors collection.
pConnection->Errors->Clear();

// Call ExecuteCommand subroutine to execute pCmdChange command.
ExecuteCommand(pCmdChange, pRstTitles);

// Print report of new data.
printf(
"\n\n\tData in Titles table after executing the query: \n");
PrintOutput(pRstTitles);

// Use the Connection object's execute method to
// execute SQL statement to restore data.
pConnection->Execute(strSQLRestore, NULL, adExecuteNoRecords);

// Retrieve the current data by requerying the recordset.
pRstTitles->Requery(adCmdUnknown);

// Print report of restored data.
printf(
"\n\n\tData after exec. query to restore original info: \n");
PrintOutput(pRstTitles);
}
catch (_com_error &e)
{
PrintProviderError(pConnection);
PrintComError(e);
}

// Clean up objects before exit.
if (pRstTitles)
if (pRstTitles->State == adStateOpen)
pRstTitles->Close();
if (pConnection)
if (pConnection->State == adStateOpen)
pConnection->Close();
}

//////////////////////////////////////////
// ExecuteCommand Function //
//////////////////////////////////////////

void ExecuteCommand(_CommandPtr pCmdTemp, _RecordsetPtr pRstTemp)
{
try
{
// CommandText property already set before function was called.
pCmdTemp->Execute(NULL, NULL, adCmdText);

// Retrieve the current data by requerying the recordset.
pRstTemp->Requery(adCmdUnknown);
}

catch(_com_error &e)
{
// Notify user of any errors that result from
// executing the query.
// Pass a connection pointer accessed from the Recordset.
PrintProviderError(pRstTemp->GetActiveConnection());
PrintComError(e);
}
}

/////////////////////////////////////
// PrintOutput Function //
/////////////////////////////////////

void PrintOutput(_RecordsetPtr pRstTemp)
{
// Ensure at top of recordset.
pRstTemp->MoveFirst();

// If EOF is true, then no data and skip print loop.
if( pRstTemp->EndOfFile )
{
printf("\tRecordset empty\n");
}
else
{
// Define temporary strings for output conversions.
// Initialize to first record's values.
_bstr_t bstrTitle;
_bstr_t bstrType;

// Enumerate Recordset and print from each.
while(!(pRstTemp->EndOfFile))
{
// Convert variant string to convertable string type.
bstrTitle = pRstTemp->Fields->GetItem("Title")->Value;
bstrType = pRstTemp->Fields->GetItem("Type")->Value;
printf("\t%s, %s \n",
(LPCSTR) bstrTitle,
(LPCSTR) bstrType);

pRstTemp->MoveNext();
}
}
}

///////////////////////////////////////////////
// PrintProviderError Function //
///////////////////////////////////////////////

void PrintProviderError(_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;

if( (pConnection->Errors->Count) > 0)
{
long nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(long i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("\t Error number: %x\t%s", pErr->Number,
pErr->Description);
}
}
}

//////////////////////////////////////
// PrintComError Function //
//////////////////////////////////////

void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());

// Print Com errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}
// EndExecuteCpp
wwwwb 2009-09-27
  • 打赏
  • 举报
回复
用ADO连接MDB,UPDATE数据
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\1.mdb

7,714

社区成员

发帖
与我相关
我的任务
社区描述
Microsoft Office Access是由微软发布的关系数据库管理系统。它结合了 MicrosoftJet Database Engine 和 图形用户界面两项特点。
社区管理员
  • Access
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧