将IDataReader转换为DataTable [英] Convert IDataReader to DataTable

查看:101
本文介绍了将IDataReader转换为DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Heloo,



我有两类课。



DatabaseFactory.cs



Heloo,

I have tow class.

DatabaseFactory.cs

....partial code

internal static Contract LoadContract(int contractId)
        {
            IDataReader reader = ExecuteSQL("select * from [Contracts] where ([ContractId] = @ContractId)", new IDbDataParameter[] { CreateParameter("@ContractId", OleDbType.BigInt, contractId) });
            if (!reader.Read())
            {
                throw new Exception(string.Format("Contractul {0} nu a fost gasit.", contractId));
            }
            return new Contract(reader);
        }

.....





和2级。



Contract.cs

部分代码..



and class 2.

Contract.cs
partial code..

public class Contract
    {
        private int clientId;
        private int contractNumber;
        private DateTime createDate;
        private DateTime endDate;
        private decimal procent;
        private DateTime startDate;

        public Contract()
        {
            this.startDate = DateTime.Now;
            this.procent = Settings.Default.DefaultProcent;
        }

        public Contract(IDataReader reader)
        {
            this.startDate = DateTime.Now;
            this.procent = Settings.Default.DefaultProcent;
            this.contractNumber = (int) reader["ContractId"];
            this.clientId = (int) reader["ClientId"];
            this.createDate = (DateTime) reader["CreateDate"];
            this.startDate = (DateTime) reader["StartDate"];
            this.endDate = (DateTime) reader["EndDate"];
            this.procent = (decimal) reader["Procent"];
        }

        public int ClientId
        {
            get
            {
                return this.clientId;
            }
            set
            {
                this.clientId = value;
            }
        }





我使用DataTable代替IdataReader。



我将DatabaseFactory.cs修改为:





I what to use DataTable instead of IdataReader.

I modifii the DatabaseFactory.cs to:

internal static Contract LoadContract(int contractId)
        {
            Program.Connection.CommandText = "select * from Contracts where ContractId=@ContractId";
            Program.Connection.AddParameter("@ContractId", contractId);

            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);

            if (Table.Rows.Count > 0)
            {
                throw new Exception(string.Format("Contractul {0} nu a fost gasit.", contractId));
            }
            return Table;
        }







我不知道如何修改第二堂课。我需要从我的数据库中返回一个ID。




and i don''t know how to modifing the second class. I need to return an ID from my database.

推荐答案

请通过此链接

http://amitchandnz.wordpress.com/2011/09/28/load-idatareader-into-datatable/ [ ^ ]



希望这有助于
Please go through this link
http://amitchandnz.wordpress.com/2011/09/28/load-idatareader-into-datatable/[^]

Hope this helps


这篇关于将IDataReader转换为DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆