So, I have the following: "SELECT * FROM MyTable;"
When I do the following I get the TableData back, which is useful, but still leaves some things unknown?
//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();
now, when iterating over my table I come across a column named "DataType" and it is either System.String or System.Byte[] or System.Int32, etc. But, this o开发者_如何转开发nly tells me the .NET type to store, it doesn't tell me if, for example, a System.Decimal
is the DbType.Currency
or DbType.Decimal
. So when I'm creating an IDataParameter, I'm not sure what to set for the DbType.
parameter.ColumnName = columnName;
parameter.DbType = DbType.Decimal; (or should it have been Currency?)
Basically, how can I get the table's real schema... or does it even matter?
If you are passing in parameters for a stored procedure, or some sql text, no you do not need to specify the parameters data type. the SqlCommand will correctly assign the data type for you.
I believe the ability to assign the DBType on a parameter is if you want to overwrite what the system would choose for you.
use the
SqlCommand.Parameters.AddWithValue("@parameterName", valueAsObject);
command
edit you are using the IDbCommand, not the SqlCommand. I know that both SqlCommand and Oracle command do not need you to specify the DbType, but I do not know if other frameworks do need you to explicitly set the DbType. here is a method to tranform a system.type to a DbType enum value:
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 hope this helps better clarify.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx
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!
Sadly, you can't do it in one line if you use IDbCommand. You can't do something like command.Parameters.Add(param).Value = thevalue;
Also, you do NOT need to set the DbType of the parameter. The correct mapping is automagically done for you :)
精彩评论