Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.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# 使用数据适配器更新在.NET中使用ACE超时的Excel_C#_Excel_Ms Jet Ace_Oledbdataadapter - Fatal编程技术网

C# 使用数据适配器更新在.NET中使用ACE超时的Excel

C# 使用数据适配器更新在.NET中使用ACE超时的Excel,c#,excel,ms-jet-ace,oledbdataadapter,C#,Excel,Ms Jet Ace,Oledbdataadapter,我在将数据集中的数据写入excel电子表格时遇到超时问题。这是我当前的连接字符串: <add key="ExcelConnectionStringHeader" value="Provider=Microsoft.ACE.OLEDB.12.0;Connect Timeout=30;Extended Properties=&quot;Excel 12.0;HDR=YES&quot;;Data Source="/> 我省略了实际构建insert命令的内容和参数的循环。相

我在将数据集中的数据写入excel电子表格时遇到超时问题。这是我当前的连接字符串:

<add key="ExcelConnectionStringHeader" value="Provider=Microsoft.ACE.OLEDB.12.0;Connect Timeout=30;Extended Properties=&quot;Excel 12.0;HDR=YES&quot;;Data Source="/>
我省略了实际构建insert命令的内容和参数的循环。相信我,一切都很好。2200多条记录可以正常工作

在我添加计时器之后,我发现问题是超时。处理2221条记录需要14.95秒,并且在电子表格中显示得很好。当处理2260条记录时,需要15.21秒,但没有显示任何内容。没有任何错误。我已经检查了更新后所有行的行状态,它们都没有显示失败

一句话,由于ConnectionTimeout属性是只读的,并且提供程序似乎不支持连接字符串中的超时,因此该怎么办

谢谢

以下是我的想法:

  • 您是否真的将其用作应用程序设置而不是连接字符串?如果您将其用作连接字符串,请检查以下内容:
  • 数据源缺少它的值,只是指出了它,因为您遗漏了代码的这一部分
  • 问题的根源可能是会话池超时设置:
  • 您应该在连接、适配器和命令创建行周围放置using语句,不这样做会创建一种形式的蒸汽锁并强制超时
  • 您确定这是超时而不是“电子表格已满”错误吗?这是我在~1:50后完成的代码,如果将其增加到4000000行,则会得到“电子表格已满”。在~4:50后出错:

    static void Main(string[] args)
    {
        var timer = new System.Diagnostics.Stopwatch();
    
        try
        {
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=\"Excel 12.0;HDR=YES;\";Data Source=Book1.xlsx";
    
            using (OleDbConnection oleDbConnection = new OleDbConnection(connectionString))
            {
                oleDbConnection.Open();
    
                string szHeaderSelect = "SELECT [A1] FROM  from [Sheet1$]";
                using (OleDbDataAdapter da = new OleDbDataAdapter(szHeaderSelect, oleDbConnection))
                {
                    using (da.InsertCommand = new OleDbCommand("INSERT INTO [Sheet1$] ( [A1] ) VALUES (?)",
                        oleDbConnection))
                    {
                        da.InsertCommand.Parameters.Add("A1", OleDbType.Integer, 20, "[A1]");
    
                        List<int> testData = new List<int>();
                        for (int i = 1; i < 400000; i++)
                        {
                            testData.Add(i);
                        }
    
                        DataSet dsTest = new DataSet();
                        dsTest.Tables.Add("[Sheet1$]");
                        dsTest.Tables[0].Columns.Add("[A1]");
    
                        foreach (int number in testData)
                        {
                            DataRow drNew = dsTest.Tables[0].NewRow();
                            drNew["[A1]"] = number;
    
                            dsTest.Tables[0].Rows.Add(drNew);
                        }
    
                        timer.Start();
                        var recs = da.Update(dsTest, "[Sheet1$]");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.Out.WriteLine(ex.Message);
        }
        finally
        {
            timer.Stop();
    
            Console.WriteLine(timer.Elapsed);
        }
    
        // Don't close before I get to read the results
        Console.WriteLine();
        Console.WriteLine("Press Enter to quit.");
        Console.ReadLine();
    }
    
    static void Main(字符串[]args)
    {
    var timer=new System.Diagnostics.Stopwatch();
    尝试
    {
    字符串连接string=“Provider=Microsoft.ACE.OLEDB.12.0;扩展属性=\”Excel 12.0;HDR=YES;\“数据源=Book1.xlsx”;
    使用(OLEDB连接OLEDB连接=新OLEDB连接(连接字符串))
    {
    oleDbConnection.Open();
    string szHeaderSelect=“从[Sheet1$]中选择[A1]”;
    使用(OleDbDataAdapter da=新的OleDbDataAdapter(szHeaderSelect,OLEDB连接))
    {
    使用(da.InsertCommand=new-OleDbCommand(“插入到[Sheet1$]([A1])值(?)”,
    OLEDB连接)
    {
    da.InsertCommand.Parameters.Add(“A1”,OleDbType.Integer,20,“[A1]”;
    List testData=new List();
    对于(int i=1;i<400000;i++)
    {
    testData.Add(i);
    }
    数据集dsTest=新数据集();
    添加(“[Sheet1$]”);
    dsTest.Tables[0].Columns.Add(“[A1]”);
    foreach(testData中的整数)
    {
    DataRow drNew=dsTest.Tables[0].NewRow();
    drNew[“[A1]”]=编号;
    dsTest.Tables[0].Rows.Add(drNew);
    }
    timer.Start();
    var recs=da.Update(dsTest,“[Sheet1$]”;
    }
    }
    }
    }
    捕获(例外情况除外)
    {
    控制台输出写入线(例如消息);
    }
    最后
    {
    timer.Stop();
    控制台写入线(计时器已过);
    }
    //在我看结果之前不要关门
    Console.WriteLine();
    控制台。WriteLine(“按Enter键退出”);
    Console.ReadLine();
    }
    

作为一件额外的事情,签出openxml,它与Excel工作表以及非常大的Excel工作表一起工作非常好
static void Main(string[] args)
{
    var timer = new System.Diagnostics.Stopwatch();

    try
    {
        string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=\"Excel 12.0;HDR=YES;\";Data Source=Book1.xlsx";

        using (OleDbConnection oleDbConnection = new OleDbConnection(connectionString))
        {
            oleDbConnection.Open();

            string szHeaderSelect = "SELECT [A1] FROM  from [Sheet1$]";
            using (OleDbDataAdapter da = new OleDbDataAdapter(szHeaderSelect, oleDbConnection))
            {
                using (da.InsertCommand = new OleDbCommand("INSERT INTO [Sheet1$] ( [A1] ) VALUES (?)",
                    oleDbConnection))
                {
                    da.InsertCommand.Parameters.Add("A1", OleDbType.Integer, 20, "[A1]");

                    List<int> testData = new List<int>();
                    for (int i = 1; i < 400000; i++)
                    {
                        testData.Add(i);
                    }

                    DataSet dsTest = new DataSet();
                    dsTest.Tables.Add("[Sheet1$]");
                    dsTest.Tables[0].Columns.Add("[A1]");

                    foreach (int number in testData)
                    {
                        DataRow drNew = dsTest.Tables[0].NewRow();
                        drNew["[A1]"] = number;

                        dsTest.Tables[0].Rows.Add(drNew);
                    }

                    timer.Start();
                    var recs = da.Update(dsTest, "[Sheet1$]");
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.Out.WriteLine(ex.Message);
    }
    finally
    {
        timer.Stop();

        Console.WriteLine(timer.Elapsed);
    }

    // Don't close before I get to read the results
    Console.WriteLine();
    Console.WriteLine("Press Enter to quit.");
    Console.ReadLine();
}