我在.NET數據集中有這種數據轉換的情況:我有一個新的數據庫,我向我的數據集查詢了一張表,但我不知道DataColumn的數據類型數據集中的數據表,我想插入一些新的數據,而不知道設計時的DataColumn.DataType。所以,我的想法是我可以使用反射來獲取DataColumn的類型以精確插入新數據。 有人請告訴我我是否可以這樣做?.net數據集中數據列的動態設置值
SomeDbDataAdapter da = new SomeDbDataAdapter("select * from table1", conn);
DataSet ds = new DataSet();
da.Fill(ds);
Int32 i = 1;
DataRow dr = ds.Tables[0].NewRow();
PropertyInfo colInfo = dr.GetType().GetProperty("COLUMN1");
Type t = ds.Tables[0].Columns["COLUMN1"].DataType;
colInfo.SetValue(dr, Convert.ChangeType(i, t), null);
ds.Tables[0].Rows.Add(dr);
SomeDbCommandBuilder builder = new SomeDbCommandBuilder(da);
builder.GetInsertCommand();
DataRow[] rows = ds.Tables[0].Select("", "", DataViewRowState.Added);
da.Update(rows);
您可以發佈您的代碼? – Lourens
Int32 i = 1; DataRow dr = ds.Tables [0] .NewRow(); PropertyInfo colInfo = dr.GetType()。GetProperty(「COLUMN1」); 類型t = ds.Tables [0] .Columns [「COLUMN1」] .DataType; colInfo.SetValue(dr,Convert.ChangeType(i,t),null); colInfo.SetValue – anhdung88
@Lourens:我想要動態屬性設置值 – anhdung88