Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Excel加载项循环从不终止_C#_Excel_Backgroundworker_Sqlconnection_Excel Addins - Fatal编程技术网

C#Excel加载项循环从不终止

C#Excel加载项循环从不终止,c#,excel,backgroundworker,sqlconnection,excel-addins,C#,Excel,Backgroundworker,Sqlconnection,Excel Addins,我编写了一个外接程序,从电子表格中读取值并填充数据库。当我从VisualStudio以调试模式执行外接程序时,它第一次就可以正常工作。当我尝试使用外接程序从另一个电子表格上载值时,如果不停止调试器,我的外接程序中的循环似乎永远不会结束。我使用活动工作表的使用范围来控制它们,由于某种原因,在循环计数器命中范围计数后,它将重新初始化为起始值,因此我的数据库由于违反了唯一键约束而抛出SqlException using (SqlConnection conn = DataConnection.GetC

我编写了一个外接程序,从电子表格中读取值并填充数据库。当我从VisualStudio以调试模式执行外接程序时,它第一次就可以正常工作。当我尝试使用外接程序从另一个电子表格上载值时,如果不停止调试器,我的外接程序中的循环似乎永远不会结束。我使用活动工作表的使用范围来控制它们,由于某种原因,在循环计数器命中范围计数后,它将重新初始化为起始值,因此我的数据库由于违反了唯一键约束而抛出SqlException

using (SqlConnection conn = DataConnection.GetConnection())
        {
            conn.Open();
            using (SqlCommand myCommand = new SqlCommand(commandText, conn))
            {
                SqlParameter termIDParam = myCommand.Parameters.Add("@tid", SqlDbType.Int);
                SqlParameter priceParam = myCommand.Parameters.Add("@price", SqlDbType.Decimal);
                SqlParameter ContractDateParam = myCommand.Parameters.Add("@date", SqlDbType.DateTime);
                SqlParameter pipelineIDParam = myCommand.Parameters.Add("@pid", SqlDbType.Int);
                string pipeline = activeSheet.Cells[1, 1].Value2.ToString();
                bool isNonDuplicate = true;
                int startRow = (pipeline.ToLower().Equals("henry hub")) ? 3 : 4;
                int startColumn = checkBox1.Checked ? 2 : 1;
                for (int i = startRow; i <= range.Rows.Count & isNonDuplicate; i++)
                {
                    for (int j = startColumn; j <= range.Columns.Count; j++)
                    {
                        if (range.Cells[i, j].Value2 != null && !range.Cells[i, j].Value2.ToString().Equals(" "))
                        {
                            string dateval = range.Cells[(startRow - 2), j].Value2.ToString();
                            double val = double.Parse(dateval);
                            DateTime contractDate = DateTime.FromOADate(val);
                            DateTime tempTerm = DateTime.FromOADate(activeSheet.Cells[i, 1].Value2);
                            string monthName = new DateTimeFormatInfo().GetAbbreviatedMonthName(tempTerm.Month);
                            string year = tempTerm.Year.ToString().Substring(2, 2);
                            string term = monthName + "-" + year;

                            double price = double.Parse(range.Cells[i,j].Value2.ToString());
                            SqlCommand subCommand = new SqlCommand();
                            subCommand.CommandText = "Select PipelineID from Pipeline where PipelineDescription='" + pipeline + "'";
                            subCommand.Connection = conn;
                            int pipeID = (int)subCommand.ExecuteScalar();
                            subCommand.CommandText = "Select TermID from Term where TermDescription = '" + term + "'";
                            int termID = (int)subCommand.ExecuteScalar();
                            termIDParam.Value = termID;
                            priceParam.Value = price;
                            ContractDateParam.Value = contractDate;
                            pipelineIDParam.Value = pipeID;
                            myCommand.Parameters["@tid"] = termIDParam;
                            myCommand.Parameters["@price"] = priceParam;
                            myCommand.Parameters["@date"] = ContractDateParam;
                            myCommand.Parameters["@pid"] = pipelineIDParam;
                            myCommand.ExecuteNonQuery();
                        }
                    }
                    double progress = (double)i / range.Rows.Count * 100;
                    bgw.ReportProgress(Convert.ToInt32(progress));
                }

            }
        }
使用(SqlConnection conn=DataConnection.GetConnection())
{
conn.Open();
使用(SqlCommand myCommand=newsqlcommand(commandText,conn))
{
SqlParameter termiparam=myCommand.Parameters.Add(“@tid”,SqlDbType.Int);
SqlParameter priceParam=myCommand.Parameters.Add(“@price”,SqlDbType.Decimal);
SqlParameter ContractDateParam=myCommand.Parameters.Add(“@date”,SqlDbType.DateTime);
SqlParameter pipelineIDParam=myCommand.Parameters.Add(“@pid”,SqlDbType.Int);
string pipeline=activeSheet.Cells[1,1].Value2.ToString();
bool isnondeplicate=true;
int startRow=(pipeline.ToLower().Equals(“henry hub”)?3:4;
int startColumn=复选框1。选中?2:1;

对于(int i=startRow;i)如何分配
range
?其中没有明显的指向循环重新启动的内容,但这可能有助于删除任何与DB相关的无关代码。
Excel.range=Excel.Selection为Excel.range;
,我将此范围传递给上述方法。