请教ado.net的问题
程序的目的是通过从N个数中选取M个,然后存入一个字符数组。
链接到sqlserver数据库后,将字符数组逐行写入数据库。
在网上找了一些ADO的教程,可惜不是很理解,编译的时候各种问题,想下手毫无头绪。
小弟刚刚接触VS平台的C++程序,请各位大侠指点。非常感谢
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
#include <stdio.h>
#include <iostream>
#include <icrsint.h>
char Result[1024][7];
int j=0;
char CharArry[4];
//using namespace std;
void Search(int array[], int array_size, int now_index, int choosed_array[], int choosed_count, int max_count)
{
int i = 0;
//int j = 0;
//int Result[20][4];
if (choosed_count > max_count)
return;
if (choosed_count == max_count)
{
/*这里打印choosed_array[0]到choosed_array[choosed_count - 1]*/
for (i = 0; i < choosed_count; i++)
{
//printf("%d ", choosed_array[i]);//把打印改成存入数组
Result[j][i] = choosed_array[i];
}
++j;
//putchar('\n');
return;
}
for (int i = now_index; i < array_size; i++)
{
choosed_array[choosed_count] = array[i];
Search(array, array_size, i + 1, choosed_array, choosed_count+1, max_count);
}
}
int main()
{
int array[128];
int chosen_array[128];
//int Result[20][4];
int N, M;
//int j=0;
//while (1)
//{
printf("输入备选数字和所需数字个数,输入两个0则退出\n");
scanf("%d%d", &N, &M);
//if (0 == N && 0 == M)
// break;
if (N < M)
{
printf("出错!备选数字应该不少于所需数字\n");
//continue;
}
for (int i = 0; i < N; i++)
array[i] = i+1;
Search(array, N, 0, chosen_array, 0, M);
//}
_ConnectionPtr pMyConnect = NULL;
HRESULT hr = pMyConnect.CreateInstance(__uuidof( Connection ));
if(FAILED(hr))
return 3;
_bstr_t strConnect = "Provider=SQLOLEDB; Server=192.168.1.1;"
"Database=DB; uid=userid; pwd=password;";
//connecting to the database server now:
//try{pMyConnect->Open(strConnect,"","",NULL);}
//catch (_com_error &e)
//{
//::MessageBox(NULL,e.Description(),"警告",MB_OK│MB_ICONWARNING);
//}
for(j=0;j<1024;j++)
{
for(int i=0;i<7;i++)
{
printf("%d ", Result[j][i]);
//需要把输出改为字符串
}
putchar('\n');
}
_RecordsetPtr m_pRecordset;
//if(!FAILED(m_pRecordset.CreateInstance(__uuidof( Recordset ))))
//{
//m_pDoc->m_initialized=FALSE;
//return 4;
//}
try
{
m_pRecordset->Open(_variant_t("Table1"),
_variant_t((IDispatch *)pMyConnect,true), adOpenKeyset,
adLockOptimistic, adCmdTable);
}
catch (_com_error &e)
{
//::MessageBox(NULL,"无法打开mytable表。","提示",MB_OK│MB_ICONWARNING);
}
try
{
m_pRecordset->MoveFirst();
while(m_pRecordset->adoEOF==VARIANT_FALSE)
{
//Retrieve column's value:
(char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("name"))->Value) = CString Result;
//Do something what you want to do:
......
m_pRecordset->MoveNext();
}
}//try
catch (_com_error &e)
{
CString str=(char*)e.Description();
::MessageBox(NULL,str+"\n又出毛病了。","提示",
MB_OK │ MB_ICONWARNING);
}
return 0;
}
[解决办法]
用 insert 语句
[解决办法]
发现你可以使用 ado,但不会 sql 吧。
参考:http://www.w3school.com.cn/sql/index.asp