|
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace SqlProc
{
/// <summary>
/// Represents a single output column in the returned dataset from a stored procedure command.
/// Also provides a static method to discover the output columns from a database.
/// </summary>
public class SqlOutputField
{
#region Static methods
/// <summary>
/// Returns an array of all output fields from this procedure.
/// If the stored procedure does not return any data then an empty array is returned.
/// </summary>
/// <param name="connectionString"></param>
/// <param name="procedure"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static SqlOutputField[] GetOutputFields(string connectionString, string procedure, SqlParameter[] parameters)
{
SqlOutputField[] fields = new SqlOutputField[] {};
try
{
fields = GetOutputFieldsFromDatabase(connectionString, procedure, parameters);
}
catch
{
}
return fields;
}
/// <summary>
/// Returns an array of all output fields from this procedure.
/// If the stored procedure does not return any data then an exception is thrown.
/// </summary>
/// <param name="connectionString"></param>
/// <param name="procedure"></param>
/// <param name="parameters"></param>
/// <returns></returns>
private static SqlOutputField[] GetOutputFieldsFromDatabase(string connectionString, string procedure, SqlParameter[] parameters)
{
ArrayList fields = new ArrayList();
using (SqlDataReader reader = SQLHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, procedure, parameters))
{
for(int i = 0; i < reader.FieldCount; i++)
{
string clrType = reader.GetFieldType(i).Name;
string sqlType = reader.GetDataTypeName(i);
string name = reader.GetName(i);
fields.Add(new SqlOutputField(name, i, sqlType, clrType, SqlTypeMapper.GetCSharpTypeName(clrType)));
}
}
SqlOutputField[] farray = (SqlOutputField[])fields.ToArray(typeof(SqlOutputField));
return farray;
}
#endregion
#region Constructor
public SqlOutputField(string field, int ordinal, string sqlType, string clrType, string csharpType)
{
Fieldname = field;
Ordinal = ordinal;
SqlTypeName = sqlType;
CLRTypeName = clrType;
CSharpTypeName = csharpType;
}
#endregion
#region Properties
public string Fieldname = string.Empty;
public int Ordinal = 0;
/// <summary>
/// Returns the Sql type name as determined by the SqlReader.
/// </summary>
public string SqlTypeName = string.Empty;
/// <summary>
/// Returns the CLR type name as determined by the SqlReader.
/// </summary>
public string CLRTypeName = string.Empty;
/// <summary>
/// Returns the C# type name that corresponds to the CLR type of this field.
/// </summary>
public string CSharpTypeName = string.Empty;
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.