C#从dbf文件读入一个DataTable [英] C# Read from .DBF files into a datatable

查看:365
本文介绍了C#从dbf文件读入一个DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用C#连接到的.dbf 文件中的Visual Studio和填充数据表。有任何想法吗?在Visual福克斯我目前可以查看表Pro的9.0



代码我曾尝试和失败,不断收到




外部表不是预期的格式。




 私人的OleDbConnection康涅狄格州; 
私人OleDbCommand的CMD;
私人OleDbDataReader博士;
私人字符串sqlStr =;
私人的DataSet myDataSet;
私人OleDbDataAdapter的myAdapter;


无效test2的()
{
康恩=新的OleDbConnection(@供应商= Microsoft.Jet.OLEDB.4.0;数据源= C:\\\ \\Users\\PC1\Documents\\Visual的FoxPro Projects\\;扩展属性= DBASE IV;);
conn.Open();
sqlStr =选择Clients.dbf *;
//做一个DataSet对象
myDataSet =新的DataSet();
//使用OleDbDataAdapter的执行查询
myAdapter =新OleDbDataAdapter的(sqlStr,康涅狄格州);
//生成Update和Delete SQL语句
OleDbCommandBuilder myBuilder =新的OleDbCommandBuilder(myAdapter);

//装满表'藏书'
myAdapter.Fill(myDataSetsomename)数据集;
//获取FileStream对象
的FileStream myFs =新的FileStream
(myXmlData.xml,FileMode.OpenOrCreate,FileAccess.Write);
//使用DataSet对象的WriteXml方法从DataSet中
// myDs.WriteXml(myFs)编写的XML文件;
myFs.Close();
conn.Close();
}


解决方案

此代码为我工作!

 公开数据表GetYourData()
{
的DataTable YourResultSet =新的DataTable();

OleDbConnection的yourConnectionHandler =新的OleDbConnection(
@供应商= VFPOLEDB.1;数据源= C:\Users\PC1\Documents\Visual FoxPro中Projects\);

//如果包括完整的DBC(数据库容器)的参考,只是钉上
// OleDbConnection的yourConnectionHandler =新的OleDbConnection(
//供应商= VFPOLEDB.1;数据源= C:\\SomePath\\NameOfYour.dbc;);


//打开连接,如果成功地打开,可以尝试进行查询
yourConnectionHandler.Open();

如果(yourConnectionHandler.State == ConnectionState.Open)
{
字符串的MySQL =来自客户的选择*; // DBF表名

的OleDbCommand更改为MyQuery =新的OleDbCommand(MySQL的,yourConnectionHandler);
OleDbDataAdapter的DA =新OleDbDataAdapter的(更改为MyQuery);

DA.Fill(YourResultSet);

yourConnectionHandler.Close();
}

返回YourResultSet;
}


I need to connect to a .dbf file in visual Studio using C# and populate a data table. Any ideas? I can currently view the tables in Visual Fox Pro 9.0

Code I have tried and failed, keep getting

External table is not in the expected format.

private OleDbConnection conn;
private OleDbCommand cmd;
private OleDbDataReader dr;
private string sqlStr = "";
private DataSet myDataSet;
private OleDbDataAdapter myAdapter;


void test2()
{
    conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\PC1\Documents\\Visual FoxPro Projects\\;Extended Properties=DBASE IV;");
    conn.Open();
    sqlStr = "Select * from Clients.dbf";
    //Make a DataSet object
    myDataSet = new DataSet();
    //Using the OleDbDataAdapter execute the query
    myAdapter = new OleDbDataAdapter(sqlStr, conn);
    //Build the Update and Delete SQL Statements
    OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdapter);         

    //Fill the DataSet with the Table 'bookstock'
    myAdapter.Fill(myDataSet, "somename");
    // Get  a FileStream object
    FileStream myFs = new FileStream
          ("myXmlData.xml", FileMode.OpenOrCreate, FileAccess.Write);
    // Use the WriteXml method of DataSet object to write XML file from the   DataSet
    //  myDs.WriteXml(myFs);
    myFs.Close();
    conn.Close();
}

解决方案

This code worked for me!

public DataTable GetYourData()
    {
        DataTable YourResultSet = new DataTable();

        OleDbConnection yourConnectionHandler = new OleDbConnection(
            @"Provider=VFPOLEDB.1;Data Source=C:\Users\PC1\Documents\Visual FoxPro Projects\");

        // if including the full dbc (database container) reference, just tack that on
        //      OleDbConnection yourConnectionHandler = new OleDbConnection(
        //          "Provider=VFPOLEDB.1;Data Source=C:\\SomePath\\NameOfYour.dbc;" );


        // Open the connection, and if open successfully, you can try to query it
        yourConnectionHandler.Open();

        if (yourConnectionHandler.State == ConnectionState.Open)
        {
            string mySQL = "select * from CLIENTS";  // dbf table name

            OleDbCommand MyQuery = new OleDbCommand(mySQL, yourConnectionHandler);
            OleDbDataAdapter DA = new OleDbDataAdapter(MyQuery);

            DA.Fill(YourResultSet);

            yourConnectionHandler.Close();
        }

        return YourResultSet;
    }

这篇关于C#从dbf文件读入一个DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆