SqlDataReader返回错误的字段值

时间:2016-01-21 21:23:44

标签: c# sql-server wpf

我遇到了SqlDataReader的问题。我有这样的功能

public static ObservableCollection<DelayedShipmentDTO> getAllDelayShipmentsInfo()
{
    ObservableCollection<DelayedShipmentDTO> retlist = new ObservableCollection<DelayedShipmentDTO>();

    SqlConnection conn = null;
    SqlCommand command = null;

    try
    {
        conn = new SqlConnection(connectionString);
        conn.Open();

        command = new SqlCommand("Select * from Delay", conn);

        SqlDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            DelayedShipmentDTO dto = new DelayedShipmentDTO();
            dto.setPromiseddate((DateTime)reader["PromisedDate"]);
            dto.setNewShipmentdate((DateTime)reader["NewShipmentDate"]);
            dto.setOrdernumber(reader["OrderNumber"].ToString());
            dto.setDelaytype(reader["DelayType"].ToString());
            dto.setDelayreason(reader["DelayReason"].ToString());
            dto.setIsnotified((bool)reader["isnotified"]);
            dto.setNotificationtype(reader["NotificationType"].ToString());

            //DelayedShipmentDTO dto = new DelayedShipmentDTO((DateTime)reader["PromisedDate"], (DateTime)reader["NewShipmentDate"], reader["OrderNumber"].ToString(), reader["NotificationType"].ToString(),
            //    reader["DelayType"].ToString(), reader["DelayReason"].ToString(), (bool)reader["isnotified"]);

            retlist.Add(dto);
        }

        return retlist;
    }
    catch (Exception ex) 
    { 
        throw new Exception(ex.Message); 
    }
    finally 
    {   
        if (conn != null) 
        { 
            ((IDisposable)conn).Dispose(); 
        } 
    }
}

public class DelayedShipmentDTO
{
    DateTime promisedDate;
    DateTime newShipmentDate;
    string orderNumber;
    string notificationType;
    string delayType;
    string delayReason;
    bool isNotified;
public DelayedShipmentDTO() { }
    public DelayedShipmentDTO(DateTime p, DateTime n, string o, string sn, string dt, string dr, bool i)
    {
        promisedDate = p;
        newShipmentDate = n;
        orderNumber = o;
        notificationType = sn;
        delayType = dt;
        delayReason = dr;
        isNotified = i;
    }

    public void setPromiseddate(DateTime value) { promisedDate = value; }
    public void setNewShipmentdate(DateTime value) { newShipmentDate = value; }
    public void setOrdernumber(string value) { orderNumber = value; }
    public void setNotificationtype(string value) { notificationType = value; }
    public void setDelaytype(string value){delayType = value;}
    public void setDelayreason(string value) { delayReason = value; }
    public void setIsnotified(bool value) { isNotified = value; }
}

我的SQL Server表设置如下

CREATE TABLE [dbo].[Delay]
(
    [DelayID] [int] IDENTITY(1,1) NOT NULL,
    [PromisedDate] [date] NOT NULL,
    [NewShipmentDate] [date] NOT NULL,
    [Ordernumber] [nvarchar](50) NOT NULL,
    [DelayType] [nvarchar](50) NOT NULL,
    [DelayReason] [nvarchar](50) NOT NULL,
    [isNotified] [bit] NOT NULL,
    [NotificationType] [nvarchar](50) NOT NULL
) ON [PRIMARY]

问题是,当我读取每一行时,所有字符串都默认为ordernumber字段值。布尔值和日期都没有问题。

编辑1:澄清方法的结果

PromisedDate = 1/21/2016  - correct
NewShipmentDate = 1/22/2016 - correct
isNotified = true; - correct
OrderNumber = "123456" -correct
DelayType = "123456" - wrong 
DelayReason = "123456" - wrong 
NotificationType = "123456" - wrong
编辑2:发现了错误! 我在使用参数的addRecord上犯了一个错误。感谢那些试图提供帮助的人。

1 个答案:

答案 0 :(得分:0)

以下是我认为您尝试对所显示的代码执行的操作:

while (reader.Read())
            {
                DelayedShipmentDTO dto = new DelayedShipmentDTO((DateTime)reader["PromisedDate"],
                (DateTime)reader["NewShipmentDate"],
                reader["OrderNumber"].ToString(),
                reader["DelayType"].ToString(),
                reader["DelayReason"].ToString(),
                reader["NotificationType"].ToString(),
                (bool)reader["isnotified"]);

                //DelayedShipmentDTO dto = new DelayedShipmentDTO((DateTime)reader["PromisedDate"], (DateTime)reader["NewShipmentDate"], reader["OrderNumber"].ToString(), reader["NotificationType"].ToString(),
                //    reader["DelayType"].ToString(), reader["DelayReason"].ToString(), (bool)reader["isnotified"]);

                retlist.Add(dto);
            }

注意在实例化类时我是如何将值传递给DelayedShipmentDTO方法的?在命名约定和学习如何调试时,请接受其他人的建议,因为这段代码会很快失控。