关于多个Repeat控件与数据源绑定问题 【100分】 我用个cmd创建2个SqlDataReader,绑定2个repeat控件。结果提示错误。分别用2个conn和2个cmd,再2个单独的reader可以绑定。但觉得效率很低。请问如何在页面中,只使用1个conn和1个cmd就可以绑定。或者说如何能效率最大化。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用一个cmd,改变它的CommandText值,然后分别执行 SqlConnection dbConn = new SqlConnection();SqlCommand dbCmd = dbConn.CreateCommand();System.Data.SqlClient.SqlDataReader reader ;dbCmd.CommandText = "select * from abc";..reader = dbCmd.ExecuteReader();....dbCmd.CommandText = "select * from bcd";..reader = dbCmd.ExecuteReader();.... 使用DataTable保存数据,然后绑定。不过,这是因为你是那种手工的绑定的原因。如果你使用sqlDataSource数据源控件,那么当然自己使用DataTable作为数据源不但不高效,写很多代码(假设你的代码没有问题的话)。退一步说,你的做法效率低多少,你有具体的测试或者测试方法吗?帖出测试代码。 一个conn只能同时使用一个datareader用DataTable吧。 SqlDbCommand dbcom=new SqlDbCommand ("SQL语句 ;"+"SQL语句",dbconn); SqlDataReader dr=dbcom.ExecuteReader();do{this.Repater1.DataSoucre=dr;}while(dr.NextResult());dr.Close();dbconn.Close();}注意在二条语句中有分号 SqlDbCommand dbcom=new SqlDbCommand ("SQL语句 ;"+"SQL语句",dbconn); SqlDataReader dr=dbcom.ExecuteReader();this.Repater1.DataSoucre=dr;dr.NextResult();this.Repater2.DataSoucre=dr;dr.Close();dbconn.Close();} 你项目下所有的数据库操作类做一个基类嘛,每次都要创建conn和cmd多麻烦?如:public class Dbase { string _connectionString; public Dbase(string connectionString) { _connectionString = connectionString; } public void Execute(string str) { SqlConnection conn = new SqlConnection(_connectionString); SqlCommand cmd = new SqlCommand(str, conn); try { conn.Open(); cmd.ExecuteNonQuery(); } catch (SqlException e) { throw new Exception(e.Message); } finally { cmd.Dispose(); conn.Close(); } } public DataTable GetDataTable(string str) { SqlConnection conn = new SqlConnection(_connectionString); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(str, conn); try { conn.Open(); da.Fill(ds); return ds.Tables[0]; } catch (SqlException e) { throw new Exception(e.Message); } finally { da.Dispose(); conn.Close(); } } }在这里定义:public class SqlDataProvider { protected static Dbase _dbUtility = new Dbase("Data Source=localhost;Initial Catalog=test;User ID=sa;password=sa"); public static Dbase DbUtility { get { return _dbUtility; } } }在其他地方就可以集中使用了..如SqlDataProvider.DbUtility.GetDataTable("select * from table1"); 之前的代码是,主页面default.aspx CS中继承InsideUI类。 protected void Page_Load(object sender, EventArgs e) { ConnectDB conn1 = new ConnectDB(); RepeaterSimpleList.DataSource = GetDsBySQL("SELECT TOP 12 * FROM [View_Videos]", conn1.Cmd,conn1.Reader); RepeaterSimpleList.DataBind(); RepeaterMainVideos.DataSource = GetDsBySQL("SELECT TOP 10 * FROM [View_Videos]", conn1.Cmd,conn1.Reader); RepeaterMainVideos.DataBind(); conn1.Dispose(); }InsideUI类中 public SqlDataReader GetDsBySQL(string sql, SqlCommand cmd, SqlDataReader reader) { if (reader != null && reader.IsClosed == false) reader.Close(); cmd.Parameters.Clear(); cmd.CommandText = sql; reader = cmd.ExecuteReader(); return reader; }ConnectDB类中 private SqlConnection conn = new SqlConnection(); public SqlConnection Conn { get { return conn; } } private SqlCommand cmd = new SqlCommand(); public SqlCommand Cmd { get { return cmd; } set { cmd = value; } } private SqlDataReader reader; public SqlDataReader Reader { get { return reader; } set { reader = value; } }执行的时候在InsideUI类中停下 reader = cmd.ExecuteReader();错误信息:已有打开的与此命令相关联的 DataReader,必须首先将它关闭。调试信息中 cmd.CommandText "SELECT TOP 10 * FROM [View_Videos]" string- reader null System.Data.SqlClient.SqlDataReader============不明白怎么会产生这个错误。 suiqirui19872005(biy) 的;方法比较简单。暂时先用这个了。谢谢大家的意见。 【一个诡异的问题---->radiobuttonlist.selectedvalue取值不对】 请教大量实例化实体类对性能影响大吗? 如何让表格中的元素置于左上角? 大家有好用的CMS推荐吗? 怎样让鼠标移到超链接时,状态栏不显示链接地址?? 如何使textbox中的内容右对齐? 如下错误怎么解决?2 动态绑定的gridview,javascript实现鼠标点击列单元格如何获取该列的列标题? 二次开发是什么意思? ASP.NET网站html页面不运行Global.asax 空间商不给启动SQL的Agent服务,我的定时Update作业不能运行怎么办? 如何去掉html中所有超鏈接?
SqlCommand dbCmd = dbConn.CreateCommand();
System.Data.SqlClient.SqlDataReader reader ;dbCmd.CommandText = "select * from abc";
..
reader = dbCmd.ExecuteReader();
....
dbCmd.CommandText = "select * from bcd";
..
reader = dbCmd.ExecuteReader();
....
SqlDbCommand dbcom=new SqlDbCommand ("SQL语句 ;"+"SQL语句",dbconn);
SqlDataReader dr=dbcom.ExecuteReader();do
{
this.Repater1.DataSoucre=dr;
}while(dr.NextResult());
dr.Close();
dbconn.Close();
}注意在二条语句中有分号
SqlDataReader dr=dbcom.ExecuteReader();
this.Repater1.DataSoucre=dr;
dr.NextResult();this.Repater2.DataSoucre=dr;
dr.Close();
dbconn.Close();
}
你项目下所有的数据库操作类做一个基类嘛,每次都要创建conn和cmd多麻烦?如:public class Dbase
{
string _connectionString;
public Dbase(string connectionString)
{
_connectionString = connectionString;
}
public void Execute(string str)
{
SqlConnection conn = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(str, conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
conn.Close();
}
}
public DataTable GetDataTable(string str)
{
SqlConnection conn = new SqlConnection(_connectionString);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(str, conn);
try
{
conn.Open();
da.Fill(ds);
return ds.Tables[0];
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
finally
{
da.Dispose();
conn.Close();
}
} }
在这里定义:
public class SqlDataProvider
{
protected static Dbase _dbUtility = new Dbase("Data Source=localhost;Initial Catalog=test;User ID=sa;password=sa");
public static Dbase DbUtility
{
get { return _dbUtility; }
}
}在其他地方就可以集中使用了..如SqlDataProvider.DbUtility.GetDataTable("select * from table1");
{
ConnectDB conn1 = new ConnectDB();
RepeaterSimpleList.DataSource = GetDsBySQL("SELECT TOP 12 * FROM [View_Videos]", conn1.Cmd,conn1.Reader);
RepeaterSimpleList.DataBind();
RepeaterMainVideos.DataSource = GetDsBySQL("SELECT TOP 10 * FROM [View_Videos]", conn1.Cmd,conn1.Reader);
RepeaterMainVideos.DataBind();
conn1.Dispose();
}InsideUI类中 public SqlDataReader GetDsBySQL(string sql, SqlCommand cmd, SqlDataReader reader)
{
if (reader != null && reader.IsClosed == false)
reader.Close();
cmd.Parameters.Clear();
cmd.CommandText = sql;
reader = cmd.ExecuteReader();
return reader;
}ConnectDB类中
private SqlConnection conn = new SqlConnection(); public SqlConnection Conn
{
get { return conn; }
} private SqlCommand cmd = new SqlCommand();
public SqlCommand Cmd
{
get { return cmd; }
set { cmd = value; }
}
private SqlDataReader reader; public SqlDataReader Reader
{
get { return reader; }
set { reader = value; }
}执行的时候在InsideUI类中停下
reader = cmd.ExecuteReader();
错误信息:已有打开的与此命令相关联的 DataReader,必须首先将它关闭。调试信息中
cmd.CommandText "SELECT TOP 10 * FROM [View_Videos]" string
- reader null System.Data.SqlClient.SqlDataReader
============
不明白怎么会产生这个错误。