gezhu1212
2021-06-07 15:04
采纳率: 88.9%
浏览 15

C#数据存数据库,稳定的10秒丢一次数,这是怎么回事

我对着代码瞅了半天,也没看出来哪里有错误,请大神们指点下,万分感谢!

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (MainFlag)
            {
                this.Show();
                MainFlag = false;
            }
            lishizhong++;
            this.date.Text = DateTime.Today.ToString("yyyy-MM-dd");
            this.time.Text = DateTime.Now.ToString("HH:mm:ss.ff").Substring(0, 8);

            SqlDataAdapter sda = new SqlDataAdapter("select * from 123", mycon);
            DataTable table = new DataTable();
            sda.Fill(table);
            for (int i = 0; i < zongliang; i++)
            {
                if (flagreceive[i] == true)
                {
                    DBcaozuo.gxq30(mingzi, flagreceive, on_off, basedata1);
                    flagreceive[i] = false;
                }
                if ((flagreceive[i] == true )&& (lishizhong >= 10))
                {
                    DBcaozuo.gxlis(byte14analog, mingzi_permanent, flagreceive, on_off, basedata1);
                    flagreceive[i] = false;
                    lishizhong = 0;
                }
            }
        }

        //更新前30数据表
        public static void gxq30(/*int[,] byte14analog,*/ string[] mingzi, bool[] flagreceive, int[,] on_off, int[] basedata1)
        {
            mycon.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from 123", mycon);
            DataTable table = new DataTable();
            sda.Fill(table);
            string dateandtime = DateTime.Today.ToString("yyyy-MM-dd") + " " + DateTime.Now.ToString("HH:mm:ss.ff");
            for (int k = 0; k < table.Rows.Count; k++)
            {
                if (MainInterface.IsExistTable(mingzi[k], "123") == true)
                {
                    if ((flagreceive[k] == true) && (on_off[k, 0] == 1))
                    {
                        string a = table.Rows[k][268].ToString();
                        int p30 = Convert.ToInt32(a);
                        string UpdateTempTable = "";
                        string UpdateTempTable1 = "";
                        string temp = "";
                        for (int i = 0; i < 75; i++) { temp = temp + ",数" + i.ToString() + "=" + basedata1[i]; }
                        p30 = p30 + 1;
                        UpdateTempTable = "UPDATE " + mingzi[k] + " SET 时间 =" + "'" + dateandtime + "'" + temp + " where 主键=" + p30;
                        SqlCommand CmdCreateTable = new SqlCommand(UpdateTempTable, mycon);
                        CmdCreateTable.ExecuteNonQuery();

                        if (p30 > 1800) { p30 = 0; }
                        UpdateTempTable1 = "UPDATE 123 SET 前30 =" + p30 + " WHERE 序号=" + (k + 1);
                        SqlCommand CmdCreateTable1 = new SqlCommand(UpdateTempTable1, mycon);
                        CmdCreateTable1.ExecuteNonQuery();
                    }
                }
            }
            mycon.Close();
        }

        //更新历史数据表
        public static void gxlis(int[,] byte14analog, string[] mingzi_permanent, bool[] flagreceive, int[,] on_off, int[] basedata1)
        {
            mycon.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from 123", mycon);
            DataTable table = new DataTable();
            sda.Fill(table);
            string dateandtime = DateTime.Today.ToString("yyyy-MM-dd") + " " + DateTime.Now.ToString("HH:mm:ss.ff");
            for (int k = 0; k < table.Rows.Count; k++)
            {
                if (MainInterface.IsExistTable(mingzi_permanent[k], "123") == true)
                {
                    if ((flagreceive[k] == true) && (on_off[k, 0] == 1) && (MainInterface.lishizhong >= 10))
                    {
                        MainInterface.lishizhong = 0;
                        string UpdateHistTable = "";
                        string hist = "";
                        int[] bit14 = new int[10];
                        for (int i = 0; i < 10; i++)
                        {
                            if (i < 5)
                            {
                                bit14[i] = byte14analog[k, Convert.ToInt32(table.Rows[k][270 + i].ToString())];
                                if (on_off[k, Convert.ToInt32(table.Rows[k][285 + 2 * i].ToString())] == 1)
                                {
                                    bit14[i] = bit14[i] + 32768;
                                }
                                if (on_off[k, Convert.ToInt32(table.Rows[k][286 + 2 * i].ToString())] == 1)
                                {
                                    bit14[i] = bit14[i] + 16384;
                                }
                            }
                            else
                            {
                                bit14[i] = (byte14analog[k, Convert.ToInt32(table.Rows[k][265 + 2 * i].ToString())]) << 8;
                                bit14[i] = bit14[i] + (byte14analog[k, Convert.ToInt32(table.Rows[k][266 + 2 * i].ToString())]);
                            }
                            hist = hist + "," + bit14[i];
                        }
                        UpdateHistTable = "INSERT " + mingzi_permanent[k] + " VALUES (" + "'" + dateandtime + "'," + basedata1[0] + "," + basedata1[1] + "," + basedata1[2] + "," + basedata1[3] + "," + basedata1[4] + hist + ",0) ";
                        SqlCommand CmdCreateTable = new SqlCommand(UpdateHistTable, mycon);
                        CmdCreateTable.ExecuteNonQuery();
                    }
                }
            }
            mycon.Close();
        }
  • 好问题 提建议
  • 收藏

1条回答 默认 最新

  • 云湖之旅 2021-06-16 13:31
    已采纳

    断开可能是网络问题,你代码也有优化空间,起码是open后要异常捕获一下,必处理close吧,万一执行异常,数据库连接数几下就没了

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题