0

コンソール アプリケーションのデバッグ中に、この OfficeWriter エラーが発生しました。メソッドを使用して、マスターデータベースからコーディングで使用されるデータベースの構成の詳細を取得したところ、このエラーが発生しました。

行 1 での GetColumnNumber のバインディング エラー

ここに添付されているのは、私の作品の部分的なコーディングです。誰がエラーが何であるかを説明できますか?

    SqlDataReader rdSource = getSource();
    SqlDataReader rdDestination = getDestination();
    SqlDataReader rdCode = getCode();

    while (rdSource.Read() && rdDestination.Read())
    {
        string src = rdSource["Value"].ToString();
        string dest = rdDest["Value"].ToString();

        ExcelTemplate XLT = new ExcelTemplate();
        XLT.Open(src);
        DataBindingProperties dataProps = XLT.CreateBindingProperties();
        XLT.BindData(rdCode, "Code", dataProps);
        XLT.Process();
        XLT.Save(dest);
    }

    //rdCode method
    SqlDataReader rdConnection = getConnection(); //method for getting connection from master
    while (rdConnection.Read())
    {
        string con = rdConnection["Value"].ToString();
        SqlConnection sqlCon = new SqlConnection(con);

        string SQL = "SELECT * FROM Sales.Currency";
        sqlCon.Open();
        SqlCommand cmd = new SqlCommand(SQL, sqlCon);
        cmd.ExecuteReader();
        sqlCon.Close();
    }
    return rdConnection;

    //getConnection method
    string strCon = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
    SqlConnection sqlCon = new SqlConnection(strCon);
    string cSQL = "SELECT Value FROM dbo.COMMON_CONFIG WHERE Value = 'Data Source=localhost;Initial Catalog=Test;Integrated Security=True'";
    SqlCommand cmd = new SqlCommand(cSQL, sqlCon);
    sqlCon.Open();
    return new SqlCommand(cSQL, sqlCon).ExecuteReader(CommandBehavior.ConnectionString);

    //getSource & getDestination methods
    string strCon = ConfigurationManager.ConnectionStrings["Master"].ConnectionString;
    SqlConnection sqlCon = new SqlConnection(strCon);
    string srcPath = @"FILE PATH NAME"; //change to destPath for getDestination
    string sSQL = "SELECT Value FROM dbo.COMMON_CONFIG WHERE Value = '" + srcPath + "'";
    SqlCommand cmd = new SqlCommand(cSQL, sqlCon);
    sqlCon.Open();
    return new SqlCommand(cSQL, sqlCon).ExecuteReader(CommandBehavior.ConnectionString);
4

1 に答える 1

0

エラーは、データをテンプレートにバインドできない場合に ExcelWriter によってスローされる一般的なメッセージです。

これは getCode() メソッドが原因である可能性があると思います。getCode() では、SQLDataReader を使用してデータベースの接続文字列を取得します。

    SqlDataReader rdConnection = getConnection(); //method for getting connection from master

次に、そのデータベースに対して SQL クエリを実行しますが、SQL クエリを実行してデータを返す SqlDataReader のハンドルを実際には取得しません。

    SqlCommand cmd = new SqlCommand(SQL, sqlCon);
    cmd.ExecuteReader(); //Note: This returns the SqlDataReader that contains the data

次に、インポートしようとしているデータではなく、接続文字列の SQLDataReader である rdConnection を返します。rdConnection には 1 行が含まれており、既に Read() を呼び出しているため、読み取るレコードは残っていません。

 SqlDataReader rdCode = getCode();
 ...
 XLT.BindData(rdCode, "Code", dataProps);

バインドしている SQL リーダーは、販売データではなく、使用されている「接続文字列」です。次のことをお勧めします。

  1. rdConnection ではなく、getCode() で cmd.ExecuteReader() によって生成された新しい SqlDataReader を返します。
  2. この新しい SqlDataReader への接続を閉じないでください。ExcelWriter は、データをバインドするためにデータ リーダーを読み取れる必要があります。接続を閉じると、ExcelWriter はデータを正しくバインドできなくなります。
于 2013-07-26T16:02:21.613 に答える