从csv填充数据表

时间:2016-01-03 00:59:55

标签: asp.net csv

我正在尝试从csv文件中填充dataTable。但是当我将dataTable绑定到GridView时,gridview为空。知道我错了吗?这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{

    DataTable dt = new DataTable();
    bool isFirstRowHeader = true;
    string path = System.IO.Path.GetFullPath(@"D:\WebApplication10\WebApplication10\Students.csv");
    string header = isFirstRowHeader ? "Yes" : "No";

    string pathOnly = Path.GetDirectoryName(path);
    string fileName = Path.GetFileName(path);

    string sql = @"SELECT * FROM [" + fileName + "]";

    using (OleDbConnection connection = new OleDbConnection(
              @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
              ";Extended Properties=\"Text;HDR=" + header + "\""))
    using (OleDbCommand command = new OleDbCommand(sql, connection))
    using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
    {

        dt.Locale = CultureInfo.CurrentCulture;
        adapter.Fill(dt);

        //if (dt.Rows.Count > 0)
        //{
        //    foreach (DataRow row in dt.Rows)
        //    {
        //        string i = row[0].ToString();
        //        string i1 = row[1].ToString();
        //        string i2 = row[2].ToString();
        //        string i3 = row[3].ToString();
        //    }
        //}
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
}

0 个答案:

没有答案