日期:2014-05-20  浏览次数:20661 次

问个关于SqlDataAdapter的Update问题
C# code

为什么下面的updateThreadRun()函数的tAdapter.Update(tds.Tables[0])动作那么慢,我只是修改了1000行记录,2个打印时间居然相差1秒,是不是我的tAdapter某些设置不正确?
private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            button3.Enabled = true;
            tSelectCommand = sc.CreateCommand();
            tUpdateCommand = sc.CreateCommand();
            tAdapter = new SqlDataAdapter(tSelectCommand);
            tAdapter.UpdateCommand = tUpdateCommand;

            tSelectCommand.CommandText = "SELECT * FROM NowXcData";
            tUpdateCommand.CommandText = "UPDATE NowXcData SET TimeST = @TimeST WHERE CollectNodeId = @CollectNodeId";
            tUpdateCommand.Parameters.Clear();
            tUpdateCommand.Parameters.Add("@TimeST", SqlDbType.DateTime, 8, "TimeST");
            tUpdateCommand.Parameters.Add("@CollectNodeId", SqlDbType.Int, 4, "CollectNodeId");

            tAdapter.Fill(tds);
            _tUpdate = new Thread(new ThreadStart(updateThreadRun));
            _tUpdate.Start();
        }

        private void updateThreadRun()
        {
            while(isRun)
            {
                try
                {
                    DateTime dt = DateTime.Now;
                    
                    for (int i = 0; i < tds.Tables[0].Rows.Count; i++)
                    {
                        tds.Tables[0].Rows[i][0] = dt;
                    }
                    Trace.WriteLine("[GAS]: begin update NowXcData!");

                    tAdapter.Update(tds.Tables[0]);

                    Trace.WriteLine("[GAS]: end update NowXcData!");
                    Thread.Sleep(1000);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }



------解决方案--------------------
你可能需要对数据库做一些优化,不要总认为是程序的问题