表db_list id 服务器名(TableName) 井名字(WellId) 时间 (InsertTime) 1 [WIS00001] B1ST4 2011-12-01 20:09:34.737
1 [WIS00002] B2ST4 2011-12-01 20:09:34.737
表wits_86 自动生成GUID 井名WID 时间 (time) 油嘴Nozzle
242342 B1ST4 2012-08-24 16:45:31.600 1616
大家不难看出.....list是主表,,,,找到井名字,通过服务器字段,找到wits_86表中油嘴字段,绑定到gv中,这样怎么实现呢?
public void bind()
{
string con = ConfigurationManager.ConnectionStrings["SQLServerConnectionString"].ConnectionString;
SqlDataAdapter sda = new SqlDataAdapter("select id,TableName,WellId from dbo.db_list", con);
DataSet ds = new DataSet();
sda.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
SqlConnection scn = new SqlConnection(con);
SqlDataAdapter sda1 = new SqlDataAdapter("select a.id,WellId,b.Nozzle from db_list as a left join " + ds.Tables[0].Rows[i]["TableName"] + ".dbo.wits_86 as b on a.WellId=b.WID", scn);
DataSet ds1 = new DataSet();
sda1.Fill(ds1);
dgUserList.DataSource = ds1;
dgUserList.DataBind();
}
}
我写的代码.后台绑定
但是为什么查出来的都是最后一个库中的数据呢?循环错了?请大神们帮帮忙吧!
最终效果是,石油油井.石油油嘴绑定到GREDVIEW...效果::B1ST4 1616
在循环里把几条SQL通过union all合起来,最后再一次执行:
"
select a.id,WellId,b.Nozzle from db_list as a left join " + ds.Tables[0].Rows[i]["TableName"] + ".dbo.wits_86 as b on a.WellId=b.WID
union all
select a.id,WellId,b.Nozzle...第二个库。。。
"
dgUserList.DataSource 这个你给他赋值的时候,当然显示的是最后一个
就是就是,那怎么样呢?我还在迷茫呢,就差最后这个语句了怎么样也搞不定了呜呜
DataSet ds3 = new DataSet();
for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
{
SqlConnection scn = new SqlConnection(con2);
SqlDataAdapter sda1 = new SqlDataAdapter("select top 1 a.id,WellId,b.Nozzle,SQID from db_list as a left join " + ds2.Tables[0].Rows[i]["TableName"] + ".dbo.wits_86 as b on a.WellId=b.WID where a.id=" + Convert.ToInt32(i + 1) + " and a.pf='" + this.txSearch.Value.Trim() + "' order by SQID desc", scn);
DataSet ds1 = new DataSet();
sda1.Fill(ds1);
ds3.Merge(ds1);
}
dgUserList.DataSource = ds3;
dgUserList.DataBind();