在一个gridview中组合并绑定两个sql查询 [英] Combine and Bind two sql queries in one gridview

查看:75
本文介绍了在一个gridview中组合并绑定两个sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我想在一个Gridview中绑定两个不同的sql数据源。

这是c#代码的后面:



  protected   void  Page_Load( object  sender, EventArgs e)
{
// 建立MySQL连接
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings [ BasketballConnectionString1 ]的ToString());
string query,query2;
SqlCommand SqlCommand,SqlCommand2;
SqlDataReader reader,reader2;

SqlDataAdapter adapter = new SqlDataAdapter();
SqlDataAdapter adapter2 = new SqlDataAdapter();
// 打开与db的连接
conn.Open();

// 生成查询以获取联系人详细信息
query = SELECT blah1 blah1 blah1;
query2 = SELECT blah2 blah2 blah2;
SqlCommand = new SqlCommand(query,conn);
adapter.SelectCommand = new SqlCommand(query,conn);
SqlCommand2 = new SqlCommand(query2,conn);
adapter.SelectCommand = new SqlCommand(query2,conn);
// 执行查询
reader = SqlCommand.ExecuteReader();
reader2 = SqlCommand2.ExecuteReader();
// 分配结果
GridView4.DataSource = reader;
GridView4.DataSource = reader2;
// 绑定数据
GridView4.DataBind();
conn.Close();
// -------------------- ----------------------------------------
}



这是我所犯的错误:

已经有一个与此命令关联的开放DataReader必须先关闭。

是否有任何建议如何使用两个sql查询(query,query2)?

解决方案

以下文章应该有所帮助:



http://blogs.msdn.com/b/spike/archive/2009/08/20/there-is -anready-an-open-datareader-related-with-this-command-must-be-closed-first-explain -asxx [ ^ ]


您好Iratus7,



既然你指向同一个数据库,你可以使用组合两个查询到单个查询并获取它并将其绑定到数据源。

这对你来说很容易。

喜欢从blah1 B1选择B1.blah1,B2.blah2,blah2 B2



使用以下代码代替你的< br $>


 protected void Page_Load(object sender,EventArgs e)
{
//建立MySQL连接
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings [BasketballConnectionString1]。ToString());
字符串查询;
SqlCommand SqlCommand;
SqlDataReader阅读器;

SqlDataAdapter adapter = new SqlDataAdapter();
// SqlDataAdapter adapter2 = new SqlDataAdapter();
//打开与db
conn.Open()的连接;

//生成查询以获取联系人详细信息
query =从blah1 B1,blah2 B2中选择B1.blah1,B2.blah2;
// query2 =SELECT blah2 blah2 blah2;
SqlCommand = new SqlCommand(query,conn);
adapter.SelectCommand = new SqlCommand(query,conn);
// SqlCommand2 = new SqlCommand(query2,conn);
//adapter.SelectCommand = new SqlCommand(query2,conn);
//执行查询
reader = SqlCommand.ExecuteReader();
// reader2 = SqlCommand2.ExecuteReader();
//分配结果
GridView4.DataSource = reader;
//GridView4.DataSource = reader2;
//绑定数据
GridView4.DataBind();
conn.Close();
// -------------------------------------------- ----------------
}





希望这会对你有所帮助位。



问候,

RK


如果它的列名相同,你可以使用UNION





UNION

Hello there. I want to bind tow different sql datasources in one Gridview.
This is the behind c# code:

protected void Page_Load(object sender, EventArgs e)
    {
        //Establishing the MySQL Connection
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BasketballConnectionString1"].ToString());
        string query, query2;
        SqlCommand SqlCommand, SqlCommand2;
        SqlDataReader reader, reader2;

        SqlDataAdapter adapter = new SqlDataAdapter();
        SqlDataAdapter adapter2 = new SqlDataAdapter();
        //Open the connection to db
        conn.Open();

        //Generating the query to fetch the contact details
        query = "SELECT blah1 blah1 blah1";
        query2 = "SELECT blah2 blah2 blah2";
        SqlCommand = new SqlCommand(query, conn);
        adapter.SelectCommand = new SqlCommand(query, conn);
        SqlCommand2 = new SqlCommand(query2, conn);
        adapter.SelectCommand = new SqlCommand(query2, conn);
        //execute the query
        reader = SqlCommand.ExecuteReader();
        reader2 = SqlCommand2.ExecuteReader();
        //Assign the results 
        GridView4.DataSource = reader;
        GridView4.DataSource = reader2;
        //Bind the data
        GridView4.DataBind();
        conn.Close();
        //------------------------------------------------------------
    }


and this is the error that i take:
"There is already an open DataReader associated with this Command which must be closed first."
Is there any suggestion how to use both two sql queries(query,query2)?

解决方案

This following article should help:

http://blogs.msdn.com/b/spike/archive/2009/08/20/there-is-already-an-open-datareader-associated-with-this-command-which-must-be-closed-first-explained.aspx[^]


Hi Iratus7,

Since you are pointing to same database, you can use combine two query into single query and fetch it and bind it to the datasource.
It will be easy for you.
Like select B1.blah1,B2.blah2 from blah1 B1,blah2 B2

Use the below code instead of yours

protected void Page_Load(object sender, EventArgs e)
    {
        //Establishing the MySQL Connection
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BasketballConnectionString1"].ToString());
        string query;
        SqlCommand SqlCommand;
        SqlDataReader reader;
 
        SqlDataAdapter adapter = new SqlDataAdapter();
        //SqlDataAdapter adapter2 = new SqlDataAdapter();
        //Open the connection to db
        conn.Open();
 
        //Generating the query to fetch the contact details
        query = "select B1.blah1,B2.blah2 from blah1 B1,blah2 B2";
        //query2 = "SELECT blah2 blah2 blah2";
        SqlCommand = new SqlCommand(query, conn);
        adapter.SelectCommand = new SqlCommand(query, conn);
        //SqlCommand2 = new SqlCommand(query2, conn);
        //adapter.SelectCommand = new SqlCommand(query2, conn);
        //execute the query
        reader = SqlCommand.ExecuteReader();
        //reader2 = SqlCommand2.ExecuteReader();
        //Assign the results 
        GridView4.DataSource = reader;
        //GridView4.DataSource = reader2;
        //Bind the data
        GridView4.DataBind();
        conn.Close();
        //------------------------------------------------------------
    }



Hope this would help you a bit.

Regards,
RK


if its a same column name ,you can use UNION


UNION


这篇关于在一个gridview中组合并绑定两个sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆