Job Recruitment Website - Immigration policy - Ask C++ how to connect to SQL server?
Ask C++ how to connect to SQL server?
//
# Contains? " stdafx.h "
# Contains? " iostream "
# Contains? "String"
# Contains? "vector"
//Step 1: Add ADO support.
# Import? “C:\Program? File \ Public? files \ System \ ado \ msado 15 . dll "? no_namespace? Rename ("EOF", "adoEOF")
Use? Namespace? std
int? _tmain(int? argc,? _TCHAR*? argv[])
{
Co-initialization (null); ? //initialize the COM environment?
_ConnectionPtr? PMyConnect(__uuidof); //Define the connection object and instantiate the object?
_RecordsetPtr? pRst(_ _ uuidof(Recordset)); //Define recordset objects and instantiate them?
Try it?
{
//Step 2: Create a data source connection
/* To open the database "SQLServer", you need a database based on your own PC? */?
pmy connect-& gt; open(" Provider = SQL oledb; ? Server =. ; Database = AIS2? uid = sa? pwd =;" ,"","",admode unknown); ?
}?
Catch? (_com_error? & ampe)?
{?
Cout & lt& lt "initiated? Failure! " & lt& ltendl?
cout & lt& lte . Description()& lt; & ltendl?
cout & lt& lte . HelpFile()& lt; & ltendl?
Return? 0; ?
}?
Cout & lt& lt "connection? Success! " & lt& ltendl?
//Step 3: Operate the database/table in the data source.
Try it?
{
pRst? =? pmy connect-& gt; Execute ("Select? *? From where? gendat ",NULL,ADC mdtext); //execute SQL:? Choice? *? From where? Gendat
If (! pRst-& gt; BOF)?
{
pRst-& gt; MoveFirst(); ?
}?
other
{
Cout < < "data? Is it? Empty! " & lt& ltendl?
Return? 0;
}?
Vector & lt_ bstr _ t>? column _ name
/* Store all the column names of the table and display the column names of the table */?
for(int? I = 0; ? I<? pRst-& gt; Field-> GetCount(); i++)?
{
cout & lt& ltpRst-& gt; Field-> GetItem(_ variant _ t((long)I))-& gt; Name & lt& lt "?" ;
column _ name . push _ back(pRst-& gt; Field-> GetItem(_ variant _ t((long)I))-& gt; Name); ?
}?
cout & lt& ltendl
/* Traverse the table and display the contents of each row in the table */?
And (! pRst-& gt; adoEOF)?
{
Vector & lt_ bstr _ t>* iterator? ITER = column _ name . begin();
for(ITER; iter! = column _ name . end(); iter++)
{?
if(pRst-& gt; GetCollect(*iter)。 vt? ! =VT_NULL)?
{
cout & lt& lt(_ bstr _ t)pRst-& gt; get collect(* ITER)& lt; & lt"?" ; ?
}?
other
{
cout & lt& lt" NULL " & lt& ltendl
}
}
pRst-& gt; MoveNext(); ?
cout & lt& ltendl
}?
}
catch(_com_error? & ampe)?
{?
cout & lt& lte . Description()& lt; & ltendl?
cout & lt& lte . HelpFile()& lt; & ltendl?
Return? 0;
}
//Step 4: Close the data source.
/* Close the database and release the pointer */
Try it?
{?
pRst-& gt; close(); ? //Close the recordset?
pmy connect-& gt; close(); //Close the database?
pRst。 Publish (); //Release the recordset object pointer?
pMyConnect。 Publish (); //Release the connection object pointer
}
catch(_com_error? & ampe)?
{?
cout & lt& lte . Description()& lt; & ltendl?
cout & lt& lte . HelpFile()& lt; & ltendl?
Return? 0; ?
}
coun initialize(); ? //publish COM environment
Return? 0;
}
- Previous article:Illegal immigrants from Britain and Germany
- Next article:What dangers do astronauts experience when riding a rocket to space?
- Related articles
- How much is the price of a beginner ukulele?
- Immigration service registration
- What are the requirements for emigrating to Japan? What are the application channels?
- Is Ukraine an EU country? Does Ukraine belong to the European Union?
- My husband is an American green card and is applying for naturalization and immigration for me. My mother-in-law has cancer and wants to take care of her. Please ask experienced people to help solve i
- A city suitable for French immigrants to live in
- Which is more suitable for China immigrants, Australian or Canadian?
- Can I use the Hurong Expressway to drive from Jiading, Shanghai to Yunyang, Chongqing?
- What is the policy of subsidizing the Three Gorges resettlement funds?
- What is the geographical location (longitude, latitude) of Cape Town? What are the characteristics of the climate?