【问题标题】:Conversion of C# datetime to sql server datetime is throwing an error将 C# datetime 转换为 sql server datetime 会引发错误
【发布时间】:2017-01-27 16:57:06
【问题描述】:

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

错误:将 nvarchar 数据类型转换为 datetime 数据类型 导致超出范围的值。表值参数的数据 @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;
    }

【问题讨论】:

  • 您没有将其作为 datetime 传递,而是将其作为 string 传递。您需要发布在 C# 中配置参数值的代码,包括该参数及其类型。如果您将它作为 datetime 传递,则不会进行转换,因此您遇到转换错误这一事实意味着您并没有真正按照您的想法或所说的去做。跨度>
  • SQL server 和你的服务器/机器的区域设置是否相同?
  • @Lasse V. Karlsen,有一个 List,Class1 有许多属性,其中 datetime 是属性之一。有一种通用方法可以将项目列表转换为数据表。没有发生显式的数据类型转换。
  • @Vijayakrishna,是一样的。
  • 有一个通用方法可以将项目列表转换为数据表:显示代码,至少是处理日期时间值的部分...

标签: c# sql-server-2012


【解决方案1】:

您的代码 - 就像现在一样 - 将在字符串级别传输任何值。这是一个非常糟糕的方法。发生的隐式转换很大程度上取决于您的系统设置(语言和文化)。最糟糕的部分是:当您测试它时,这可能在您的机器上运行良好,但在客户的系统上它会因奇怪的消息而中断。调试愉快:-(

像这样更改你的代码

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

【讨论】:

  • dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);这非常有效。谢谢!
【解决方案2】:

去掉引号

“@UttParameter”

@UttParameter

【讨论】:

  • 这根本不是问题。实际问题出在从日期时间隐式转换为字符串的数据类型上。
【解决方案3】:

您正在使用 InvariantCulture 作为 DataTable 语言环境。不变文化期望 Date 为 yyyy-MM-dd 格式。

【讨论】:

  • 问题出在类型转换上。在创建数据表时,我必须将类型指定为DateTime。现在,泛型方法已被修改以处理可为空的数据类型。正如您在评论中所问的那样,我以为您在询问数据库服务器和应用程序服务器的时区。后来发现有人修改了数据库服务器的日期格式。
猜你喜欢
  • 2017-02-07
  • 2023-04-02
  • 2019-09-02
  • 2013-11-29
  • 1970-01-01
  • 2017-04-14
  • 2019-12-26
  • 2019-05-20
  • 2014-01-08
相关资源
最近更新 更多