Andy
Andy

Reputation: 33

How to determine MS Access field size via OleDb

The existing application is in C#. During startup the application calls a virtual method to make changes to the database (for example a new revision may need to calculate a new field or something). An open OleDb connection is passed into the method.

I need to change a field width. The ALTER TABLE statement is working fine. But I would like to avoid executing the ALTER TABLE statement if the field is already the appropriate size. Is there a way to determine the size of an MS Access field using the same OleDb connection?

Upvotes: 1

Views: 5032

Answers (2)

Andy
Andy

Reputation: 33

Here's what I came up with based on shahkalpesh's answer:

var command = new OleDbCommand("SELECT FIELD FROM TABLE", connection); 
var reader = command.ExecuteReader(CommandBehavior.SchemaOnly); 
var schema = reader.GetSchemaTable(); 
var size = Convert.ToInt32(table.Rows[0]["ColumnSize"]);

Upvotes: 2

shahkalpesh
shahkalpesh

Reputation: 33474

Not sure if I understand your question completely.
But you could query the table for 0 rows (SELECT 1 FROM myTable WHERE 1= 0)

And you could use recordet's field collection, refer to that field by name or index and use field's property like size, type etc.

Does that help?

Upvotes: 0

Related Questions