SqlCommand cmdm = new SqlCommand("select *from dbv_mcpagg where recorddate='" + Date.ToString("yyyy-MM-dd") + "'", conn);
            SqlDataReader strm = cmdm.ExecuteReader();
            while (strm.Read())
            {
                this.bmc_prod.Text = strm["production"].ToString();
                this.bmc_tot.Text = strm["totalqty"].ToString();
            }
            strm.Close();
            //------------------------------------------------------------------------------------------------------------
            SqlCommand cmddt = new SqlCommand("select *from dbv_dtagg where recorddate='" + Date.ToString("yyyy-MM-dd") + "'", conn);
            SqlDataReader strdt = cmddt.ExecuteReader();
            while (strdt.Read())
            {
                this.bmc_dt.Text = strdt["Downtime"].ToString();
            }
            strdt.Close();
假如一个页面中有几十个要查询的,而且每个要查询的数据都不在同一个数据库,那不是要重复写很多了啊,有没有什么简便的方法?

解决方案 »

  1.   

    把相同数据库的sql语句可以合在一起写,然后放datatable里,不同数据库只能开不同链接了
      

  2.   

    你可以寫一個通用方法嘛
    傳SQL進去
    返回一個DataReader
      

  3.   

    网上搜索一下“多表查询”,有很多例子的,只是要是同一个数据库,就可以用一个SqlCommand 进行多表查询
      

  4.   

    你可以写一个方法搞定
    此方法传两个参数(库名,sql语句) 返回一个 SqlDataReader
    然后在这个方法里实现连接数据库和执行SqlCommand,并且返回一个 SqlDataReader
      

  5.   

    写个通用的,用哪个数据库传哪个参数
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("select * from {0} ",数据库参数);
    sb.Append(" where ......")string sql = sb.ToString();
    SqlCommand cmddt = new SqlCommand(sql,conn);