Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. // OleDbSample.cs
  2. using System;
  3. using System.Data;
  4. using System.Data.OleDb;
  5. using System.Xml.Serialization;
  6.  
  7. public class MainClass {
  8. public static void Main ()
  9. {
  10. // Set Access connection and select strings.
  11. // The path to BugTypes.MDB must be changed if you build
  12. // the sample from the command line:
  13. #if USINGPROJECTSYSTEM
  14. string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\BugTypes.MDB";
  15. #else
  16. string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BugTypes.MDB";
  17. #endif
  18. string strAccessSelect = "SELECT * FROM Categories";
  19.  
  20. // Create the dataset and add the Categories table to it:
  21. DataSet myDataSet = new DataSet();
  22. OleDbConnection myAccessConn = null;
  23. try
  24. {
  25. myAccessConn = new OleDbConnection(strAccessConn);
  26. }
  27. catch(Exception ex)
  28. {
  29. Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
  30. return;
  31. }
  32.  
  33. try
  34. {
  35.  
  36. OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect,myAccessConn);
  37. OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
  38.  
  39. myAccessConn.Open();
  40. myDataAdapter.Fill(myDataSet,"Categories");
  41.  
  42. }
  43. catch (Exception ex)
  44. {
  45. Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
  46. return;
  47. }
  48. finally
  49. {
  50. myAccessConn.Close();
  51. }
  52.  
  53. // A dataset can contain multiple tables, so let's get them
  54. // all into an array:
  55. DataTableCollection dta = myDataSet.Tables;
  56. foreach (DataTable dt in dta)
  57. {
  58. Console.WriteLine("Found data table {0}", dt.TableName);
  59. }
  60.  
  61. // The next two lines show two different ways you can get the
  62. // count of tables in a dataset:
  63. Console.WriteLine("{0} tables in data set", myDataSet.Tables.Count);
  64. Console.WriteLine("{0} tables in data set", dta.Count);
  65. // The next several lines show how to get information on
  66. // a specific table by name from the dataset:
  67. Console.WriteLine("{0} rows in Categories table", myDataSet.Tables["Categories"].Rows.Count);
  68. // The column info is automatically fetched from the database,
  69. // so we can read it here:
  70. Console.WriteLine("{0} columns in Categories table", myDataSet.Tables["Categories"].Columns.Count);
  71. DataColumnCollection drc = myDataSet.Tables["Categories"].Columns;
  72. int i = 0;
  73. foreach (DataColumn dc in drc)
  74. {
  75. // Print the column subscript, then the column's name
  76. // and its data type:
  77. Console.WriteLine("Column name[{0}] is {1}, of type {2}",i++ , dc.ColumnName, dc.DataType);
  78. }
  79. DataRowCollection dra = myDataSet.Tables["Categories"].Rows;
  80. foreach (DataRow dr in dra)
  81. {
  82. // Print the CategoryID as a subscript, then the CategoryName:
  83. Console.WriteLine("CategoryName[{0}] is {1}", dr[0], dr[1]);
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement