Use C # to generate and save the DBF format

Source: Internet
Author: User
Tags dbase

When developing a project, the user requires that the data be stored in the dbf database. Therefore, we will simplify the code after the end, and hope to help you. The class code is as follows:

  1. Using system;
  2. Using system. collections;
  3. Using system. Data;
  4. Using system. Data. oledb;
  5. Namespace yq
  6. {
  7. /// <Summary>
  8. /// Summary of saveasyq7.
  9. /// </Summary>
  10. Public class saveasdbf
  11. {
  12. Public saveasdbf ()
  13. {
  14. //
  15. // Todo: add the constructor logic here
  16. //
  17. }
  18. Private string _ errinfo = "";
  19. Public String errinfo
  20. {
  21. Get {return _ errinfo ;}
  22. }
  23. // Filename is actually a directory
  24. Public bool create (string filename)
  25. {
  26. Bool r = false;
  27. String outconnstring = string. Format ("provider = Microsoft. Jet. oledb.4.0; Data Source = {0}; extended properties = dbase iv;", filename );
  28. Oledbconnection outconn = new oledbconnection (outconnstring );
  29. Oledbcommand Dc = outconn. createcommand ();
  30. Try
  31. {
  32. Outconn. open ();
  33. DC. commandtype = commandtype. text;
  34. DC. commandtext = "create table Table1 (automatic number int, name char (5), salary double )";
  35. DC. executenonquery ();
  36. R = true;
  37. }
  38. Catch (exception C)
  39. {
  40. _ Errinfo = C. message;
  41. }
  42. Finally
  43. {
  44. DC. Dispose ();
  45. If (outconn. State = system. Data. connectionstate. open)
  46. Outconn. Close ();
  47. Outconn. Dispose ();
  48. }
  49. Return R;
  50. }
  51. // Filename is actually a directory
  52. // The recordinfo class contains three attributes: Automatic ID, name, and salary.
  53. Public bool save (string filename, recordinfo REC)
  54. {
  55. Bool r = false;
  56. String outconnstring = string. Format ("provider = Microsoft. Jet. oledb.4.0; Data Source = {0}; extended properties =/" dbase iv/";", filename );
  57. Oledbconnection outconn = new oledbconnection (outconnstring );
  58. Oledbcommand Dc = outconn. createcommand ();
  59. String TMP = "insert into Table1 (automatic number, name, salary) values ({0}, '{1}', {2 })";
  60. _ Errinfo = "";
  61. Try
  62. {
  63. Outconn. open ();
  64. DC. commandtext = string. Format (TMP, Rec. Automatic ID, Rec. Name, Rec. Salary );
  65. DC. executenonquery ();
  66. }
  67. Catch (exception ERR)
  68. {
  69. _ Errinfo = err. message;
  70. }
  71. Finally
  72. {
  73. If (outconn! = NULL)
  74. Outconn. Close ();
  75. DC. Dispose ();
  76. Outconn. Dispose ();
  77. }
  78. Return R;
  79. }
  80. }
  81. }

The usage is as follows:

  1. Folderbrowserdialog FBD = new folderbrowserdialog ();
  2. FBD. Description = "select the folder for saving the dbase iv file ";
  3. FBD. selectedpath = system. environment. currentdirectory;
  4. FBD. shownewfolderbutton = true;
  5. If (FBD. showdialog () = dialogresult. OK)
  6. {
  7. If (SY. Create (FBD. selectedpath ))
  8. {
  9. // Recdata is data. There is no detailed value process here, which is just a demonstration.
  10. Sy. Save (FBD. selectedpath, recdata );
  11. }
  12. }

This example does not include a complete error handling mechanism, but serves as an example. You can do it as needed.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.