本文整理汇总了C#中DbCommand.ExecuteReader方法的典型用法代码示例。如果您正苦于以下问题:C# DbCommand.ExecuteReader方法的具体用法?C# DbCommand.ExecuteReader怎么用?C# DbCommand.ExecuteReader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DbCommand
的用法示例。
在下文中一共展示了DbCommand.ExecuteReader方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteReader
public IDataReader ExecuteReader(DbCommand cmd)
{
DbConnection conn = null;
try {
conn = this.GetOpenConnection();
cmd.Connection = conn;
CommandBehavior behavior = this.IsSharedConnection() ? CommandBehavior.Default : CommandBehavior.CloseConnection;
return cmd.ExecuteReader(behavior);
} catch {
ReleaseConnection(conn);
throw;
}
}
示例2: Query
//----------------------------------------------------------------------------------------------------
/// <summary>
/// 쿼리 요청 ( SELECT )
/// </summary>
/// <param name="sql">SQL문</param>
/// <returns>성공 유무</returns>
//----------------------------------------------------------------------------------------------------
public bool Query( string sql )
{
try
{
CloseQuery();
m_command = m_connection.CreateCommand();
m_command.CommandText = sql;
m_reader = m_command.ExecuteReader();
return true;
}
catch( Exception ex )
{
Log( ex.ToString() );
m_error = ex.Message;
CloseQuery();
return false;
}
}
示例3: CreateDataReader
/// <summary>
/// Create a DataReader for the specified command.
/// <para>Please note that the associated connection is automatically closed when then datareader is closed.</para>
/// </summary>
public DbDataReader CreateDataReader(DbCommand cm)
{
DbConnection cn = cm.Connection;
DbDataReader rd = null;
errorMessage = "";
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
rd = cm.ExecuteReader(CommandBehavior.CloseConnection);
return rd;
}
catch (Exception ex)
{
errorMessage = ex.Message;
if (logEvents)
LogEvent("ERROR", cm.CommandText + "\r\n/\r\n" + ex.ToString());
if (cn.State == ConnectionState.Open)
cn.Close();
return null;
}
}
示例4: QueryReader
/// <summary>
/// Executes a command, and returns 1 row at a time until all rows are returned
/// </summary>
/// <param name="Command">The database command to execute the reader on</param>
/// <returns></returns>
public IEnumerable<Dictionary<string, object>> QueryReader(DbCommand Command)
{
// Increase Query Count
NumQueries++;
// Execute the query
using (Command)
using (DbDataReader Reader = Command.ExecuteReader())
{
// If we have rows, add them to the list
if (Reader.HasRows)
{
// Add each row to the rows list
while (Reader.Read())
{
Dictionary<string, object> Row = new Dictionary<string, object>(Reader.FieldCount);
for (int i = 0; i < Reader.FieldCount; ++i)
Row.Add(Reader.GetName(i), Reader.GetValue(i));
yield return Row;
}
}
// Cleanup
Reader.Close();
}
}
示例5: ExecuteReader
/// <summary>
/// Executes a command, and returns the resulting rows
/// </summary>
/// <param name="Command">The database command to execute the reader on</param>
/// <returns></returns>
public List<Dictionary<string, object>> ExecuteReader(DbCommand Command)
{
// Execute the query
List<Dictionary<string, object>> Rows = new List<Dictionary<string, object>>();
DbDataReader Reader = Command.ExecuteReader();
// If we have rows, add them to the list
if (Reader.HasRows)
{
// Add each row to the rows list
while (Reader.Read())
{
Dictionary<string, object> Row = new Dictionary<string, object>(Reader.FieldCount);
for (int i = 0; i < Reader.FieldCount; ++i)
Row.Add(Reader.GetName(i), Reader.GetValue(i));
Rows.Add(Row);
}
}
// Cleanup
Reader.Close();
Reader.Dispose();
Command.Dispose();
// Return Rows
return Rows;
}
示例6: ExecuteReader
public IDataReader ExecuteReader(DbCommand command)
{
DbConnection conn = GetOpenConnection();
command.Connection = conn;
CommandBehavior commandBehavior = IsSharedConnection() ? CommandBehavior.Default : CommandBehavior.CloseConnection;
return command.ExecuteReader(commandBehavior);
}
示例7: DumpRecordsFromTable
/// <summary>
/// Dumps the result of a given SQL query into a tab separated file.
/// </summary>
/// <param name="command">The database to pull the data from.</param>
/// <param name="query">The query to select the data with.</param>
/// <param name="filename">The file to dump the results to.</param>
private void DumpRecordsFromTable(DbCommand command, string query, string filename)
{
command.CommandText = query;
using (StreamWriter writer = new StreamWriter($"migrate\\{filename}"))
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; ++i)
{
if (i != 0)
{
writer.Write('\t');
}
object val = reader.GetValue(i);
writer.Write(val);
}
writer.WriteLine();
}
}
}
示例8: GetDataReader
//metodo para retornar o leitor usado para ler os Selects usados
public static DbDataReader GetDataReader(DbCommand comando)
{
return comando.ExecuteReader();
}
示例9: Select
//Select statement
public IDataReader Select(DbCommand select_query)
{
try
{
//Open connection
if (this.OpenConnection() != null)
{
//Create a data reader and Execute the command
IDataReader data_reader = select_query.ExecuteReader();
//return data reader so programmer can read results
return data_reader;
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
return null;
}
示例10: ReadTable
public DataTable ReadTable(DbCommand cmd)
{
DataTable dt = new DataTable();
DbDataReader reader = null;
try
{
reader = cmd.ExecuteReader();
int fieldc = reader.FieldCount;
for (int i = 0; i < fieldc; i++)
{
DataColumn dc = new DataColumn(reader.GetName(i), reader.GetFieldType(i));
dt.Columns.Add(dc);
}
while (reader.Read())
{
DataRow dr = dt.NewRow();
for (int i = 0; i < fieldc; i++)
{
dr[i] = reader[i];
}
dt.Rows.Add(dr);
}
return dt;
}
finally
{
if (reader != null) reader.Close();
}
}
示例11: callStoredProcedure
private DataTable callStoredProcedure(string name, List<DbParameter> parameters)
{
DataTable results ;
command = connection.CreateCommand();
command.CommandText = name;
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddRange(parameters.ToArray());
command.Connection = connection;
if (connection.State != ConnectionState.Open) { connection.Open(); }
results = new DataTable();
results.Load(command.ExecuteReader());
return results;
}