想每隔1秒向数据库中写入当前时间,结果没过几秒,数据库中有了几千条时间,顺序也不对,不知为什么? private void button1_Click(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Enabled = true;
        }        private void timer1_Tick(object sender, EventArgs e)
        {
            
            SqlConnection cn = new SqlConnection(con);
            SqlCommand com = new SqlCommand("select * from test0419", cn);
            SqlDataAdapter adp = new SqlDataAdapter(com);            DataSet ds = new DataSet();
            adp.Fill(ds, "test0419");            DataTable dt = ds.Tables[0];
            DataRow newRow;
            newRow = dt.NewRow();
            newRow["时间"] = System.DateTime.Now.ToString();
            dt.Rows.Add(newRow);
                        SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(con, SqlBulkCopyOptions.UseInternalTransaction);
            sqlbulkcopy.DestinationTableName = "test0419";
            sqlbulkcopy.WriteToServer(dt);
            
        }
SQLC#datatime

解决方案 »

  1.   

    LZ你看看的"button1_Click"事件之前的time的enable是不是已经为true,还有之前的interval是不是较短?
      

  2.   

      SqlCommand com = new SqlCommand("select * from test0419", cn);这个每次会把上次插入的结果读出来再插入,造成结果累加的效果。  SqlCommand com = new SqlCommand("select * from test0419 where 1<>1", cn);