6

SQL Server/T-SQL データ型を含む文字列が与えられた場合、その文字列を .Net 型に評価する最も簡単な方法は何ですか?

たとえば、「nvarchar」を含む文字列がある場合、変換メソッドによって返される結果はSystem.StringType である必要があります。「 int 」を含む文字列がある場合、結果はSystem.Int32Type オブジェクトになるはずです。

SQL データ型文字列を受け取り、.Net Type オブジェクトを返す switch/case ステートメントを介して文字列を送信する関数を簡単に作成できます。しかし、私が見落としていた .Net フレームワークに埋め込まれていて、既にこれを行っている関数があるかどうかはわかりませんでした。

SQL Server データ型を .Net データ型に解決する最も簡単で正しい方法は何ですか?

追加のコンテキスト

私の場合、実際には、データに関するメタ情報を返すストアド プロシージャがあります。具体的には、SQL Server 2005 内で使用可能な任意の sql-type である可能性のある sql-type 値を含む文字列フィールドが返されます。

int私のストアド プロシージャは、smallintdatetime、 、 などの sql-type を返す可能性があります。binaryこのデータ型を取得して、.NetTypeオブジェクトに変換する必要があります。

以下の Matthew のコメントは、Microsoft のドキュメントから直接、必要なすべてのマッピング情報を提供しますが、繰り返しになりますが、System.DataまたはSystem.Data.SqlClient名前空間のいずれかに統合されたものがあるかどうか疑問に思っていました。

4

2 に答える 2

2

私が知っている露出は何もありません。System.Data.SqlClient コードの奥深くには、型マッピングを決定するために使用される次の関数があります。

internal Type GetTypeFromStorageType(bool isSqlType)
{
    if (isSqlType)
    {
        switch (this._type)
        {
            case StorageType.Empty:
                return null;

            case StorageType.Boolean:
                return typeof(SqlBoolean);

            case StorageType.Byte:
                return typeof(SqlByte);

            case StorageType.DateTime:
                return typeof(SqlDateTime);

            case StorageType.Decimal:
                return typeof(SqlDecimal);

            case StorageType.Double:
                return typeof(SqlDouble);

            case StorageType.Int16:
                return typeof(SqlInt16);

            case StorageType.Int32:
                return typeof(SqlInt32);

            case StorageType.Int64:
                return typeof(SqlInt64);

            case StorageType.Money:
                return typeof(SqlMoney);

            case StorageType.Single:
                return typeof(SqlSingle);

            case StorageType.String:
                return typeof(SqlString);

            case StorageType.SqlBinary:
                return typeof(object);

            case StorageType.SqlCachedBuffer:
                return typeof(SqlString);

            case StorageType.SqlGuid:
                return typeof(object);

            case StorageType.SqlXml:
                return typeof(SqlXml);
        }
    }
    else
    {
        switch (this._type)
        {
            case StorageType.Empty:
                return null;

            case StorageType.Boolean:
                return typeof(bool);

            case StorageType.Byte:
                return typeof(byte);

            case StorageType.DateTime:
                return typeof(DateTime);

            case StorageType.Decimal:
                return typeof(decimal);

            case StorageType.Double:
                return typeof(double);

            case StorageType.Int16:
                return typeof(short);

            case StorageType.Int32:
                return typeof(int);

            case StorageType.Int64:
                return typeof(long);

            case StorageType.Money:
                return typeof(decimal);

            case StorageType.Single:
                return typeof(float);

            case StorageType.String:
                return typeof(string);

            case StorageType.SqlBinary:
                return typeof(byte[]);

            case StorageType.SqlCachedBuffer:
                return typeof(string);

            case StorageType.SqlGuid:
                return typeof(Guid);

            case StorageType.SqlXml:
                return typeof(string);
        }
    }
    return null;
}
于 2013-08-13T20:01:38.530 に答える