C语言用ADO插入sqlserver数据库
时间: 2024-02-12 10:06:33 浏览: 104
要在C语言中使用ADO插入SQL Server数据库,你需要使用OLE DB API。以下是一个简单的示例代码,可以将数据插入到数据库中:
```c
#include <stdio.h>
#include <windows.h>
#include <oledb.h>
int main() {
// 定义变量
HRESULT hr;
IDBInitialize *pInitialize = NULL;
IDBCreateSession *pCreateSession = NULL;
IDBCreateCommand *pCreateCommand = NULL;
ICommandText *pCommandText = NULL;
IAccessor *pAccessor = NULL;
IRowset *pRowset = NULL;
HACCESSOR hAccessor = NULL;
DBBINDING dbBinding[3];
BYTE pData[3][50] = { "val1", "val2", "val3" };
// 初始化COM组件
CoInitialize(NULL);
// 创建数据库连接
hr = CoCreateInstance(CLSID_SQLSERVER, NULL, CLSCTX_ALL, IID_IDBInitialize, (void**)&pInitialize);
hr = pInitialize->Initialize();
hr = pInitialize->QueryInterface(IID_IDBCreateSession, (void**)&pCreateSession);
// 创建数据库会话
hr = pCreateSession->CreateSession(NULL, IID_IDBCreateCommand, (IUnknown**)&pCreateCommand);
hr = pCreateCommand->CreateCommand(NULL, IID_ICommandText, (IUnknown**)&pCommandText);
// 设置SQL语句
hr = pCommandText->SetCommandText(DBGUID_DBSQL, "INSERT INTO myTable (col1, col2, col3) VALUES (?, ?, ?)");
// 绑定参数
dbBinding[0].iOrdinal = 1;
dbBinding[0].dwPart = DBPART_VALUE;
dbBinding[0].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
dbBinding[0].eParamIO = DBPARAMIO_INPUT;
dbBinding[0].cbMaxLen = 50;
dbBinding[0].pTypeInfo = NULL;
dbBinding[0].pObject = NULL;
dbBinding[0].pBindExt = NULL;
dbBinding[0].wType = DBTYPE_STR;
dbBinding[0].bPrecision = 0;
dbBinding[0].bScale = 0;
dbBinding[0].obValue = 0;
dbBinding[0].obLength = 0;
dbBinding[0].obStatus = 0;
dbBinding[1].iOrdinal = 2;
dbBinding[1].dwPart = DBPART_VALUE;
dbBinding[1].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
dbBinding[1].eParamIO = DBPARAMIO_INPUT;
dbBinding[1].cbMaxLen = 50;
dbBinding[1].pTypeInfo = NULL;
dbBinding[1].pObject = NULL;
dbBinding[1].pBindExt = NULL;
dbBinding[1].wType = DBTYPE_STR;
dbBinding[1].bPrecision = 0;
dbBinding[1].bScale = 0;
dbBinding[1].obValue = 50;
dbBinding[1].obLength = 0;
dbBinding[1].obStatus = 0;
dbBinding[2].iOrdinal = 3;
dbBinding[2].dwPart = DBPART_VALUE;
dbBinding[2].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
dbBinding[2].eParamIO = DBPARAMIO_INPUT;
dbBinding[2].cbMaxLen = 50;
dbBinding[2].pTypeInfo = NULL;
dbBinding[2].pObject = NULL;
dbBinding[2].pBindExt = NULL;
dbBinding[2].wType = DBTYPE_STR;
dbBinding[2].bPrecision = 0;
dbBinding[2].bScale = 0;
dbBinding[2].obValue = 100;
dbBinding[2].obLength = 0;
dbBinding[2].obStatus = 0;
hr = pCommandText->QueryInterface(IID_IAccessor, (void**)&pAccessor);
hr = pAccessor->CreateAccessor(DBACCESSOR_PARAMETERDATA, 3, dbBinding, 0, &hAccessor, NULL);
// 执行SQL语句
hr = pCommandText->Execute(NULL, IID_IRowset, NULL, NULL, (IUnknown**)&pRowset);
hr = pAccessor->ReleaseAccessor(hAccessor, NULL);
hr = pRowset->Release();
hr = pAccessor->Release();
hr = pCommandText->Release();
hr = pCreateCommand->Release();
hr = pCreateSession->Release();
hr = pInitialize->Release();
// 释放COM组件
CoUninitialize();
return 0;
}
```
这个代码中,`myTable` 是你要插入数据的表名,`col1`、`col2`、`col3` 分别是表中的列名。你需要将这些信息替换为你自己的信息。最后,通过执行 `pCommandText->Execute` 函数来执行插入操作。
阅读全文