Tak for hjælpen. Så godt så vidt.
Her får jeg så en IDataReader retur. Dette vil jeg gerne have lavet om til DataTable. Hertil har jeg fundet noget kode:
--------------------------------------------
using System;
using System.Data;
using System.Data.Common ;
namespace DataUtils
{
public class DataReaderAdapter : DbDataAdapter
{
public int FillFromReader(DataTable dataTable, IDataReader dataReader)
{
return this.Fill(dataTable, dataReader);
}
protected override RowUpdatedEventArgs CreateRowUpdatedEvent(
DataRow dataRow,
IDbCommand command,
StatementType statementType,
DataTableMapping tableMapping
){return null;}
protected override RowUpdatingEventArgs CreateRowUpdatingEvent(
DataRow dataRow,
IDbCommand command,
StatementType statementType,
DataTableMapping tableMapping
){return null;}
protected override void OnRowUpdated(
RowUpdatedEventArgs value
){}
protected override void OnRowUpdating(
RowUpdatingEventArgs value
){}
}
}
With this complete, here is some console app code to illustrate its use:
using System;
using System.Data;
using System.Data.SqlClient;
namespace DataReaderAdapter
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
SqlConnection cn = new SqlConnection("Server=(local);database=Northwind;user id=sa;password=;");
string sql ="Select * from Employees";
SqlCommand cmd= new SqlCommand(sql,cn);
cn.Open();
SqlDataReader dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataUtils.DataReaderAdapter dra = new DataUtils.DataReaderAdapter();
DataTable dt = new DataTable();
dra.FillFromReader(dt,dr);
dr.Close();
for(int i =0;i<dt.Rows.Count;i++)
Console.WriteLine(dt.Rows[i][1].ToString());
}
}
}
--------------------------------------------
(kilde:
http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=628)