2

だから、私は次のものを持っています:"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タイプのみを示しています。たとえば、aSystem.DecimalがまたはであるDbType.CurrencyかどうかはわかりませんDbType.Decimal。そのため、IDataParameterを作成しているときに、DbTypeに何を設定すればよいかわかりません。

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

基本的に、テーブルの実際のスキーマを取得するにはどうすればよいですか...それとも重要ですか?

4

2 に答える 2

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を使用すると、1行でそれを行うことはできません。あなたは次のようなことはできませんcommand.Parameters.Add(param).Value = thevalue;

また、パラメータのDbTypeを設定する必要はありません。正しいマッピングが自動的に行われます:)

于 2011-06-14T14:12:33.013 に答える
1

ストアドプロシージャのパラメータまたはSQLテキストを渡す場合は、パラメータのデータ型を指定する必要はありません。SqlCommandは、データ型を正しく割り当てます。

パラメータにDBTypeを割り当てる機能は、システムが選択するものを上書きしたい場合にあると思います。

使用

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

指図

SqlCommandではなくIDbCommandを使用して編集します。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

于 2011-06-14T13:49:12.717 に答える