Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.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# 如何改进此代码_C#_Sql_Performance - Fatal编程技术网

C# 如何改进此代码

C# 如何改进此代码,c#,sql,performance,C#,Sql,Performance,此代码用于将新记录写入C:\by check exist first中的数据库。 我的问题对很多记录来说太慢了。以及如何提高它的速度 // conn is read from handydrive //conn2 read from C:\ 我想看看: 使您能够高效地大容量加载SQL 包含来自其他服务器的数据的服务器表 来源 顺便说一句:在上面的代码中,选择整个票据贴现表并不是一个好主意,尤其是如果表很大的话 [此外,您似乎可以执行单个TSQL语

此代码用于将新记录写入C:\by check exist first中的数据库。 我的问题对很多记录来说太慢了。以及如何提高它的速度

            // conn is read from handydrive
            //conn2 read from C:\

我想看看:

使您能够高效地大容量加载SQL 包含来自其他服务器的数据的服务器表 来源

顺便说一句:在上面的代码中,选择整个
票据贴现
表并不是一个好主意,尤其是如果表很大的话

[此外,您似乎可以执行单个TSQL语句,而不是在每一行中循环并往返到数据库。]


这个例子应该有帮助:

我想看看:

使您能够高效地大容量加载SQL 包含来自其他服务器的数据的服务器表 来源

顺便说一句:在上面的代码中,选择整个
票据贴现
表并不是一个好主意,尤其是如果表很大的话

[此外,您似乎可以执行单个TSQL语句,而不是在每一行中循环并往返到数据库。]


这个例子应该会有所帮助:

让我们从提高代码可读性开始。结果如下:

        SqlCeCommand cmd1 = new SqlCeCommand("Select * from bill_discount  ", conn);

        SqlCeDataReader dr1 = cmd1.ExecuteReader();

        while (dr1.Read() != false)
        {
            SqlCeCommand cmd4 = new SqlCeCommand("Select * from bill_discount where bill_no='" + dr1.GetInt32(0) + "' AND bill_shopdc='" + dr1.GetString(2) + "'  ", conn2);

            SqlCeDataReader dr4 = cmd4.ExecuteReader();
            if (dr4.Read() == false)
            {
                SqlCeCommand cmd2 = new SqlCeCommand("INSERT INTO bill_discount (bill_no,bill_value,bill_shopdc) VALUES ('" + dr1.GetInt32(0) + "','" + dr1.GetDouble(1) + "','" + dr1.GetString(2) + "') ", conn2);

               // SqlCeDataReader dr2 = cmd2.ExecuteReader();
                cmd2.ExecuteNonQuery();

            }

        }
        //-------------------------------------------------------------------
必须处理一次性物品。让我们做吧

我们还将删除SQL注入

最后,让我们优化第二个查询:如果只想检查数据库中是否存在该值,则不需要选择某些内容并执行读取器

SqlCeCommand getAllBills = new SqlCeCommand("select * from bill_discount", primaryConnection);
SqlCeDataReader allBillsReader = getAllBills.ExecuteReader();
while (allBillsReader.Read())
{
    SqlCeCommand getBill = new SqlCeCommand("select * from bill_discount where bill_no = '" + allBillsReader.GetInt32(0) + "' and bill_shopdc = '" + allBillsReader.GetString(2) + "'  ", secondaryConnection);
    SqlCeDataReader billReader = getBill.ExecuteReader();
    if (!billReader.Read())
    {
        SqlCeCommand addMissingBill = new SqlCeCommand("insert into bill_discount (bill_no, bill_value, bill_shopdc) values ('" + allBillsReader.GetInt32(0) + "', '" + allBillsReader.GetDouble(1) + "', '" + allBillsReader.GetString(2) + "')", secondaryConnection);
        addMissingBill.ExecuteNonQuery();
    }
}
我们到了


现在,它仍然是一个低性能的解决方案。为了更有效,您可能希望在带有联接的单个SQL查询中执行相同的操作。由于两个表可能位于不同的服务器上,您可以查看链接的服务器:这是一项功能,可以从多个服务器对多个表执行单个查询。

让我们从提高代码可读性开始。结果如下:

        SqlCeCommand cmd1 = new SqlCeCommand("Select * from bill_discount  ", conn);

        SqlCeDataReader dr1 = cmd1.ExecuteReader();

        while (dr1.Read() != false)
        {
            SqlCeCommand cmd4 = new SqlCeCommand("Select * from bill_discount where bill_no='" + dr1.GetInt32(0) + "' AND bill_shopdc='" + dr1.GetString(2) + "'  ", conn2);

            SqlCeDataReader dr4 = cmd4.ExecuteReader();
            if (dr4.Read() == false)
            {
                SqlCeCommand cmd2 = new SqlCeCommand("INSERT INTO bill_discount (bill_no,bill_value,bill_shopdc) VALUES ('" + dr1.GetInt32(0) + "','" + dr1.GetDouble(1) + "','" + dr1.GetString(2) + "') ", conn2);

               // SqlCeDataReader dr2 = cmd2.ExecuteReader();
                cmd2.ExecuteNonQuery();

            }

        }
        //-------------------------------------------------------------------
必须处理一次性物品。让我们做吧

我们还将删除SQL注入

最后,让我们优化第二个查询:如果只想检查数据库中是否存在该值,则不需要选择某些内容并执行读取器

SqlCeCommand getAllBills = new SqlCeCommand("select * from bill_discount", primaryConnection);
SqlCeDataReader allBillsReader = getAllBills.ExecuteReader();
while (allBillsReader.Read())
{
    SqlCeCommand getBill = new SqlCeCommand("select * from bill_discount where bill_no = '" + allBillsReader.GetInt32(0) + "' and bill_shopdc = '" + allBillsReader.GetString(2) + "'  ", secondaryConnection);
    SqlCeDataReader billReader = getBill.ExecuteReader();
    if (!billReader.Read())
    {
        SqlCeCommand addMissingBill = new SqlCeCommand("insert into bill_discount (bill_no, bill_value, bill_shopdc) values ('" + allBillsReader.GetInt32(0) + "', '" + allBillsReader.GetDouble(1) + "', '" + allBillsReader.GetString(2) + "')", secondaryConnection);
        addMissingBill.ExecuteNonQuery();
    }
}
我们到了


现在,它仍然是一个低性能的解决方案。为了更有效,您可能希望在带有联接的单个SQL查询中执行相同的操作。由于两个表可能位于不同的服务器上,您可以查看链接的服务器:这是一项功能,可以在多个服务器的多个表上执行单个查询。

我看到您使用的是SqlCe,它在插入批量数据时有许多限制。主要限制是实际的SqlCe引擎。但是,您可以通过使用直接表插入绕过此操作:

using (SqlCeCommand getAllBills = new SqlCeCommand("select bill_no, bill_value, bill_shopdc from [bill_discount]", primaryConnection))
{
    using (SqlCeDataReader allBillsReader = getAllBills.ExecuteReader())
    {
        while (allBillsReader.Read())
        {
            using (SqlCeCommand getBill = new SqlCeCommand("if exists(select * from [bill_discount] where [bill_no] = @billNumber and bill_shopdc = @billShop) select 1 else select 0", secondaryConnection))
            {
                getBill.Parameters.AddWithValue("@billNumber", allBillsReader["bill_no"]);
                getBill.Parameters.AddWithValue("@billShop", allBillsReader["bill_shopdc"]);

                bool billExists = Convert.ToBoolean(getBill.ExecuteScalar());
                if (!billExists)
                {
                    using (SqlCeCommand addMissingBill = new SqlCeCommand("insert into [bill_discount] ([bill_no], [bill_value], [bill_shopdc]) values (@billNumber, @billValue, @billShop)", secondaryConnection))
                    {
                        addMissingBill.Parameters.AddWithValue("@billNumber", allBillsReader["bill_no"]);
                        addMissingBill.Parameters.AddWithValue("@billValue", allBillsReader["bill_value"]);
                        addMissingBill.Parameters.AddWithValue("@billShop", allBillsReader["bill_shopdc"]);

                        int countAffectedRows = addMissingBill.ExecuteNonQuery();
                        Debug.Assert(countAffectedRows == 1, "The data was not inserted.");
                    }
                }
            }
        }
    }
}

我看到您正在使用SqlCe,它在插入批量数据时有很多限制。主要限制是实际的SqlCe引擎。但是,您可以通过使用直接表插入绕过此操作:

using (SqlCeCommand getAllBills = new SqlCeCommand("select bill_no, bill_value, bill_shopdc from [bill_discount]", primaryConnection))
{
    using (SqlCeDataReader allBillsReader = getAllBills.ExecuteReader())
    {
        while (allBillsReader.Read())
        {
            using (SqlCeCommand getBill = new SqlCeCommand("if exists(select * from [bill_discount] where [bill_no] = @billNumber and bill_shopdc = @billShop) select 1 else select 0", secondaryConnection))
            {
                getBill.Parameters.AddWithValue("@billNumber", allBillsReader["bill_no"]);
                getBill.Parameters.AddWithValue("@billShop", allBillsReader["bill_shopdc"]);

                bool billExists = Convert.ToBoolean(getBill.ExecuteScalar());
                if (!billExists)
                {
                    using (SqlCeCommand addMissingBill = new SqlCeCommand("insert into [bill_discount] ([bill_no], [bill_value], [bill_shopdc]) values (@billNumber, @billValue, @billShop)", secondaryConnection))
                    {
                        addMissingBill.Parameters.AddWithValue("@billNumber", allBillsReader["bill_no"]);
                        addMissingBill.Parameters.AddWithValue("@billValue", allBillsReader["bill_value"]);
                        addMissingBill.Parameters.AddWithValue("@billShop", allBillsReader["bill_shopdc"]);

                        int countAffectedRows = addMissingBill.ExecuteNonQuery();
                        Debug.Assert(countAffectedRows == 1, "The data was not inserted.");
                    }
                }
            }
        }
    }
}

我建议使用存储过程,我也强烈建议学习LINQtoSQL,如果您了解ADO.NET,LINQtoSQL是一件轻而易举的事。这就像jQuery对javascript所做的一样……我建议使用存储过程,我也强烈建议学习LINQtoSQL,如果您了解ADO.NET,LINQtoSQL是一件轻而易举的事。这就像jQuery对javascript所做的一样……非常感谢您的帮助,我会解决它的。我希望我能尽快向你求助^^非常感谢你的帮助,我会解决的。我希望我能很快问你^^SqlBulkCopy类很难理解。SqlBulkCopy类很难理解。