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

一个关于DataTable返回值的问题
请问下边代码有错误么?
public   DataTable   GetTab(string   tabid)
{  
string   ConnString   =   System.Configuration.ConfigurationSettings.AppSettings[ "ConnectiongSqlServer "];


SqlConnection   myConn=new   SqlConnection(ConnString);
SqlCommand   topcmd=new   SqlCommand();
topcmd.Connection=   myConn;
topcmd.Parameters.Add(new   SqlParameter( "@tabid ",SqlDbType.VarChar,20));
topcmd.Parameters[ "@tabid "].Value=tabid;
topcmd.CommandText= "GetsysTab ";
                         
topcmd.CommandType   =   CommandType.StoredProcedure;//new

    DataTable   dstable   =   new   DataTable();
                               

SqlDataAdapter   adapter   =   new   SqlDataAdapter(topcmd);


adapter.Fill(dstable);
return   dstable;
}
为什么错误提示:类型   System.ComponentModel.ISite   的成员   System.ComponentModel.MarshalByValueComponent.Site   是接口,因此无法将其序列化。  



------解决方案--------------------
SqlCommand topcmd=new SqlCommand();
topcmd.Connection= myConn;
topcmd.Parameters.Add(new SqlParameter( "@tabid ",SqlDbType.VarChar,20));
topcmd.Parameters[ "@tabid "].Value=tabid;
topcmd.CommandText= "GetsysTab ";

topcmd.CommandType = CommandType.StoredProcedure;//new

这块应该有问题.
改一下写的顺序.
SqlCommand topcmd=new SqlCommand();
topcmd.Connection= myConn;
topcmd.CommandText= "GetsysTab ";
topcmd.Parameters.Add(new SqlParameter( "@tabid ",SqlDbType.VarChar,20));
topcmd.Parameters[ "@tabid "].Value=tabid;

------解决方案--------------------
要返回一个数据集,要定义一个游标类型的输出参数...具体给你看一个Oracle的例子吧..

1:建立包
CREATE OR REPLACE package SCOTT.pk_test
as
type mytype is ref cursor;
procedure p_get(mycs out mytype);
end;
/
2:建立包体:
CREATE OR REPLACE package BODY SCOTT.pk_test
as
procedure p_get(mycs out mytype)
as
begin
open mycs for select * from test;
end p_get;
end pk_test;
/
3:C#掉用存储过程:
OracleConnection orcn =new OracleConnection(System.Configuration.ConfigurationSettings.AppSettings[ "scott "]);
OracleCommand cmd=new OracleCommand( "pk_test.p_get ",orcn);
cmd.CommandType=CommandType.StoredProcedure;
OracleParameter p=new OracleParameter( "mycs ",OracleType.Cursor);
p.Direction=ParameterDirection.Output;
cmd.Parameters.Add(p);
OracleDataAdapter da=new OracleDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds, "test ");
this.DataGrid1.DataSource=ds.Tables[ "test "].DefaultView;
Page.DataBind();


或者看看这里:
http://dev.csdn.net/develop/article/59/59109.shtm
------解决方案--------------------
返回dataset就好. public DataTable GetTab(string tabid) ==> public DataSet GetTab(string tabid) fill时用dataset
------解决方案--------------------