日期:2014-05-18 浏览次数:20818 次
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