不能隐式地将类型'int'转换为'system.data.dataset'

时间:2013-08-15 01:23:21

标签: c# asp.net oracle

大家好,我需要你的帮助,我正在尝试执行查询并将所有检索到的数据放入数据集中,但我收到此错误"cannot implicitly convert type 'int' to 'system.data.dataset'"

以下是代码:

// this is a small piece of the sql
 String Astra_conn = ConfigurationManager.ConnectionStrings["AstraSeverConnection"].ConnectionString;

System.Text.StringBuilder sql = new System.Text.StringBuilder();

sql.Append(" SELECT ROWNUM AS ID, institution, LPAD (a.zone_name, 3, '0') AS campus, ");
sql.Append(" term_name AS term, student_instance_id AS student_id, subject, course, ");
sql.Append(" section_name AS section_num, offering AS title, ");

//Its OracleConnection because it is an Oracle server otherwise, it would be SqlConnection.

    DataSet rs = new DataSet();
    OracleConnection Astra_db_Conn = new OracleConnection(Astra_conn);
    string myquery = sql.ToString();
    OracleCommand cmd = new OracleCommand(myquery);

Astra_db_Conn.Open();
try
{
    SqlDataAdapter adpt = new SqlDataAdapter();
    rs = cmd.ExecuteNonQuery(); // this is where is get the error.
    adpt.Fill(rs);

}
catch(Exception e) 
{
log.Error("*** ERROR *** IRISExportQueries.loadStudentInfoLearningSites():" + e);

}

我也试过

 Astra_db_Conn.Open();
 try
 {
     SqlDataReader reader = new SqlDataAdapter();
     reader = cmd.ExecuteNonQuery(); // this is where is get the error.


 }
 catch(Exception e)
 {
log.Error("*** ERROR *** IRISExportQueries.loadStudentInfoLearningSites():" + e);</pre>

 }

然后我收到错误:"cannot implicitly convert type 'int' to 'System.Data.SqlClient.SqlDataReader'"

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:4)

问题是ExecuteNonQuery返回受影响的行数(整数),而不是DataSetDataReader。我担心你没有正确使用ADO.NET组件。

这两行足以填充DataSet

SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(rs);

在任何情况下,这不是您唯一的问题,您将Sql* ADO.NET组件与Oracle*组件混合在一起。适配器应为OracleDataAdapter

OracleDataAdapter adpt = new OracleDataAdapter(cmd);
adpt.Fill(rs);

其他:你永远不会将连接分配给命令。你应该做

OracleCommand cmd = new OracleCommand(myquery, Astra_db_Conn);

最后但并非最不重要的是,处理实现IDisposable接口的每个类的实例,否则不会释放作为数据集连接的非托管资源。

这是应用我所有建议的最终版本

var rs = new DataSet();
string myquery = sql.ToString();
using (var Astra_db_Conn = new OracleConnection(Astra_conn))
using (var cmd = new OracleCommand(myquery, Astra_db_Conn))
using (var adpt = new OracleDataAdapter(cmd))
{
    Astra_db_Conn.Open();
    adpt.Fill(rs);
}

答案 1 :(得分:1)

方法ExecuteNonQuery()返回一个int,其中包含受命令影响的行数。

要从查询中访问数据,您应该会看到现有的答案:Direct method from SQL command text to DataSet

答案 2 :(得分:0)

SqlDataAdapter adapt= new SqlDataAdapter(cmd.CommandText,cmd.Connection);
adapt.Fill(rs, " Your Table name as it is in database inside this quotation");

现在你可以将数据源视图,例如datalist或datatable或gridview提供给

Datalist1.DataSource= rs.Tables("Your Table name as it is in database inside the above q mark")

现在atlast jst bind it

Datalist1.DataBind();