如何将一个数组中的数据利用ADO 写入SQL数据库中
各位大哥,小弟刚开始学习ado,想把数组中的数据逐个放入到 数据库中。
我的程序是这样的
//建立数据库
struct Module{
char Modle[16];
char DO[16];
}
light[255];
void Pulling::Write();
{
//初始化
::CoInitialize(NULL);
try
{ // 创建 Connection对象
m_pConnection.CreateInstance("ADODB.Recordset");
//设置连接字符串,必须是BSTR型或者是_bstr_t类型
_bstr_t strConnect="Provider=SQLOLEDB;Server=ntsever;Database=light;";//Database 数据库名称
m_pConnection->Open(strConnect,"","",adModeUnknown);
}
//捕捉异常
catch(_com_error e)
{ //显示错误信息
AfxMessageBox(e.Description());
}
return m_pRecordset; //返回记录
//设置SELECT语句
_bstr_t vSQL;
vSQL = "SELECT * FROM light ORDER BY DeviceNum";//根据sql的数据模块编号;
//执行SELETE语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
//执行SELECT语句
_variant_t var;
CString strIP,strNum,strwModule;
//m_pRecordset->BOF判断是否 在第一条记录前面
try
{
if(!m_pRecordset->adoBOF)
m_pRecordset->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
return;
}
}
//添加新的数据到数据库中
try
{
if(!m_pRecordset->adoBOF)
m_pRecordset->MoveFirst();
else
{
AfxMessageBox("表内数据为空");
return;
}
if(!m_pRecordset->Supports(adAddNew))
return;
m_pRecordset->AddNew();
//写入数据
int i=0;
int number;
char light[16];
while(!m_pRecordset->adoEOF)
{
number =light[i].Modle ;
light[]=light[i].DO;
// 一个记录读取完后,需要向下移动指针,同时数组下标加1
m_pRecordset->Fields->GetItem(_variant_t("模块编号"))->Value=_bstr_t("number");
m_pRecordset->Fields->GetItem(_variant_t("灯具一"))->Value=_bstr_t("light[0]");
m_pRecordset->Fields->GetItem(_variant_t("灯具二"))->Value=_bstr_t("light[1]");
m_pRecordset->Fields->GetItem(_variant_t("灯具三"))->Value=_bstr_t("light[2]");
m_pRecordset->Fields->GetItem(_variant_t("灯具四"))->Value=_bstr_t("light[3]");
m_pRecordset->Fields->GetItem(_variant_t("灯具五"))->Value=_bstr_t("light[4]");
m_pRecordset->Fields->GetItem(_variant_t("灯具六"))->Value=_bstr_t("light[5]");
m_pRecordset->Update();
//指向下一个数据
m_pRecordset->MoveNext();
i++;
}
//try
catch (_com_error &e)
{
AfxMessageBox(e.Description());
}
}
但是运行程序有错误,请帮我看下我的问题,谢谢