C#加载项Excel 2010 - 来自HRESULT的异常:0x800A03EC

时间:2017-02-07 16:17:05

标签: c# sql-server excel excel-addins

我的app有问题。几年前开发的。它是MS Excel 2010的一个加载项,它执行对SQL Server数据库的调用并在Worksheet中形成一个表。我必须修改它以使其与Azure数据库(连接字符串)一起使用,但事实证明问题是比那更深一点。当我尝试运行一个应用程序时,它给出了HRESULT:0x800A03EC异常,我无法理解为什么。 已安装MS Excel 2010。产生错误的代码:

public void insertData(string startDate, string endDate, string startTime, string endTime, string tickerSymbol,
        string RegId, string events, string userId, string PO, bool includePO, string markets)
    {

        try
        {

            //
            if (Globals.ExclAddIn.recordCount < 1000000)
            {
                string szSql = string.Format(@"EXEC sp_Get_Excel_TOQ_Report_Data_Mkt {0}, {1}, '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', {10}",
                startDate, endDate, startTime, endTime, tickerSymbol, RegId, events, userId, PO, markets, includeJPO);

                Workbook w = this.Application.ActiveWorkbook;
                Worksheet ws;
                if (w != null)
                {
                        ws = (Worksheet)w.Sheets.Add(missing,missing,missing,missing);
                        ws.Name = DateTime.Now.ToString("(RPT) yyyyMMdd HHmmss");
                    //}
                }
                else
                {
                    w = this.Application.Workbooks.Add(missing);
                    ws = (Worksheet) w.Worksheets.get_Item(1);
                }


                Range unfreezeRow = (Range)ws.Rows[10];
                unfreezeRow.Application.ActiveWindow.FreezePanes = false;

                ws.Rows.Delete();
                object misValue = System.Reflection.Missing.Value;
                Range range = ws.get_Range("A1:A9", misValue);
                MessageBox.Show(range.EntireRow.Address);

                //Check if the issue is in connection
                SqlConnection conn = new SqlConnection(selectedConnection);
                conn.Open();
                MessageBox.Show("Connection opened...");

                QueryTables xlQryTables = ws.QueryTables;
                IEnumerable<QueryTable> qt = xlQryTables.Cast<QueryTable>();
                foreach (QueryTable q in qt)
                {
                    q.Delete();
                }

                QueryTable xlQueryTable = (QueryTable)xlQryTables.Add(selectedConnection, range, szSql);
                xlQueryTable.RefreshStyle = XlCellInsertionMode.xlOverwriteCells;
                xlQueryTable.AfterRefresh += new RefreshEvents_AfterRefreshEventHandler(xlQueryTable_AfterRefresh);
                xlQueryTable.CommandType = XlCmdType.xlCmdSql;
                xlQueryTable.Refresh(true);

                assignReportHeader(ws);


            }
        }
        catch (Exception e)
        { 
            throw new Exception(e.Message);
        }
    }

我收到错误的地方是:

QueryTable xlQueryTable = (QueryTable)xlQryTables.Add(selectedConnection, range, szSql);

连接字符串有效...我也可以看到范围被选中。请帮忙。

1 个答案:

答案 0 :(得分:0)

我不能发表评论,因为我会以这种方式提出要求。 你能编写明确的连接字符串吗?我认为问题出在连接字符串中,也许你忘记了一些分号或类似的东西。