0

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

感谢您的帮助,我们将不胜感激。

4

3 回答 3

4

问题是ExecuteNonQuery返回受影响的行数(整数)而不是 a DataSetor DataReader。恐怕您没有正确使用 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);
}
于 2013-08-15T01:27:41.740 回答
1

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

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

于 2013-08-15T01:29:18.737 回答
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 绑定它

Datalist1.DataBind();
于 2013-08-15T02:38:17.213 回答