两种方式执行代码的路线没什么不一样的啊,都是顺序执行以下几句代码(中间那4句是连在一起的,中间没有其它代码) db.openconnection();//db是自己封装的类 …… strSql="select xx into #temp from xx where xx"; db.ExecuteSql(strSql);//无论哪种方式,这句都执行成功 strSql = "select xx from #temp where xx"; DataTable dt = db.GetDataTable(strSql);//直接运行,是OK的,跳转,不ok …… db.closeconnection();//最后才关闭连接的
刚才代码试了下,单独一个页面和从另一个页面跳转,以下代码都可以运行,没有出现楼主说的问题 SqlConnection conn = new SqlConnection(connectstring); SqlCommand cmd1 = new SqlCommand("select * into #temp from tb", conn); SqlCommand cmd2 = new SqlCommand("select * from #temp", conn); conn.Open(); int x = cmd1.ExecuteNonQuery(); SqlDataReader reader = cmd2.ExecuteReader();
DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(cmd2); da.Fill(dt); conn.Close();是不是你的ExecuteSql或者GetDataTable里面有问题?
db.openconnection();//db是自己封装的类 …… strSql="select xx into #temp from xx where xx;select xx from #temp where xx"; DataTable dt = db.GetDataTable(strSql);//直接运行,是OK的,跳转,不ok …… db.closeconnection();//最后才关闭连接的改成这样,试试 另外全局临时表(带##)慎用,不好管理,建议不用。
只对当前连接
调用的批有效
连接一旦断开 就消失了
不过在create之后,是不会在sqlserver里有保存的。不使用后,它会自己释放的。
……
strSql="select xx into #temp from xx where xx";
db.ExecuteSql(strSql);//无论哪种方式,这句都执行成功
strSql = "select xx from #temp where xx";
DataTable dt = db.GetDataTable(strSql);//直接运行,是OK的,跳转,不ok
……
db.closeconnection();//最后才关闭连接的
页面,与数据库访问进程不是一回事.
数据库访问是由WEB服务器进行的,完事后,把得到的数据经转换成HTML,发给WEB客户端,这时,WEB服务器与数据库之间的连接已经断开了.
你想想,如果这个连接不断开,如果有一千万个人在访问同一个页面,而WEB服务器又得把这一千万个用户连接到数据库服务器,那数据库能受得了嘛!
其实,当页面下载到客户端以后,客户端与WEB服务器的连接也是断开的.因此,如果你要访问原来生成的那个#temp,就必须将访问语句写在生成#temp的同一段语句中,这样才能访问到.
通过登录页面转到页面A(这里还是运行页面A,同样是运行那几句代码,怎么就会突然断开连接,访问不到#temp呢?)
SqlCommand cmd1 = new SqlCommand("select * into #temp from tb", conn);
SqlCommand cmd2 = new SqlCommand("select * from #temp", conn);
conn.Open();
int x = cmd1.ExecuteNonQuery();
SqlDataReader reader = cmd2.ExecuteReader();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd2);
da.Fill(dt);
conn.Close();是不是你的ExecuteSql或者GetDataTable里面有问题?
1、跟踪程序运行到什么地方报错。
2、再仔细查看下 db.openconnection(),db.ExecuteSql()这两函数。
#temp临时表创建后,之后有没断开连接(即:db.closeconnection();)
你的登录页面造成了影响,如创建了同名的临时表而没清理掉;第二就是你对数据库的封装里有问题,第一次运行能过而第二次就不行了。
……
strSql="select xx into #temp from xx where xx;select xx from #temp where xx";
DataTable dt = db.GetDataTable(strSql);//直接运行,是OK的,跳转,不ok
……
db.closeconnection();//最后才关闭连接的改成这样,试试
另外全局临时表(带##)慎用,不好管理,建议不用。