日期:2014-05-19  浏览次数:21108 次

DataGridView显示存储过程返回数据集
运行下面的代码:
  //连接数据库
                              SqlConnection   conn   =   new   SqlConnection( "server=.;uid=sa;pwd=5366845;database=testfile ");          
                              SqlCommand     selectCMD       =       new   SqlCommand( "test ",   conn);          
                              selectCMD.CommandType       =       CommandType.StoredProcedure;          

                              //       创建DataAdapter对象填充数据      
                              DataSet   myDS   =   new   DataSet();
                              SqlDataAdapter   adapter   =   new   SqlDataAdapter(selectCMD);
                              adapter.Fill(myDS,   "testtable ");      

                              //       将返回的数据和DataGrid绑定显示  
                              myDataGrid.DataSource   =   myDS.Tables[ "testtable "];
                              myDataGrid.DataMember   =   "testtable ";
                              myDS   =   null;
                              adapter   =   null;


                              selectCMD.Dispose();
                              conn.Dispose();
其中test是存储过程。myDataGrid是我在设计窗口中拉到窗体中的,运行后
myDataGrid.DataMember   =   "testtable ";出错
“无法创建字段   testtable   的子列表。”
去掉这一句,不报错但myDataGrid中什么也没有!不知如何解决,请大家帮忙,谢谢!(是winform)


------解决方案--------------------
myDataGrid.DataSource = myDS;
这样就可以了.

或者
myDataGrid.DataSource = myDS.Tables[0]

建议去看下MSDN先.
------解决方案--------------------
不要用DataSet 填充。
用DataTable填充
string strSql = "select * from authors "
using (OleDbConnection OleConn = new OleDbConnection(strConn))
{
OleConn.Open();
OleDbDataAdapter oleDA = new OleDbDataAdapter(strSql, OleConn);
DataTable DT = new DataTable();
oleDA.Fill(DT);
DataView.DataSource = DT;
OleConn.Close();
}
------解