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:
- Using system;
- Using system. collections;
- Using system. Data;
- Using system. Data. oledb;
- Namespace yq
- {
- /// <Summary>
- /// Summary of saveasyq7.
- /// </Summary>
- Public class saveasdbf
- {
- Public saveasdbf ()
- {
- //
- // Todo: add the constructor logic here
- //
- }
- Private string _ errinfo = "";
- Public String errinfo
- {
- Get {return _ errinfo ;}
- }
- // Filename is actually a directory
- Public bool create (string filename)
- {
- Bool r = false;
- String outconnstring = string. Format ("provider = Microsoft. Jet. oledb.4.0; Data Source = {0}; extended properties = dbase iv;", filename );
- Oledbconnection outconn = new oledbconnection (outconnstring );
- Oledbcommand Dc = outconn. createcommand ();
- Try
- {
- Outconn. open ();
- DC. commandtype = commandtype. text;
- DC. commandtext = "create table Table1 (automatic number int, name char (5), salary double )";
- DC. executenonquery ();
- R = true;
- }
- Catch (exception C)
- {
- _ Errinfo = C. message;
- }
- Finally
- {
- DC. Dispose ();
- If (outconn. State = system. Data. connectionstate. open)
- Outconn. Close ();
- Outconn. Dispose ();
- }
- Return R;
- }
- // Filename is actually a directory
- // The recordinfo class contains three attributes: Automatic ID, name, and salary.
- Public bool save (string filename, recordinfo REC)
- {
- Bool r = false;
- String outconnstring = string. Format ("provider = Microsoft. Jet. oledb.4.0; Data Source = {0}; extended properties =/" dbase iv/";", filename );
- Oledbconnection outconn = new oledbconnection (outconnstring );
- Oledbcommand Dc = outconn. createcommand ();
- String TMP = "insert into Table1 (automatic number, name, salary) values ({0}, '{1}', {2 })";
- _ Errinfo = "";
- Try
- {
- Outconn. open ();
- DC. commandtext = string. Format (TMP, Rec. Automatic ID, Rec. Name, Rec. Salary );
- DC. executenonquery ();
- }
- Catch (exception ERR)
- {
- _ Errinfo = err. message;
- }
- Finally
- {
- If (outconn! = NULL)
- Outconn. Close ();
- DC. Dispose ();
- Outconn. Dispose ();
- }
- Return R;
- }
- }
- }
The usage is as follows:
- Folderbrowserdialog FBD = new folderbrowserdialog ();
- FBD. Description = "select the folder for saving the dbase iv file ";
- FBD. selectedpath = system. environment. currentdirectory;
- FBD. shownewfolderbutton = true;
- If (FBD. showdialog () = dialogresult. OK)
- {
- If (SY. Create (FBD. selectedpath ))
- {
- // Recdata is data. There is no detailed value process here, which is just a demonstration.
- Sy. Save (FBD. selectedpath, recdata );
- }
- }
This example does not include a complete error handling mechanism, but serves as an example. You can do it as needed.