【问题标题】:cannot implicitly convert type 'int' to 'system.data.dataset'无法将类型“int”隐式转换为“system.data.dataset”
【发布时间】:2013-08-17 04:24:39
【问题描述】:

大家好,我需要您的帮助,我正在尝试执行查询并将所有检索到的数据放入数据集中,但我收到此错误"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'"

非常感谢您的帮助。

【问题讨论】:

    标签: c# asp.net oracle


    【解决方案1】:

    问题在于ExecuteNonQuery 返回受影响的行数(整数),而不是DataSetDataReader。恐怕您没有正确使用 ADO.NET 组件。

    这两行足以填满一个DataSet

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

    无论如何,这不是您唯一的问题,您将Sql* ADO.NET 组件与Oracle*ones 混合在一起。适配器应该是OracleDataAdapter

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

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

    OracleCommand cmd = new OracleCommand(myquery, Astra_db_Conn);
    

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

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

    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);
    }
    

    【讨论】:

    • 我可以告诉你已经从事这个行业很长时间了,谢谢
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      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 绑定它

      Datalist1.DataBind();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多