SqlDataReader.NextResult()有什么作用?

sakuta 2003-04-22 11:11:05
请问SqlDataReader.NextResult()有什么作用?不是获得下一个结果集嘛?
有没有是用的例子?
...全文
154 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qtj 2003-04-22
  • 打赏
  • 举报
回复
当读取批处理 Transact-SQL 语句的结果时,使数据读取器前进到下一个结果。
用于处理多个结果,这些结果可通过执行批处理 Transact-SQL 语句而生成。
默认情况下,数据读取器定位在第一项结果上。

free_free 2003-04-22
  • 打赏
  • 举报
回复
/// <summary>
/// Queries the FMStocks7 database
/// <param name='statement'>Sql statement to execute</param>
/// <returns>DataSet filled with the results of running the query</returns>
/// </summary>

public DataSet RunSQL( string statement )
{
Debug.Assert( connection != null );

SqlCommand command = new SqlCommand( statement, connection );
DataSet dataSet = new DataSet();
command.Connection.Open();
SqlDataReader reader = null;

try {
// We use a reader so we can handle queries that do not
// return record sets. If the query returns records, we add
// them to a table. If it doesn't, then we add a table to
// display the number of affected records.

reader = command.ExecuteReader();

do {
// Create new data table

DataTable schemaTable = reader.GetSchemaTable();
DataTable dataTable = new DataTable();

if ( schemaTable != null )
{
// A query returning records was executed

for ( int i = 0; i < schemaTable.Rows.Count; i++ )
{
DataRow dataRow = schemaTable.Rows[ i ];
// Create a column name that is unique in the data table
string columnName = ( string )dataRow[ "ColumnName" ] + "<C" + i + "/>";
// Add the column definition to the data table
DataColumn column = new DataColumn( columnName, ( Type )dataRow[ "DataType" ] );
dataTable.Columns.Add( column );
}

dataSet.Tables.Add( dataTable );

// Fill the data table we just created

while ( reader.Read() )
{
DataRow dataRow = dataTable.NewRow();

for ( int i = 0; i < reader.FieldCount; i++ )
dataRow[ i ] = reader.GetValue( i );

dataTable.Rows.Add( dataRow );
}
}
else
{
// No records were returned

DataColumn column = new DataColumn( RowsAffected );
dataTable.Columns.Add(column);
dataSet.Tables.Add( dataTable );
DataRow dataRow = dataTable.NewRow();
dataRow[0] = reader.RecordsAffected;
dataTable.Rows.Add( dataRow );
}
}
while ( reader.NextResult() );
}
catch ( SqlException e )
{
// There was an error executing the query, display it to the user

DataTable errorTable = new DataTable( Error );
DataColumn column = new DataColumn( Error );
errorTable.Columns.Add(column);
dataSet.Tables.Add( errorTable );
DataRow dataRow = errorTable.NewRow();
dataRow[0] = e.Message;
errorTable.Rows.Add( dataRow );
}
finally
{
if(reader!=null)
reader.Close();
}
return dataSet;
}
ermachao 2003-04-22
  • 打赏
  • 举报
回复
可以举一个例子吗?

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧