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

然后返回 rdConnection,它是连接字符串的 SQLDataReader,而不是您尝试导入的数据。rdConnection 包含 1 行并且您已经调用了 Read(),因此没有要读取的记录。

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

您绑定的 SQL 阅读器是使用的“连接字符串”,而不是您的销售数据。我会推荐以下内容:

  1. 在 getCode() 中返回由 cmd.ExecuteReader() 生成的新 SqlDataReader,而不是 rdConnection。
  2. 不要关闭到这个新的 SqlDataReader 的连接。ExcelWriter 需要能够读取数据读取器才能绑定数据。如果关闭连接,ExcelWriter 将无法正确绑定数据。
于 2013-07-26T16:02:21.613 回答