Использование ADO для доступа MSSQL DB

Сначала нужно подключится к базе данных, затем послать запрос на выборку данных из базы данных, после получения данных из базы следует закрыть соединение.

using namespace std;
#import "C:\Program Files\Common Files\System\ADO\msado15.dll"
no_namespace rename("EOF", "EndOfFile")
#include <string>

LV_COLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM ;
lvc.fmt = LVCFMT_LEFT;
lvc.cx = 95;
lvc.pszText = "Номер";
m_list.InsertColumn(0, &lvc);
lvc.pszText = "Имя";
m_list.InsertColumn(1, &lvc);
lvc.pszText = "Фамилия";
m_list.InsertColumn(2, &lvc);
CoInitializeEx(NULL, COINIT_MULTITHREADED);
_ConnectionPtr Conn("ADODB.Connection");
_bstr_t strCon("Provider=SQLOLEDB.1; Data Source=NONAME-15FCB4E8\\MyServerSQL; Initial File Name=D:\\MyBDMSSQL\\MyMSSQL.mdf;; Integrated Security=SSPI;");
Conn.CreateInstance(__uuidof(Connection));
Conn->Open(strCon, "", "", adConnectUnspecified);

_CommandPtr spCommand;
spCommand.CreateInstance(__uuidof (Command));
spCommand->CommandType = adCmdText;
spCommand->ActiveConnection = Conn;

string clean_cmd = "SELECT * FROM Table1";
spCommand->CommandText = clean_cmd.c_str();
//spCommand->Execute(NULL, NULL, 0);

variant_t v;
_RecordsetPtr rs("ADODB.Recordset");
rs.CreateInstance(__uuidof (Recordset));
//rs= spCommand->Execute(NULL, NULL, 0);

rs->CursorLocation = adUseClient;
rs->Open ((IDispatch *) spCommand, vtMissing, adOpenForwardOnly, adLockReadOnly, adCmdUnknown);
CString strContent;

while (!rs->EndOfFile)
{
strContent = (char *) (_bstr_t) rs->Fields->GetItem ("kod")->Value;
int num = atoi(strContent.GetBuffer(256));
m_list.InsertItem( num, strContent);
strContent = (char *) (_bstr_t) rs->Fields->GetItem
("pname")->Value;
m_list.SetItemText(num-1, 1, strContent);
strContent = (char *) (_bstr_t) rs->Fields->GetItem ("psoname")->Value;
m_list.SetItemText(num-1, 2, strContent);
rs->MoveNext ();
}
rs->Close();
Conn->Close();

//---------------------------------
//---------------------------------
//---------------------------------
//---------------------------------
//Add Row to Table
_ConnectionPtr Conn("ADODB.Connection");

_bstr_t strCon("Provider=SQLOLEDB.1; Data Source=NONAME-15FCB4E8\\MyServerSQL; Initial File Name=D:\\MyBDMSSQL\\MyMSSQL.mdf;;
Integrated Security=SSPI;");
Conn.CreateInstance(__uuidof(Connection));
Conn->Open(strCon, "", "", adConnectUnspecified);

_CommandPtr spCommand;
spCommand.CreateInstance(__uuidof (Command));
spCommand->CommandType = adCmdText;
spCommand->ActiveConnection = Conn;

string clean_cmd = "SELECT * FROM Table1";
spCommand->CommandText = clean_cmd.c_str();
spCommand->Execute(NULL, NULL, 0);

variant_t v;
_RecordsetPtr rs("ADODB.Recordset");
rs.CreateInstance(__uuidof (Recordset));
//rs= spCommand->Execute(NULL, NULL, 0);

rs->CursorLocation = adUseClient;
rs->Open ((IDispatch *) spCommand, vtMissing, adOpenForwardOnly, adLockReadOnly, adCmdUnknown);
//rs->Requery(adCmdUnknown);
rs->MoveLast();

//получаем номер последней записи что бы добавить последнюю к таблице
CString strContent = (char *) (_bstr_t) rs->Fields->GetItem ("kod")->Value;
int num = atoi(strContent.GetBuffer(256));

//rs->Close();

//получаем из полей ввода имя и фамилию
char szTextName[256];
m_pname.GetWindowText(szTextName, 256);
char szTextSoName[256];
m_psoname.GetWindowText(szTextSoName, 256);

CString str;
str.Format("INSERT INTO Table1 (kod, pname, psoname) VALUES( %d, '%s', '%s')", num+1,szTextName,szTextSoName );

clean_cmd = str;
spCommand->CommandText = clean_cmd.c_str();
spCommand->Execute(NULL, NULL, 0);

rs->Close();
Conn->Close();


//using stored procedures

using namespace std;
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include <string&qt
//------------------------------
//------------------------------
//------------------------------
//------------------------------
_ConnectionPtr Conn("ADODB.Connection");

_bstr_t strCon("Provider=SQLOLEDB.1; Data Source=NONAME-15FCB4E8\\MyServerSQL; Initial File Name=D:\\MyBDMSSQL\\MyMSSQL.mdf;; Integrated Security=SSPI;");
Conn.CreateInstance(__uuidof(Connection));
Conn->Open(strCon, "", "", adConnectUnspecified);

_CommandPtr spCommand;
spCommand.CreateInstance(__uuidof (Command));
spCommand->CommandType = adCmdText;
spCommand->ActiveConnection = Conn;
spCommand->CommandText = "myproc";

_ParameterPtr pParam1,pParam0;
pParam0 = spCommand->CreateParameter(
_bstr_t ("ret"), //имя параметра можна задать любое
adInteger, //тип хранимый в параметре
adParamReturnValue, //тип входной выходной
sizeof(int)); //размер параметра
//0); //сам параметр

pParam1 = spCommand->CreateParameter(
_bstr_t ("kod"), //имя параметра можна задать любое
adInteger, //тип хранимый в параметре
adParamInput, //тип входной выходной
sizeof(int), //размер параметра
3); //сам параметр

spCommand->Parameters->Append( pParam0);
spCommand->Parameters->Append( pParam1);

//_RecordsetPtr rs("ADODB.Recordset");
_RecordsetPtr rs=NULL;
rs.CreateInstance(__uuidof (Recordset));

spCommand->Execute(NULL, NULL, adCmdStoredProc);
rs->CursorLocation = adUseClient;
rs->Open ((IDispatch *) spCommand, vtMissing, adOpenForwardOnly,adLockReadOnly, -1);

pParam0 = spCommand->Parameters->Item["ret"];
int ret = (int) pParam0->Value.iVal;

CString szret;
szret.Format("%d",ret );
AfxMessageBox(szret);

CString strContent;
while (!rs->GetEndOfFile ())
{
variant_t num = rs->Fields->GetItem ("kod")->Value;
strContent.Format("%d -", num.iVal);
strContent += (char *) (_bstr_t) rs->Fields->GetItem ("pname")->Value;
strContent += " - " ;
strContent += (char *) (_bstr_t) rs->Fields->GetItem ("psoname")->Value;
AfxMessageBox(strContent);
rs->MoveNext ();
}
rs->Close();