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

时间:2022-11-25 16:35:44

Hi all, please I need your help, I am trying to execute a query and put all the retrieved data in a data set, but I get this error "cannot implicitly convert type 'int' to 'system.data.dataset'"

大家好,我需要你的帮助,我正在尝试执行一个查询并将所有检索到的数据放在一个数据集中,但我得到这个错误“不能隐式地将类型'int'转换为'system.data.dataset'”

Here's the code:

这是代码:

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

}

I've also tried

我也试过了

 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>

 }

Then I get the error: "cannot implicitly convert type 'int' to 'System.Data.SqlClient.SqlDataReader'"

然后我得到错误:“不能隐式地将类型'int'转换为'System.Data.SqlClient.SqlDataReader'”

Thanks your help will be very much appreciated.

非常感谢您的帮助。

3 个解决方案

#1


4  

The problem is that ExecuteNonQuery returns the number of affected rows (an integer) and not a DataSet or DataReader. I'm afraid you're not using ADO.NET components correctly.

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

These 2 lines are enough to fill a DataSet

这两行足以填充DataSet

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

In any case this is not your only problem, you're mixing Sql* ADO.NET components with Oracle*ones. Adapter should be OracleDataAdapter

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

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

Something else: you're never assigning the connection to the command. You should do

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

OracleCommand cmd = new OracleCommand(myquery, Astra_db_Conn);

And at last but not least important, dispose every instance of classes implementing IDisposable interface, otherwise unmanaged resources as connections to datasase won't be released.

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

This is the final version applying all my suggestions

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

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


1  

The method ExecuteNonQuery() returns an int with the number of rows that are affected by the command.

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

To access the data from the query you should see this existing answer: Direct method from SQL command text to DataSet.

要从查询中访问数据,您应该看到现有的答案:从SQL命令文本到DataSet的直接方法。

#3


0  

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

now u can give source to ur data views like datalist or datatable or gridview as following

现在你可以给你的数据视图提供源,如datalist或datatable或gridview,如下所示

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

now atlast jst bind it

现在atlast jst绑定它

Datalist1.DataBind();

#1


4  

The problem is that ExecuteNonQuery returns the number of affected rows (an integer) and not a DataSet or DataReader. I'm afraid you're not using ADO.NET components correctly.

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

These 2 lines are enough to fill a DataSet

这两行足以填充DataSet

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

In any case this is not your only problem, you're mixing Sql* ADO.NET components with Oracle*ones. Adapter should be OracleDataAdapter

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

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

Something else: you're never assigning the connection to the command. You should do

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

OracleCommand cmd = new OracleCommand(myquery, Astra_db_Conn);

And at last but not least important, dispose every instance of classes implementing IDisposable interface, otherwise unmanaged resources as connections to datasase won't be released.

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

This is the final version applying all my suggestions

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

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


1  

The method ExecuteNonQuery() returns an int with the number of rows that are affected by the command.

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

To access the data from the query you should see this existing answer: Direct method from SQL command text to DataSet.

要从查询中访问数据,您应该看到现有的答案:从SQL命令文本到DataSet的直接方法。

#3


0  

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

now u can give source to ur data views like datalist or datatable or gridview as following

现在你可以给你的数据视图提供源,如datalist或datatable或gridview,如下所示

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

now atlast jst bind it

现在atlast jst绑定它

Datalist1.DataBind();