views:

1279

answers:

4

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?

A: 

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?

shahkalpesh
Here is what I came up with: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"]);
Andy
But how would you know if it's a fixed width NCHAR, variable width NVARCHAR or blob MEMO? Does it have the WITH COMPRESSION option? Better to go with the OpenSchema method (+ ADOX if necessary) IMO: you get more/granular information.
onedaywhen
The schema table had the dbtype, but I did not need it. I know the field type in this case...I just need the current field size and prefer to use the existing OleDb connection. OpenSchema may be a better solution for a different problem.
Andy
+2  A: 

It is possible to use schemas. Here are a few notes.

Sub ListFieldDescriptions()

Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset

    Set cn = CurrentProject.Connection

    Set rs = cn.OpenSchema(adSchemaColumns, _
            Array(Empty, Empty, "TableNameHere"))
    While Not rs.EOF
        Debug.Print "     " & rs!Column_Name
        Debug.Print "     " & rs!CHARACTER_MAXIMUM_LENGTH
        Debug.Print "     " & rs!Data_Type

        rs.MoveNext
    Wend

    Set cn = Nothing

End Sub

Note: adSchemaColumns=4, Data_Type adWChar=130

Based on: http://wiki.lessthandot.com/index.php/ADO_Schemas

Remou
A: 

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"]);
Andy
Remou's answer is actually superior as it doesn't require opening a recordset, and you're using the tools provided in ADO for querying the data dictionary.
David-W-Fenton
Are you sure? Remou's answer is walking a recordset (While Not rs.EOF ... rs.MoveNext). Note that I only needed size and preferred to use existing connection. Remou's answer may be a better answer to a different problem.
Andy
A: 

Andy's works better for me... have to use OleDB for my implementation. And his is only grabbing schema which is much cleaner.

Jeff