|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
使用cdonts,可以发送、查看邮件,实现webmail的功能。结合wsh,可以实现对nt主机的管理,如nt用户管理、iis虚拟主机设置、exchange邮箱设置等等,就像管理本地机一样方便。原码下载地点:
http://www.codeproject.com/dotnet/ADONET_datareader/ADONET_datareader.zip
Introduction
ADO.NETisthe.NETenhancedversionofADOthatweallknowandlove.ADO.NETaimstoaddresssomeofthedeficienciesoftraditionalADOsuchaslackoftypesafety,lackofanobjectorientedmodel,andinefficienciesinreturningrowsofdata.
Thisfirstarticlewilldemonstratethemostcommontaskwhenaccessingadatabase:queryingfordata,andtraversingthatdatafromstarttofinishinordertodisplaythecontents(orsubsetthereof)ofatable.
TheADODataReaderclass
ADO.NETreplacestheconceptofdatarowswiththeDataSetobject.Thisessentiallyprovidesuswithfullaccesstoagivendatabase,includingallrows,tablesandrelationshipsinanobjectorientedandtype-safemanner.Itis,however,totaloverkillforthesimplequeryandtraversalsthataremostoftenperformedondatabases.
Forthissimplecase.NETprovidesuswiththeADODataReaderclassthatisessentiallyatypesafereadonly,forwardonlyrowset.Allweneedtodoisopenaconnectiontoadatabase,sendanSQLcommand,thentraversethroughtheresultantADODataReaderusingtheReadcommandandprocesstheresults.
TheeasiestwaytoillustratethisistoshowyousomeC#code.ThiscodeopensanAccessdatabase,readsalltheinformationfromatable,thenpopulatesaListViewcontrolwiththedatainside.
Afewnotesonthecode:
StatusTextisaRichTextBoxcontroldeclaredasSystem.WinForms.RichTextBoxStatusText;
StatusText=newSystem.WinForms.RichTextBox();
listViewisalistviewcontroldeclaredasSystem.WinForms.ListViewlistView;
listView=newSystem.WinForms.ListView();
Thelistviewhasbeenplacedinreportmodewithgridlinesusing
listView.View=System.WinForms.View.Report;
listView.GridLines=true;
TheCode
ADOConnectionadoConnection=newADOConnection();
//TODO:Changethelocationofthisfile
//The@meansthatthestringwillbetreatedas-is,andthe
//swillnotbeinterpretedastheescapecharacter.
//Thissavestyping"D:ADONETdemo..."
StringstrDatabaseFile=@"D:ADONETdemoAuthors.mdb";
try
{
//Openaconnectiontothedatabase
adoConnection.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;"+
"DataSource="+strDatabaseFile+";"+
"PersistSecurityInfo=False;";
adoConnection.Open();
//CreateanSQLcommand,setitsconnectionanditscommandtext
ADOCommandcommand=newADOCommand();
command.ActiveConnection=adoConnection;
command.CommandText="SELECT*FROMAuthor";
//Executethecommand,andreturntherowsinthedatareaderobject
ADODataReaderdataReader;
command.Execute(outdataReader);
//Getthenumberoffields(columns)inthetable
intnFields=dataReader.FieldCount;
//Setupthecolumnsinthelistviewusingthefieldsinthetable
listView.Clear();
for(inti=0;i<nFields;i++)
{
listView.InsertColumn(i,dataReader.GetName(i),100,HorizontalAlignment.Left);
}
//Filltherowsinthelistviewusingthedataintherows
intnRow=0;
while(dataReader.Read())
{
//Createanarrayofsubitemsforquickinsertion
//Thesubitemswillbeallfieldsintherowexceptfor
//thefirstfield
String[]subitems=newString[nFields-1];
for(inti=1;i<nFields;i++)
{
subitems[i-1]=dataReader.GetValue(i).ToString();
}
//Insertanewitemintothelistview,andaddthesubitems
//atthesametime.Theitemwillbethefirstfieldinthe
//row
listView.InsertItem(nRow,dataReader.GetValue(0).ToString(),
-1,subitems);
//nextrow.
nRow++;
}
dataReader.Close();
//Setthestatustext
StatusText.Text=nFields.ToString()+"columns,"+
nRow.ToString()+"rowsread";
}
catch
{
//Ifanerroroccuredalerttheuser
StatusText.Text="Erroroccurred";
}
finally
{
//Closetheconnectionifnecessary
if(adoConnection.State==DBObjectState.Open)
adoConnection.Close();
}
Thatsallthereistoit.Wehaveclosedthedatabaseconnectionbutsinceweareusingmanagedcodethereisnoneed(orway)todeletetheobjectsandmemoryweallocated.
AboutChrisMaunder
ChrisisthefounderandsiteadministratorforCodeProject.com.HesbeenprogramminginC/C++for10yearsandVisualC++/MFCfor4years.Hisbackgroundincludespureandappliedmathematics,engineeringandphysics,andheiscurrentlybasedinCanberra,Australia.
对于中小型web应用来说,php有很强的竞争力,linux+apache+mysql+php(lamp)的组合几乎可以胜任绝大多数网站的解决方案,对于大型应用来讲,对于系统架构要求更高,需要有成熟的框架支持,jsp的struts是个不错的框架,国内介绍它的资料也非常多,应用逐渐广泛起来。asp就不用说了, |
|