繁体   English   中英

输入字符串的格式不正确(使用 SqlDataReader)

[英]Input string was not in a correct format (using SqlDataReader)

我正在从远程数据库获取数据,所以我实际上并不知道我正在处理的列类型。 所以我使用dataReader.GetString(X)作为默认值。 我可以将这些值保存到变量中,但是当实际尝试将变量发送到 object 构造函数时,它会失败(在进入构造函数之前)并出现错误: System.FormatException: Input string was not in a correct format. 如果我可以将它们保存到字符串类型变量,为什么在尝试使用这些变量创建 object 而不是之前会出错?

        //VARIABLES
        DateTime PreparationDate;
        string ClientId;
        string ClientName;
        string BL;
        string QTD_TOT;
        string QTD_PREP;
        string X_QTD;
        string TAXA;

            string query = GetPreparationsMACPAC;
            SqlConnection con = new SqlConnection(_connectionString);
            if (con.State == ConnectionState.Closed) con.Open();
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = cmd.ExecuteReader();

            List<PreparationViewModel> shippingsList = new List<PreparationViewModel>();

            PreparationViewModel prep = new PreparationViewModel(new DateTime(), "1", "2", "3", "4", "5", "6", "7");

            if (dr.HasRows)
            {
                while (dr.Read())
                {                    
                    PreparationDate = dr.GetDateTime(0);
                    ClientId = dr.GetString(1);
                    ClientName = dr.GetString(2);
                    BL = dr.GetString(3);
                    QTD_TOT = dr.GetString(4);
                    QTD_PREP = dr.GetString(5);
                    X_QTD = dr.GetString(6);
                    TAXA = dr.GetString(7);

                    //THE FOLLOWING OUTPUT IS CORRET (see below)
                    Console.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7}", PreparationDate, ClientId, ClientName, BL, QTD_TOT, QTD_PREP, X_QTD, TAXA);

                    //FAILS HERE
                    try
                    {
                        prep = new PreparationViewModel(PreparationDate, ClientId, ClientName, BL, QTD_TOT, QTD_PREP, X_QTD, TAXA);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                    shippingsList.Add(prep);
                }
            }

OUTPUT (1 个日期时间 + 7 个由“,”分隔的字符串)

09/04/2021 00:00:00,92843160002,RENAULT MONTAJE VALLADOLID,506653,120,120,0.000,1.0000000

CLASS MODEL

    public class PreparationViewModel
    {
        public PreparationViewModel()
        {

        }

        public PreparationViewModel(DateTime preparationDate, string clientId, string clientName, string transportDocumentId,
            string totalQuantity, string preparedQuantity, string xQuantity, string complianceRate)
        {
            //BREAKPOINT IS SET, NOT REACHED FROM THE TRY/CATCH CODE
            PreparationDate = preparationDate;
            ClientId = clientId;
            ClientName = clientName;
            TransportDocumentId = transportDocumentId;
            TotalQuantity = Int16.Parse(totalQuantity);
            PreparedQuantity = Int16.Parse(preparedQuantity);
            XQuantity = float.Parse(xQuantity);
            ComplianceRate = float.Parse(complianceRate);
        }

        public DateTime PreparationDate {get;set;}
        public string ClientId { get; set; }
        public string ClientName { get; set; }
        public string TransportDocumentId { get; set; }
        public int TotalQuantity { get; set; }
        public int PreparedQuantity { get; set; }
        public float XQuantity { get; set; }
        public float ComplianceRate { get; set; }
}

我从远程数据库获取数据,所以我不确切知道列类型

在这种情况下,您最好的选择可能是:

short prepQty = Convert.ToInt16(reader.GetValue(5), CultureInfo.InvariantCulture);

(或者如果您想要不变的文化:用您想要的任何文化替换;如果值是字符串并且您不知道文化:您已经输掉了战斗)

这将处理更多数据类型在处理字符串时具有更好定义的语义。 建议将读取/解析代码保留在处理读取器的代码中,并让PreparationViewModel只取解析值( short )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM