数据库
首页 > 数据库> > C#日期时间到SQL Server日期时间的转换引发错误

C#日期时间到SQL Server日期时间的转换引发错误

作者:互联网

在C#中,值{27-01-2017 12.00.00 AM}的DateTime属性在数据表中传递给具有UTT参数的过程. UTT也具有相同的数据类型datetime.我正在使用下面提供的通用方法.我无法显式转换数据类型.

Error : The conversion of a nvarchar data type to a datetime data type
resulted in an out-of-range value. The data for table-valued parameter
@UttParameter doesn’t conform to the table type of the parameter.
SQL Server error is: 242, state: 3
The statement has been terminated.

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;
    }

解决方法:

您的代码(现在是这样)将在字符串级别传输任何值.这是一个非常糟糕的方法.发生的隐式转换在很大程度上取决于系统的设置(语言和区域性).最糟糕的部分是:这可能在您测试机器时对您的机器非常有用,但是在客户系统上,它会被奇怪的消息破坏.快乐调试:-(

像这样更改代码

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);
}

这将添加具有正确数据类型的列-即使这是可为空的类型.

学分:This answer helped me

更新更简单

(在链接答案下方的评论中,向Yves M.致谢)

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

标签:sql-server-2012,c
来源: https://codeday.me/bug/20191111/2022349.html