0

次のコードを使用して、Oledb を使用して Excel データを DataGrid に表示しています。しかし、私はエラーが発生していますFill: SelectCommand.Connection property has not been initialized.

誰が私がどこで間違ったのか教えてもらえますか?

ありがとう

コード:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\testing.xlsx;Extended Properties='Excel 12.0 Xml;HDR=Yes;IMEX=1;'");
        OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]");
        if (conn.State == System.Data.ConnectionState.Open)
        {
            conn.Close();
        }
        conn.Open();
        OleDbDataAdapter adap = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        this.GridView1.DataSource = ds.Tables[0].DefaultView;
        conn.Close();
    }
}
4

1 に答える 1

0

接続をコマンド オブジェクトに割り当てるのを怠っています。

これを下に移動 conn.Open();

OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", conn);
于 2013-05-19T05:36:57.210 に答える