问题
I have a datatable that I'm filling with SqlDataAdapter.Fill(). Is there a way to dynamically assign the MaxLength
property to each string datacolumn to the maximum allowed by that column in the database?
回答1:
So I googled for a while and found that the DataAdapter has another method besides Fill
which is: FillSchema(), which includes the database schema into the DataTable
. Since all I wanted to bring was the MaxLength property what I did was load the schema into another table and loop through the columns assigning the value to each one.
Of course if you wanted to keep all the constraints in the database within your table, then you'd need no loop and just load the schema into the datatable.
来源:https://stackoverflow.com/questions/6859615/dynamically-setting-maxlength-property-of-a-datacolumn-with-dataadapter