簡體   English   中英

將C#datetime轉換為sql server datetime會引發錯誤

[英]Conversion of C# datetime to sql server datetime is throwing an error

在C#中,值為{27-01-2017 12.00.00 AM}的DateTime屬性在數據表中傳遞給具有UTT參數的過程。 UTT也具有相同的數據類型datetime。 我正在使用下面提供的通用方法。 我無法顯式轉換數據類型。

錯誤:將nvarchar數據類型轉換為日期時間數據類型會導致超出范圍的值。 表值參數@UttParameter的數據不符合參數的表類型。
SQL Server錯誤是:242,狀態:3
該語句已終止。

public static DataTable ToDataTable<T>(IList<T> items, bool usePropertyMappingName = false)
    {
        DataTable dataTable = null;

        if (items != null)
        {
            using (dataTable = new DataTable(typeof(T).Name))
            {
                dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;

                // Get all the properties.
                PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

                foreach (PropertyInfo prop in props)
                {
                    string columnName = prop.Name;

                    if (usePropertyMappingName)
                    {
                        var mappingAttribute = prop.GetCustomAttributes(typeof(PropertyMappingAttribute), true).FirstOrDefault() as PropertyMappingAttribute;

                        if (mappingAttribute != null && !string.IsNullOrEmpty(mappingAttribute.Name))
                        {
                            columnName = mappingAttribute.Name;
                        }
                    }

                    // Setting column names as Property names.
                    dataTable.Columns.Add(columnName, prop.PropertyType);
                }

                foreach (T item in items)
                {
                    var values = new object[props.Length];
                    for (int i = 0; i < props.Length; i++)
                    {
                        // Inserting property values to data table rows.
                        values[i] = props[i].GetValue(item, null);
                    }

                    dataTable.Rows.Add(values);
                }
            }
        }

        return dataTable;
    }

刪除引號

“@UttParameter”

@UttParameter

您正在使用InvariantCulture作為DataTable語言環境。 不變文化期望Date采用yyyy-MM-dd格式。

您的代碼 - 就像現在一樣 - 將在字符串級別傳輸任何值 這是一個非常糟糕的方法 發生的隱式轉換在很大程度上取決於系統的設置(語言和文化)。 最糟糕的部分是:在您測試它時,這可能在您的計算機上運行良好,但在客戶的系統上,它會打破奇怪的消息。 快樂調試:-(

像這樣更改你的代碼

foreach (PropertyInfo prop in props) {
    // Setting column names as Property names.
    if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
        dataTable.Columns.Add(prop.Name, prop.PropertyType.GetGenericArguments()[0]);
    else
        dataTable.Columns.Add(prop.Name, prop.PropertyType);
}

這將添加列 - 即使這是一個可空類型 - 具有正確的數據類型。

學分: 這個答案對我有幫助

更新更簡單

(請在鏈接答案下方的評論中向Yves M.發送)

foreach (PropertyInfo prop in props) {
    // Setting column names as Property names.
        dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM