sinat_33196372 2015-11-30 07:15 采纳率: 0%
浏览 2669
已采纳

c#向数据库添加大量数据为何我的这么慢

public void ShuaXin()
{

        SqlCommand com = new SqlCommand("delete from priceavg", DBHelper.con);
        DBHelper.con.Open();
        com.ExecuteNonQuery();
        DBHelper.con.Close();
        pros = new List<ProPrice>();
        com.CommandText = "select proid from proprice where newprice=1";
        DBHelper.con.Open();
        SqlDataReader re = com.ExecuteReader();
        ProPrice pp = null;
        while (re.Read())
        {
            pp = new ProPrice() { ProID = Convert.ToInt32(re["ProID"]) };
            pros.Add(pp);
        }
        re.Close();
        DBHelper.con.Close();
        //刷新上个月价格
        for (int i = 0; i < pros.Count; i++)
        {
            com.CommandText = string.Format("select ptype from product where id={0} ", pros[i].ProID);
            com.Connection = DBHelper.con;
            DBHelper.con.Open();
            if (com.ExecuteScalar() == null)
            {
                DBHelper.con.Close();
                continue;
            }
            int id = (int)com.ExecuteScalar();
            DBHelper.con.Close();
            DateTime beginTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1);  //获取本月的月份然后减去一个月
            DateTime finishTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1);

            if (set.Tables["Avg1"] != null)
            {
                set.Tables.Remove("Avg1");
            }
            string sql = string.Format("select " + getSqlWhere(id) + " from proprice  where proid={0} and addtime between '{1}' and '{2}'", pros[i].ProID, beginTime, finishTime);
            adapter = new SqlDataAdapter(sql, DBHelper.con);
            adapter.Fill(set, "Avg1");
            DataTable dt = set.Tables["Avg1"];
            string[] strs = new string[dt.Columns.Count + 1];
            int count1 = 0;
            if (dt.Columns.Count == 1)
            {
                strs = new string[dt.Columns.Count + 1];
            }
            double avg = 0;
            for (int j = 0; j < dt.Columns.Count; j++)
            {

                double num = 0;
                int count = 0;
                for (int c = 0; c < dt.Rows.Count; c++)
                {
                    if (dt.Rows[c][j] is DBNull || dt.Rows[c][j] == null)
                    {
                        count++;
                        continue;
                    }

                    if ((dt.Rows[c][j]).ToString() == "非数字")
                    {
                        count++;
                        continue;
                    }

                    if (Convert.ToDouble(dt.Rows[c][j]) == 0)
                    {
                        count++;
                        continue;
                    }
                    num += Convert.ToDouble(dt.Rows[c][j]);

                }

                strs[j] = Math.Round(num / (dt.Rows.Count - count), 2).ToString();

                if (strs[j] == "非数字")
                {
                    count1++;
                }
                else
                {
                    avg += Convert.ToDouble(strs[j]);
                }

            }
            strs[strs.Length - 1] = Math.Round((avg / (strs.Length - 1 - count1)), 2).ToString();

            com.CommandText = string.Format("select count(*) from product where id={0} ", pros[i].ProID);
            DBHelper.con.Open();
            if ((int)com.ExecuteScalar() == 0)
            {
                DBHelper.con.Close();
                continue;
            }
            DBHelper.con.Close();

            com.CommandText = string.Format("select IsSeason,ProDescription,ID,ProductImage,Spec,ProAddress,MaoZhong,JingZhong from product where id={0}", Convert.ToInt32(pros[i].ProID));
            DBHelper.con.Open();
            re = com.ExecuteReader();
            Model.Product p1 = null;
            if (re.Read())
            {
                p1 = new Model.Product()
                {
                    IsSeason = Convert.ToInt32(re["IsSeason"]),
                    ProDescription = re["ProDescription"].ToString(),
                    ID = Convert.ToInt32(re["ID"]),
                    ProductImage = re["ProductImage"].ToString(),
                    Spec = re["Spec"].ToString(),
                    ProAddress = re["ProAddress"].ToString(),
                    MaoZhong = re["MaoZhong"].ToString(),
                    JingZhong = re["JingZhong"].ToString()
                };
            }
            re.Close();
            DBHelper.con.Close();
            string sqlstr = @"INSERT INTO priceAvg ([IsSeason],[ProDescription],[编号],[图片],[品名],[规格],[产地],[毛重],[净重],[isnoBiaoZhunJin],[isnoBiaoZhunJinls]," + insertSql(id) + ",timetype) values('" + p1.IsSeason + "','" + p1.ProDescription + "','" + p1.ID + "','" + p1.ProductImage + "','" + PinMing(Convert.ToInt32(pros[i].ProID)) + "','" + p1.Spec + "','" + p1.ProAddress + "','" + p1.MaoZhong + "','";
            sqlstr = sqlstr + p1.JingZhong + "','" + 0 + "','" + 0 + "','";
            if (insertSql(id).Trim() == "pavgprice")
            {
                strs = new string[] { strs[0] };
            }
            int nb = 0;
            for (int m = 0; m < strs.Length; m++)
            {
                if (strs[m] == "非数字")
                {
                    strs[m] = "0";
                }
                if (strs[m] == null)
                {
                    strs[m] = "0";
                }
                if (strs[m] == "0")
                {
                    nb++;
                }
                sqlstr = sqlstr + strs[m] + "','";
            }
            if (nb == strs.Length)
            {
                continue;
            }
            sqlstr = sqlstr + 1 + "')";
            com.CommandText = sqlstr;
            DBHelper.con.Open();
            com.ExecuteNonQuery();
            DBHelper.con.Close();
        }
    }
  • 写回答

1条回答 默认 最新

  • u013961798 2015-12-01 00:55
    关注

    用vs的性能分析,看看各行语句的运行时间

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境