c# - Use DataTable.Load() without creating all column properties -
i'm loading data sql server database this:
sqlcommand cmd = new sqlcommand("select * x", conn); cmd.commandtype = commandtype.text; datatable dt = new datatable(); sqldatareader rd = cmd.executereader(); dt.load(rd, loadoption.preservechanges); issue:
the load() function initializes table columns columnname , datatype, looks deeper database, , adds constraints allowdbnull, autoincrement, maxlength, etc.
however, leads problems in application, because want further process data internally.
so, possible load() setting basic properties (which come directly select statement), without setting allowdbnull, maxlength, , on? or need clean these values after load() ? or there alternative calling load() ?
if don't want behaviour don't use datatable.load dbdataadapter.fill(datatable):
datatable dt = new datatable(); using(var da = new sqldataadapter("select * x", conn)) da.fill(dt); the
filloperation adds rows destinationdatatableobjects indataset, creatingdatatableobjects if not exist. when creatingdatatableobjects, thefilloperation creates column name metadata. however, ifmissingschemaactionproperty setaddwithkey, appropriate primary keys , constraints created.
i have tested it, loads column-names , types, other properties allowdbnull or maxlength have default values might right or wrong.
Comments
Post a Comment