首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

.net中的数据库方面的有关问题

2012-03-26 
.net中的数据库方面的问题SqlConnectionconnnewSqlConnection(System.Configuration.ConfigurationManage

.net中的数据库方面的问题
SqlConnection   conn   =   new   SqlConnection(System.Configuration.ConfigurationManager.AppSettings[ "DSN "]);
                string   str1   =   "select   *from   table1 ";
                conn.Open();
                SqlCommand   comm1   =   new   SqlCommand(str1,   conn);
                SqlDataReader   dr1   =   comm1.ExecuteReader();
                while(dr.read())
                        {
                                string   s= "select   *   from   table   2 "
                                SqlCommand   comm   =   null;
                                SqlCommand   comm   =   new   SqlCommand(s,   conn);
                                SqlDataReader   dr   =   comm.ExecuteReader();//执行到这一句时提示:已有打开的与此命令相关联的   DataReader,必须首先将它关闭
                          }      
这是一个嵌套读取数据库的程序,请问有什么办法让第二个datareader也可以正常读取?

[解决办法]
再创建一个连接放在外面. SqlCommand comm = new SqlCommand(s, conn2);
[解决办法]
实在不得已才会这么用.
[解决办法]
while(dr.read())
{
string s= "select * from table 2 "
SqlCommand comm = null;
SqlCommand comm = new SqlCommand(s, conn);
SqlDataReader dr = comm.ExecuteReader();
}
改为:试试看
SqlCommand comm = null;
SqlDataReader dr=null;
while(dr.read())
{
string s= "select * from table 2 "
comm = new SqlCommand(s, conn);
dr=null;
dr = comm.ExecuteReader();
}
[解决办法]
用dataset吧,先把外层的放到数据集里,从头到尾是一个循环,再用datareader做内部循环.
[解决办法]
while(dr.read())
这句

热点排行
Bad Request.