程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> ADO.NET:使用ADO.NET連接文本文件

ADO.NET:使用ADO.NET連接文本文件

編輯:ASP技巧

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.

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved