数据库超时问题的彻底解决

 最近被一个数据库超时问题,弄的很崩溃。花了很多时间,终于解决了。下面加色和加粗的地方重点看看。

  var cx_xscj = from aa in sjklj.xgk_xscj

                          select aa;

            foreach (var kk in cx_xscj)

            {

                sjklj.xgk_xscj.DeleteObject(kk);

            }

            sjklj.SaveChanges();

 sjklj.Connection.Close();//不用了,要关闭数据库释放资源,否则会引起超时问题

 

 //建立一个用来离线查询并存储的dataset

                SqlConnection sqlcon = new SqlConnection(conn);

                sqlcon.Open();

                SqlCommand sqlcmd = new SqlCommand();

                sqlcmd.Connection = sqlcon;

                sqlcmd.CommandTimeout = 1800;//30分钟

                sqlcmd.CommandText = "select * from xgk_xscj";

                SqlDataAdapter sda_gx_xscj = new SqlDataAdapter(sqlcmd);//面向全国的用户,还要设置where条件获取指定的学校的指定年级和年度的数据,尽量少。

                SqlCommandBuilder scb = new SqlCommandBuilder(sda_gx_xscj);//这个非常重要,否则可能保存失败

                DataSet ds_xscj_pm = new DataSet();

                sda_gx_xscj.Fill(ds_xscj_pm, "xscj");

 

 sda_gx_xscj.Update(ds_xscj_pm, "xscj"); //.Update(ds, "xscj");//放到最后执行

                     ds_xscj_pm.Dispose();

                     ds_xscj_tj.Dispose();

                     sda_gx_xscj.Dispose();

                     sqlcon.Close();

 

数据库超时问题的彻底解决

上一篇:MySQL的安装和介绍


下一篇:mybatis配置,idea报错:java.sql.SQLException: No suitable driver found for jdbc://localhost:3306/mybatis_test