ADO.NET:使用ADO.NET连接文本文件
2024-07-10 13:07:48
供稿:网友
try
{
// create a new adoconnection to the text file through odbc and an existing data source
adoconnection conn = new adoconnection("provider=msdasql;dsn=registrations;");
// create a dataset command that selects all the records from the registration.txt table (which in this case is a file)
adodatasetcommand adocmd = new adodatasetcommand("select * from registrations.txt", conn);
// fill the dataset with the registration.txt table
adocmd.filldataset(dataset1, "registrations.txt");
datatable contacttable = dataset1.tables[0];
int count = 0;
// loop through each row of the table and fill 15 rows of the listview
foreach (datarow dr in contacttable.rows)
{
listview3.listitems[count].text = dr["lastname"].tostring();
listview3.listitems[count].setsubitem(0, dr["firstname"].tostring());
listview3.listitems[count].setsubitem(1, dr["company"].tostring());
listview3.listitems[count].setsubitem(2, dr["address"].tostring());
count++;
if (count > 15)
{
break;
}
}
}
catch(adoexception ae)
{
console.writeline(ae.message.tostring());
}
that's all there is to it. this should also give you an idea of how to connect to databases through odbc such as oracle, informix, sybase, or interbase. all you need to do is set up the appropriate data source through the administration tools and use the code above to access your tables.