简体   繁体   English

如何将SqlType本地转换为基础SQL类型声明?

[英]How do I natively translate SqlType to underlying SQL type declaration?

Is there a way in .NET SqlClient to translate SqlType to actual SQL type declaration, like .NET SqlClient中是否有一种方法可以将SqlType转换为实际的SQL类型声明,例如

SqlInt32  -> "int"
SqlGuid   -> "uniqueidentifier"
SqlXml    -> "xml"
SqlString -> "nvarchar(??)"

without doing it manually? 不手动做?

CLARIFICATION: I am trying to automatically refactor a number of SQL select statements, and I need to know what types do they return. 澄清:我试图自动重构一些SQL select语句,我需要知道它们返回什么类型。 I'm planning to do it by running following code: 我打算通过运行以下代码来实现:

using (var connection = new SqlConnection(SqlConnectionString))
{
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = sql;
    var reader = command.ExecuteReader(CommandBehavior.SchemaOnly);
    for (int i = 0; i < reader.FieldCount; ++i)
    {
         var type = reader.GetProviderSpecificFieldType(i);
         ... generate some sql code ...
    }
}

So, once I got the type, I'm trying to generate some SQL code and use SqlTye returned in GetProviderSpecificFieldType, and I wanted to see if there's already a function I can use that will take SqlType and give me back the SQL declaration 所以,一旦我得到了类型,我正在尝试生成一些SQL代码并使用在GetProviderSpecificFieldType中返回的SqlTye,我想看看是否已经有一个我可以使用的函数,它将采用SqlType并给我回复SQL声明

Not that I know of. 从来没听说过。 Since there is no 1:1 correspondence (for example, an SqlString can correspond to the SQL Server types char , nchar , text , ntext , nvarchar and varchar ), such a function would have to guess what the original type was, since this information is no longer available once you got the SqlString . 由于没有1:1的对应关系(例如, SqlString可以对应于SQL Server类型charnchartextntextnvarcharvarchar ),这样的函数必须猜测原始类型是什么,因为这个信息获得SqlString不再可用。

If you know that an SqlString always corresponds to an nvarchar(255) in your database, you can write such a function yourself, using this list as a starting point. 如果您知道SqlString始终对应于数据库中的nvarchar(255) ,则可以使用此列表作为起点SqlString编写此类函数。

What are you trying to achieve? 你想要实现什么目标? Maybe there is better solution to your problem... 也许有更好的解决方案来解决你的问题......

I found a solution. 我找到了解决方案。 I used GetSchemaTable in order to get the details of SQL type: 我使用GetSchemaTable来获取SQL类型的详细信息:

    private string __type(SqlDataReader reader, int i)
    {
        var schema = reader.GetSchemaTable();
        var row = (from DataRow r in schema.Rows 
                   where (int)r["ColumnOrdinal"] == i 
                   select r).First();

        var type = (string) row["DataTypeName"];
        if (type == "nvarchar" || type == "varchar" || 
            type == "nchar" || type == "char")
        {
            int maxLength = (int) row["ColumnSize"];
            if (maxLength == 2147483647) maxLength = -1;
            type += string.Format("({0})", (maxLength > 0) ? 
                        (object) maxLength : (object) "max");
        }
        return type;
    }

Not quite a total duplicate but there is the opposite direction . 不完全重复,但有相反的方向

The problem of it being a Many <-> Many mapping is exactly the same though. 它是一个很多< - >许多映射的问题是完全相同的。

Any fully automatic translation would be, at best overzealous (for example mapping any string to a TEXT column), and at worst wrong in some subtle way. 任何全自动的翻译最好是过于热心(例如将任何字符串映射到TEXT列),最糟糕的是以某种微妙的方式错误。

At the very least it would need to be Version dependent, for example should it use DateTime or DateTime2? 至少它需要依赖于版本,例如它应该使用DateTime还是DateTime2?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过protobuf-net以编程方式确定类型是否可以本机序列化? - How do I programmatically determine if a type is natively serializable by protobuf-net? 如何使用linq to sql检查底层空值? - how do I check for underlying null value using linq to sql? 如果我有对某个特定类型的未知指针引用,如何获得基础的特定类型? - If I have an unknown pointer reference to a certain type, how do I get the underlying certain type? 如何将此特定 SQL 语句转换为 LINQ - How do I translate this specifik SQL statement to LINQ 如何在 LINQ 中使用 MAX 翻译我的 SQL 查询? - How do I translate my SQL Query with Having MAX in LINQ? SQL Server XML数据类型在.NET中转换为什么?如何将其转换为XmlDocument? - What does the SQL Server XML datatype translate to in .NET and how do I convert it to XmlDocument? 如何将Binding.Path属性绑定到基础数据? - How do I bind the Binding.Path property to underlying data? 如何将此SQL语句转换为Linq-to-SQL方法? - How can I translate this SQL statement to a Linq-to-SQL approach? 如何将可为空的类型转换为其基础类型? - How to convert nullable type to its underlying type? 如何使用Linq to SQL投影到具有自定义类型的字典? - How do I project to a dictionary with a custom type using Linq to SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM