C#:映射IDataReader.GetTableSchema()和DbType枚举?

时间:2011-06-14 13:34:11

标签: c# database database-schema query-parameters dbtype

所以,我有以下内容:"SELECT * FROM MyTable;"

当我执行以下操作时,我得到了TableData,这很有用,但仍有一些未知的东西?

//CommandBehavior.KeyInfo seems to actually return the correct primary keys
// not so much with CommandBehavior.SchemaOnly.
IDataReader reader = command.ExecuteReader(CommandBehavior.KeyInfo)

DataTable table = reader.GetSchemaTable();

现在,当迭代我的表时,我遇到一个名为“DataType”的列,它是System.String或System.Byte []或System.Int32等。但是,这只告诉我.NET类型为例如,System.DecimalDbType.Currency还是DbType.Decimal,它不会告诉我。因此,当我创建IDataParameter时,我不确定要为DbType设置什么。

parameter.ColumnName = columnName;
parameter.DbType = DbType.Decimal; (or should it have been Currency?)

基本上,我怎样才能获得表格的真实模式......或者它是否重要?

2 个答案:

答案 0 :(得分:1)

如果要传入存储过程的参数或某些sql文本,则不需要指定参数数据类型。 SqlCommand将为您正确分配数据类型。

我相信能够在参数上分配DBType是否要覆盖系统为您选择的内容。

使用

SqlCommand.Parameters.AddWithValue("@parameterName", valueAsObject);

命令

编辑您使用的是IDbCommand,而不是SqlCommand。我知道SqlCommand和Oracle命令都不需要你指定DbType,但我不知道其他框架是否确实需要你显式设置DbType。这是一个将system.type转换为DbType枚举值的方法:

Class DBTypeConversion
{
    private static String[,] DBTypeConversionKey = new String[,] 
    {
     {"BigInt","System.Int64"},
     {"Binary","System.Byte[]"},
     {"Bit","System.Boolean"},
     {"Char","System.String"},
     {"DateTime","System.DateTime"},
     {"Decimal","System.Decimal"},
     {"Float","System.Double"},
     {"Image","System.Byte[]"},
     {"Int","System.Int32"},
     {"Money","System.Decimal"},
     {"NChar","System.String"},
     {"NText","System.String"},
     {"NVarChar","System.String"},
     {"Real","System.Single"},
     {"SmallDateTime","System.DateTime"},
     {"SmallInt","System.Int16"},
     {"SmallMoney","System.Decimal"},
     {"Text","System.String"},
     {"Timestamp","System.DateTime"},
     {"TinyInt","System.Byte"},
     {"UniqueIdentifer","System.Guid"},
     {"VarBinary","System.Byte[]"},
     {"VarChar","System.String"},
     {"Variant","System.Object"}
    };


    public static SqlDbType SystemTypeToDbType( System.Type sourceType )
    {
    SqlDbType result;
    String SystemType = sourceType.ToString();
    String DBType = String.Empty;
    int keyCount = DBTypeConversionKey.GetLength(0);

    for(int i=0;i<keyCount;i++)
    {
    if(DBTypeConversionKey[i,1].Equals(SystemType)) DBType = DBTypeConversionKey[i,0];
    }

    if (DBType==String.Empty) DBType = "Variant";

    result = (SqlDbType)Enum.Parse(typeof(SqlDbType), DBType);

    return result;
    }

    public static Type DbTypeToSystemType( SqlDbType sourceType )
    {
    Type result;
    String SystemType = String.Empty;
    String DBType = sourceType.ToString();
    int keyCount = DBTypeConversionKey.GetLength(0);

    for(int i=0;i<keyCount;i++)
    {
    if(DBTypeConversionKey[i,0].Equals(DBType)) SystemType = DBTypeConversionKey[i,1];
    }

    if (SystemType==String.Empty) SystemType = "System.Object";

    result = Type.GetType(SystemType);

    return result;
    }

http://social.msdn.microsoft.com/Forums/en/winforms/thread/c6f3ab91-2198-402a-9a18-66ce442333a6 希望这有助于更好地澄清。

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx

答案 1 :(得分:1)

IDbCommand command = GetCommand(); //However you want to implement it.

IDbDataParameter param = command.CreateParameter();
//Or some other method that returns a parameter.

command.Parameters.Add(param);
param.Value = thevalue; //You're value here!

遗憾的是,如果使用IDbCommand,则无法在一行中执行此操作。你不能做command.Parameters.Add(param).Value = thevalue;

之类的事情

另外, NOT 需要设置参数的DbType。为您自动完成正确的映射:)