日期:2014-05-18  浏览次数:20806 次

datagridview显示两个表的问题~如果解决了,立刻结贴
问题描述:两个表mps_material_cal和itemdata,要求显示mps_material_cal表中的itemno和quantity字段,条件是mps_material_cal.itemno=itemdata.itemno并且表itemdata中的mp_code字段为zz,这个通过sql语句能够实现,在sql查询分析器中已经运行过了是可以的。现在关键是这段程序的最后两句该怎么写,就是tableadapter的问题,究竟该使用哪个adapter填到哪个dataset中。
这个问题卡了我好几天了,还是不知道怎么处理,特求救,以下是程序代码:最后两句不知道怎么改?

SqlDataAdapter wahahaDataSet1TableAdapters = new SqlDataAdapter();
  //连接数据库  
  string myConnectionString = "Data Source=IBM-715A590BD4E;Initial Catalog=wahaha;Persist Security Info=True;User ID=sa;Password=sa;";
  //设置数据库连接
  SqlConnection myConnection = new SqlConnection(myConnectionString);
  //设置数据库操作命令
  string myInsertQuery = "select mps_material_cal.itemno,mps_material_cal.quantity from mps_material_cal join itemdata on(mps_material_cal.itemno=itemdata.itemno) where itemdata.mp_code='zz'";
  SqlCommand myCommand = new SqlCommand(myInsertQuery);
  myCommand.Connection = myConnection; //设置 SqlCommand实例使用的连接
  myConnection.Open(); //打开数据库
  myCommand.ExecuteNonQuery();
  this.resource_shebeiTableAdapter.Fill(this.wahahaDataSet.resource_shebei);
  //this.wahahaDataSet1TableAdapters.Fill(this.wahahaDataSet1.mps_material_cal);



------解决方案--------------------
C# code
Dataset ds=new DataSet()
            //连接数据库           
            string myConnectionString = "Data Source=IBM-715A590BD4E;Initial Catalog=wahaha;Persist Security Info=True;User ID=sa;Password=sa;";
            //设置数据库连接
            SqlConnection myConnection = new SqlConnection(myConnectionString);
            //设置数据库操作命令
            string myInsertQuery = "select mps_material_cal.itemno,mps_material_cal.quantity from mps_material_cal join itemdata on(mps_material_cal.itemno=itemdata.itemno) where  itemdata.mp_code='zz'";
            SqlCommand myCommand = new SqlCommand(myInsertQuery);
            myCommand.Connection = myConnection;  //设置 SqlCommand实例使用的连接
            myConnection.Open();    //打开数据库
           SqlDataAdapter wahahaDataSet1TableAdapters = new SqlDataAdapter(myCommand,myConnection);
          wahahaDataSet1TableAdapters.Fill(ds);
myConnection.Close()
return ds

------解决方案--------------------
mqcan说的就是对的.GridView DataBing需要数据源,通常用DataSet很方便的.

Dataset ds=new DataSet()
//连接数据库
string myConnectionString = "Data Source=IBM-715A590BD4E;Initial Catalog=wahaha;Persist Security Info=True;User ID=sa;Password=sa;";
//设置数据库连接
SqlConnection myConnection = new SqlConnection(myConnectionString);
//设置数据库操作命令
string myInsertQuery = "select mps_material_cal.itemno,mps_material_cal.quantity from mps_material_cal join itemdata on(mps_material_cal.itemno=itemdata.itemno) where itemdata.mp_code='zz'";
SqlCommand myCommand = new SqlCommand(myInsertQuery);
myCommand.Connection = myConnection; //设置 SqlCommand实例使用的连接
myConnection.Open(); //打开数据库
SqlDataAdapter wahahaDataSet1TableAdapters = new SqlDataAdapter(myCommand,myConnection);
wahahaDataSet1TableAdapters.Fill(ds);
myConnection.Close()
return ds