ODBC连接到SQL Server 2016,错误为未知数据类型-155 [英] ODBC connect to SQL Server 2016 with error Unknown data type -155

查看:176
本文介绍了ODBC连接到SQL Server 2016,错误为未知数据类型-155的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个网站上进行了搜索,发现相似的问题,但使用python 而不是C#.此外,该文章中的解决方法(避免使用DateTimeOffset数据类型查询列)对我没有帮助,因为我的应用程序略有不同.我的MVC Web应用程序需要动态列出SQL Server 2016数据库中的所有表.然后,用户可以从该列表中选择1个表,并在网格中显示所有列及其值. 我在

I've searched throughout the site and found the similar issue but with python not C#. Moreover, the workaround in that post (to avoid querying columns with DateTimeOffset datatype) did not help me since my application is slightly different. My MVC web application needs to list all tables in a SQL Server 2016 database dynamically. Then users can select 1 table from that list and display all columns and their values in a grid. I'm using ODBC with

driver = {ODBC Driver 13 for SQL Server}

连接到SQL Server 2016数据库.与数据库的连接成功.尽管

to connect to SQL Server 2016 database. The connection to the database is successful. Although this article from Microsoft mentions data type support for ODBC Date and Time enhancement, specially for DataTimeOffset, I cannot access a table with a column in DateTimeOffSet datatype. The following code returns an error message "Unknown data type --155".

OdbcDataAdapter adapter = new OdbcDataAdapter(queryString, con);

你有什么主意吗?

谢谢.

推荐答案

不要使用ODBC.将System.Data.SqlClient中的类用于SQL Server和 ODP.NET 对于Oracle.这两个命名空间中的类都实现了System.Data命名空间中的相应接口-因此您可以使用它们相同-所有您需要的是一个简单的工厂,该工厂将返回所需的任何接口的SQLClient实现或ODP.NET实现.与之合作-像这样:

Don't use ODBC. Use the classes in System.Data.SqlClient for SQL Server and ODP.NET for Oracle. The classes in both namespaces implements the corresponding interfaces in the System.Data namespace - so you can work with them the same - all you need is one simple factory that will return either the SQLClient implementation or the ODP.NET implementation of whatever interface you need to work with - something like this:

public enum rdbmsTypes
{
    SQLServer,
    Oracle
}

public class ADONetFactory
{
    private rdbmsTypes _dbType;
    private string _connectionString;
    public ADONetFactory (rdbmsTypes dbType, string connectionString)
    {
        _dbType = dbType;
        _connectionString = connectionString;
    }

    public System.Data.IDbConnection GetConnecion()
    {
        switch(_dbType)
        {
            case rdbmsTypes.SQLServer:
                return new System.Data.SqlClient.SqlConnection(_connectionString);
            case rdbmsTypes.Oracle:
                return new Oracle.DataAccess.Client.OracleConnection(_connectionString);
        }
        ThrowNotSupportedException();
    }

    public System.Data.IDbCommand GetCommand()
    {
        switch (_dbType)
        {
            case rdbmsTypes.SQLServer:
                return new System.Data.SqlClient.SqlCommand();
            case rdbmsTypes.Oracle:
                return new Oracle.DataAccess.Client.OracleCommand();
        }
        ThrowNotSupportedException();
    }

    private void ThrowNotSupportedException()
    {
        throw new NotSupportedException("The RDBMS type " + Enum.GetName(typeof(rdbmsTypes), _dbType) + " is not supported"); 
    }
}

那么您应该已经内置了对两个数据库中所有数据类型的支持.

Then you should have built in support for all data types in both databases.

这篇关于ODBC连接到SQL Server 2016,错误为未知数据类型-155的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆