Read the DBF file on the Novice road

Source: Internet
Author: User

Understand the DBF file for the first time, because there is a need to export the data and insert it into the database. Start with Excel opened it, thought it is an Excel file, think of using npoi to achieve, is obviously as a newcomer too naïve, later on someone else's blog learned that read this file there are many ways, according to different difficult to use different methods, I used the simplest one because I didn't have too many constraints on the file I was in touch with.

 /// <summary>        ///read DBF file, this method is suitable for simple DBF file, that is similar to the Szse file/// </summary>        /// <param name= "path" >The path where the file to be read is located</param>        /// <param name= "Oledbname" >the name of the file to read</param>        /// <returns></returns>         Public StaticDataSet READDBF (stringPathstringoledbname) {                        stringstrconn =@"Provider=vfpoledb;data source="+ Path +"; Collating sequence=machine;"; using(OleDbConnection myconnection =NewOleDbConnection (strconn)) {OleDbDataAdapter ADPT=NewOleDbDataAdapter ("SELECT * from"+Oledbname, MyConnection); DataSet MySet=NewDataSet (); Adpt.                Fill (MySet);                Myconnection.close (); returnMySet; }        } 
View Code

The DBF file is read out after the existence of a dataset, the next is to insert the value into the database, due to my limited ability, the database to be inserted is a pre-written structure, and then inserted directly. The main function of this method is the concatenation of strings.

        /// <summary>        ///assign a value to the field to insert the database, and return the SQL statement/// </summary>        /// <param name= "Data" >extracting data from DBF that exists in a dataset</param>        /// <param name= "TableName" >indication of the database to be inserted</param>        /// <param name= "name" >defines how many fields are in the data table, in addition to the automatically generated GUIDs</param>        /// <returns>stitching up a good SQL statement</returns>         Public Static stringSetValue (DataSet data,stringTableName,params string[] name) {                       intCount = data. tables[0].            Columns.count; stringsql =NULL; intNumber =0; foreach(DataRow IteminchData. tables[0]. Rows) {//The primary key in the table is generated with a GUID                stringGUID =Guid.NewGuid ().                ToString (); stringValue ="'"+guid+"'"+",";  for(inti =0; I < count; i++)                {                    if(i = = count-1) {Name[i]="'"+ Item[i]. ToString () +"'"; number++; }                    Else{Name[i]="'"+ Item[i]. ToString () +"'"+","; number++; } Value+=Name[i]; }// for                stringNewsql ="Insert into"+" "+ TableName +" "+"VALUES ("+ Value +")"; SQL+=Newsql; }//foreach            returnSQL; }             
View Code

And finally, the simplest interpolation to the database.

 /// <summary>        ///execute the SQL statement to interpolate the values in the database/// </summary>        /// <param name= "SQL" >SQL statements</param>        /// <returns></returns>         Public Static intYexecutenonquer (stringSQL) {            stringConnectionString = configurationmanager.connectionstrings["Con"].            ConnectionString; using(SqlConnection conn =NewSqlConnection (ConnectionString)) {Conn.                Open (); using(SqlCommand cmd =NewSqlCommand (SQL, conn)) {                    returncmd.                ExecuteNonQuery (); }            }        } 
View Code

Such a simple operation to read the DBF file and insert it into the database is complete.

Read the DBF file on the Novice road

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.