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 only 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
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:
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