亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 DataRowExtension 解析 TimeSpan

使用 DataRowExtension 解析 TimeSpan

C#
慕妹3146593 2022-06-12 10:26:15
我想使用 DataRowExtension 將 DataRow 中的字段值作為 TimeSpan(格式如 mm:ss),但它給了我 System.InvalidCastException,如下所示var time = staffItems.Rows[0].Field<TimeSpan>("TIME_DURATION"); // System.InvalidCastException但是當將此值作為字符串并在 Parse to TimeSpan 之后沒有問題發生。var time = staffItems.Rows[0].Field<string>("TIME_DURATION"); // time : 0:43var time2 = TimeSpan.Parse(time); // time2 : 00:43:00問題是,我如何在沒有任何額外解析或強制轉換的情況下使用 DataRowExtension 來做到這一點。
查看完整描述

3 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

可能 TIME_DURATION 字段來自 DataTable 的 vharchar 或其他內容。它必須等同于 TimeSpan。



查看完整回答
反對 回復 2022-06-12
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

首先你必須解析然后獲取時間部分


var time = TimeSpan.Parse(staffItems.Rows[0]["TIME_DURATION"]);

var time2 = time.ToString(@"mm\:ss");

如果你想從 datarowextension 獲得它。您必須創建數據表并為“TIME_DURATION”列指定時間對象。您可以通過以下方法做到這一點:


using System;

using System.Data;


class Program

{

    static void Main()

    {

        //

        // Loop over DataTable rows and call the Field extension method.

        //

        foreach (DataRow row in GetTable().Rows)

        {

            // Get first field by column index.

            int weight = row.Field<int>(0);


            // Get second field by column name.

            string name = row.Field<string>("Name");


            // Get third field by column index.

            string code = row.Field<string>(2);


            // Get fourth field by column name.

            DateTime date = row.Field<DateTime>("Date");


            // Display the fields.

            Console.WriteLine("{0} {1} {2} {3}", weight, name, code, date);

        }

    }


    static DataTable GetTable()

    {

        DataTable table = new DataTable(); // Create DataTable

        table.Columns.Add("Weight", typeof(int)); // Add four columns

        table.Columns.Add("Name", typeof(string));

        table.Columns.Add("Code", typeof(string));

        table.Columns.Add("Date", typeof(DateTime));

        table.Rows.Add(57, "Koko", "A", DateTime.Now); // Add five rows

        table.Rows.Add(130, "Fido", "B", DateTime.Now);

        table.Rows.Add(92, "Alex", "C", DateTime.Now);

        table.Rows.Add(25, "Charles", "D", DateTime.Now);

        table.Rows.Add(7, "Candy", "E", DateTime.Now);

        return table;

    }

}


查看完整回答
反對 回復 2022-06-12
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

下面的方法將 a 解析string為擴展方法TimeSpan中的 a 。DataRow


public static TimeSpan ExtractTimeData(this DataRow row, string column)

{

    // check column exists in dataTable

    var exists = row.Table.Columns.Contains(column);


    if (exists)

    {

        // ensure we're not trying to parse null value

        if (row[column] != DBNull.Value)

        {

            TimeSpan time;


            if (TimeSpan.TryParse(row[column].ToString(), out time))

            {

                // return if we can parse to TimeSpan

                return time;

            }

        }

    }


    // return default TimeSpan if there is an error

    return default(TimeSpan);

}

你可以像這樣使用它:


TimeSpan time = row.ExtractTimeData("TIME_DURATION");

string timeString = time.ToString(@"h\:mm");


查看完整回答
反對 回復 2022-06-12
  • 3 回答
  • 0 關注
  • 152 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號