簡體   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