Ver Mensaje Individual
  #5 (permalink)  
Antiguo 20/07/2009, 00:22
Avatar de pampa_
pampa_
 
Fecha de Ingreso: mayo-2006
Mensajes: 334
Antigüedad: 18 años
Puntos: 1
Respuesta: c++ conectar a SQL

int leer(){

HRESULT hr = S_OK;
try
{
CoInitialize(NULL);
_bstr_t strCnn("Driver={SQL Server};Server=RUBEN\\WINCC;Database=Servidor;Uid= ruben;Pwd=ruben;");
_RecordsetPtr pRstAuthors = NULL;

// Call Create instance to instantiate the Record set
hr = pRstAuthors.CreateInstance(__uuidof(Recordset));

if(FAILED(hr)) //Comprueba que se pueda crear la instancia
{
printf("Failed creating record set instance\n");
return 0;
}

//Open the Record set for getting records from Author table
pRstAuthors->Open("SELECT [id],[ip],[puerto], [bit] FROM [Configuracion]",strCnn, adOpenStatic, adLockReadOnly,adCmdText);


//Declare a variable of type _bstr_t
_bstr_t valField1;
_bstr_t valField2;
bool valField3;
int valField4;

pRstAuthors->MoveFirst();

//Loop through the Record set
if (!pRstAuthors->EndOfFile)
{
printf(" id ip puerto bit \n\n");
while(!pRstAuthors->EndOfFile)
{
valField1 = pRstAuthors->Fields->GetItem("ip")->Value;
valField2 = pRstAuthors->Fields->GetItem("puerto")->Value;
valField3 = pRstAuthors->Fields->GetItem("bit")->Value.intVal;
valField4 = pRstAuthors->Fields->GetItem("id")->Value;
printf("%d - %s - %s - %d\n",valField4,(LPCSTR)valField1,(LPCSTR)valField 2,valField3);
pRstAuthors->MoveNext();
}
}

}
catch(_com_error & ce)
{
//printf("Error:%s\n",ce.Description);
//printf(ce.Description);
printf("Error:\n");
printf(ce.Description());
}
CoUninitialize();
return 0;
}

Este es el codigo de leer, si te fijas es distinto del codigo de escribir, en que en este otro tienes que hacer un string con la consulta, para despues ejecutarlo

int escribir(char x[], int y, bool b){
_bstr_t s;
_bstr_t sx=(_bstr_t) x;
_bstr_t sy=(_bstr_t) y;
_bstr_t sb=(char) b;
_ConnectionPtr pConn = NULL;
HRESULT hr = S_OK;
try
{
CoInitialize(NULL);
_bstr_t strCnn("Driver={SQL Server};Server=RUBEN\\WINCC;Database=Servidor;Uid= ruben;Pwd=ruben;");
_RecordsetPtr pRstAuthors = NULL;

// Call Create instance to instantiate the Record set
/*hr = pRstAuthors.CreateInstance(__uuidof(Recordset));*/
//Create the Connection pointer
hr = pConn.CreateInstance((__uuidof(Connection)));

if(FAILED(hr)) //Comprueba que se pueda crear la instancia
{
printf("Failed creating record set instance\n");
return 0;
}

//Open the Record set for getting records from Author table
//pRstAuthors->Open("SELECT [NUMERO_MOVI],[POSICION_ACTUAL] FROM [AUX_DATA]",strCnn, adOpenStatic, adLockReadOnly,adCmdText);
hr = pConn->Open(strCnn,"","",0);

/* Append the values to the Insert Statement */

s="Insert into Configuracion(ip,puerto,bit) Values ('";
s+=sx;
s+="','";
s+=sy;
s+="','";
s+=sb;
s+="')";

//_bstr_t strSQL("Insert into Configuracion(puerto,ip) Values(3,4)");
_bstr_t strSQL(s);
//Execute the insert statement
pConn->Execute(strSQL,NULL,adExecuteNoRecords);
printf("%s Data Added Successfully\n",(LPCSTR)strSQL);

//Close the database
pConn->Close();

}
catch(_com_error & ce)
{
//printf("Error:%s\n",ce.Description);
//printf(ce.Description);
printf("Error:\n");
printf(ce.Description());
}

CoUninitialize();
return 0;

}

Espero que te sirva, si tienes alguna duda pregunta