我如何设法用.dbf填充listview

时间:2013-06-08 15:48:31

标签: c# wpf listview

我正在尝试通过c#wpf打开.dbf并将其加载到ListView,但我没有运气。

在我的ViewModel中:

public void DBF()
    {
        var databasePath = @"C:\Users\jesson\Desktop\FLCOLU_Building_Outline_Hints_308EL_section3_2180ER_QC.dbf";
        var connectionString = string.Format("DSN=dBase Files", databasePath);
        OdbcConnection connection = new OdbcConnection(connectionString);

        connection.Open();

        var _command = connection.CreateCommand();
        var query = string.Format(@"SELECT * FROM C:\Users\jesson\Desktop\FLCOLU_Building_Outline_Hints_308EL_section3_2180ER_QC.dbf");
        string commandText = query;


        var _dataAdapter = new OdbcDataAdapter(commandText, connection);

        DataSet _dataSet = new DataSet();
        DataTable _dataTable = new DataTable();

        _dataSet.Reset();
        _dataAdapter.Fill(_dataSet);

        _dataTable = _dataSet.Tables[0];
        var rows = _dataTable.Rows;

        string userName = rows[0].ItemArray[1] as string;
        string password = rows[0].ItemArray[2] as string;

        UserDataVar = new InputDataSingle
        {
            UserName = userName,
            Password = password
        };

        connection.Close();

    }

我做错了什么吗?还有其他想法吗?

1 个答案:

答案 0 :(得分:0)

你去吧。尝试以下一个。

string filename= //yourfilePath;
    string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ filename +";Extended Properties=dBASE IV;User ID=Admin;Password=;";
DataTable dt = new DataTable();   
 using (OleDbConnection con = new OleDbConnection(constr))
                {
                    var sql = "select * from " + filename ;
                    OleDbCommand cmd = new OleDbCommand(sql, con);
                    con.Open();
                    DataSet ds = new DataSet(); ;
                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    da.Fill(ds);
dt  =ds.Tables[0]
                }

这里是如何将数据传递给listview或dataGrid。;

mylistview.ItemsSource = dt.DefaultView;

<强>条件  你必须创建ViewView到ListView;