gpt4 book ai didi

C# 从 .DBF 文件读取到数据表

转载 作者:太空狗 更新时间:2023-10-29 21:09:10 25 4
gpt4 key购买 nike

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

我尝试过但失败的代码,继续获取

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();
}

最佳答案

这段代码对我有用!

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 文件读取到数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22361457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com