Use C # To operate the DBF DATABASE

Source: Internet
Author: User

Currently, most C # databases are connected to SQL server. However, I am not familiar with the connection and operations of some old database files.
This problem occurs today. You need to operate the DBF file database generated by FOXPRO.
I have been investigating CSDN for a long time, and most of the problems have not been well resolved to these operations.
After my experiment, you can use ODBC to connect to it.
The following code is used.
It is very simple, that is, pay attention to the format. It is very different from the open query statement.
Protected void Page_Load (object sender, EventArgs e)
{
System. Data. Odbc. OdbcConnection conn = new System. Data. Odbc. OdbcConnection ();
String table = @ "D: \ aaa \ code. dbf ";
String connStr = @ "Driver = {Microsoft Visual FoxPro Driver}; SourceType = DBF; SourceDB =" + table + "; Exclusive = No; NULL = NO; Collate = Machine; BACKGROUNDFETCH = NO; DELETED = NO ";

Conn. ConnectionString = connStr;
Conn. Open ();

OdbcCommand cmd = new OdbcCommand ();
Cmd. Connection = conn;
String SQL = "update" + table + "set other = '2', rate = 1.014 ";
Cmd. CommandText = SQL;
Cmd. CommandType = CommandType. Text;
Cmd. ExecuteNonQuery ();

SQL = @ "select * from" + table;
OdbcDataAdapter da = new OdbcDataAdapter (SQL, conn );
DataTable dt = new DataTable ();
Da. Fill (dt );
This. GridView1.DataSource = dt. DefaultView;
This. GridView1.DataBind ();

}
Note: the format of 1 connection string. The table name corresponding to 2 must have a path.

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.