我用个cmd创建2个SqlDataReader,绑定2个repeat控件。结果提示错误。分别用2个conn和2个cmd,再2个单独的reader可以绑定。但觉得效率很低。请问如何在页面中,只使用1个conn和1个cmd就可以绑定。或者说如何能效率最大化。

解决方案 »

  1.   

    用一个cmd,改变它的CommandText值,然后分别执行
      

  2.   

    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();
    ....
      

  3.   

    使用DataTable保存数据,然后绑定。不过,这是因为你是那种手工的绑定的原因。如果你使用sqlDataSource数据源控件,那么当然自己使用DataTable作为数据源不但不高效,写很多代码(假设你的代码没有问题的话)。退一步说,你的做法效率低多少,你有具体的测试或者测试方法吗?帖出测试代码。
      

  4.   

    一个conn只能同时使用一个datareader用DataTable吧。
      

  5.   


    SqlDbCommand dbcom=new SqlDbCommand ("SQL语句 ;"+"SQL语句",dbconn);
     SqlDataReader dr=dbcom.ExecuteReader();do
    {
    this.Repater1.DataSoucre=dr;
    }while(dr.NextResult());
    dr.Close();
    dbconn.Close();
    }注意在二条语句中有分号
      

  6.   

    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();
    }
      

  7.   


     你项目下所有的数据库操作类做一个基类嘛,每次都要创建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");
      

  8.   

    之前的代码是,主页面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
    ============
    不明白怎么会产生这个错误。
      

  9.   

    suiqirui19872005(biy) 的;方法比较简单。暂时先用这个了。谢谢大家的意见。