如何在ASP.NET C#中动态更改数据列的数据类型? [英] How to change datatype of datacolumn dynamically in ASP.NET C#?
问题描述
我正在动态创建数据表。我正在从DB添加数据类型和列名称。但是在创建数据表时动态地从DB获取Int32 / double数据类型时,我遇到问题一旦列有数据就无法更改列的数据类型
我尝试过:
StreamReader Rep001Stream = new StreamReader(filePath);
尝试
{
if (dsStdRep001SchemaDetails.Tables [ 0 ]。Rows.Count > 0 )
{
string [] words;
DataRow row = null ;
尝试
{
string Rep001Record = ;
while (!Rep001Stream.EndOfStream)
{
Rep001Record = Rep001Stream.ReadLine();
for ( int i = 0 ; i < dsStdRep001SchemaDetails.Tables [ 0 ]。Rows.Count; i ++)
{
int temp = i;
System.Data.DataTable dtTemp1 = new System.Data.DataTable();
dtTemp1.Columns.Add( new DataColumn( 列 + i));
dtRep001.Columns [i] .DataType = System.Type.GetType( System。 + dsStdRep001SchemaDetails.Tables [ 0 ]。行[i] [ ColumnType跨度>]的ToString());
dtRep001.Columns [i] .ColumnName = dsStdRep001SchemaDetails.Tables [ 0 ]。行[i] [ ColumnName]。ToString();
string ColumnId = dtRep001.Columns [i] .ColumnName.ToString();
words = Rep001Record.Split(' ,');
if (字[ 0 ]!= STR_RPT_END)
{
if (i == 0 )
{
row = dtRep001.NewRow ();
dtRep001.Rows.Add(row);
}
// row [ColumnId] = words [i] .Trim();
}
}
}
}
catch (例外情况)
{
}
}
Rep001Stream.Close();
Rep001Stream.Dispose();
}
catch (例外情况)
{
objFileDetails.Status = E;
objFileDetails.ErrorDescription = ex.Message;
bool Succeeded = SaveFile(objFileDetails);
Common.WriteLogService( 错误: + ex.Message,ex.StackTrace,< span class =code-keyword> true );
Rep001Stream.Close();
Rep001Stream.Dispose();
}
这是如何使用C#动态更改数据类型到ASP.NET中的动态数据表? [ ^ ]
无论如何,
首先尝试将所有数据CAST到单个数据类型从数据库中提取。
示例:< pre>从表中选择强制转换(columnName as int)columnName
参考:< a href =http://stackoverflow.com/questions/9028029/how-to-change-datatype-of-a-datacolumn-in-a-datatable> c# - 如何更改DataTable中DataColumn的DataType? - 堆栈溢出 [ datatype-of-a-datacolumn-in-a-datatabletarget =_ blanktitle =新窗口> ^ ]
在此自定义数据类型
Temp1.Columns.Add(new DataColumn(Column+ i));
喜欢这个
Temp1.Columns.Add(new DataColumn(Column+ i),typeof(int));
I am creating a datatable dynamically. I am adding the datatype and column name from DB. but I am getting issue "cannot change datatype of column once a column has data" on fetching Int32 /double datatype from DB while creating a data table dynamically
What I have tried:
StreamReader Rep001Stream = new StreamReader(filePath);
try
{
if (dsStdRep001SchemaDetails.Tables[0].Rows.Count > 0)
{
string[] words;
DataRow row=null;
try
{
string Rep001Record = "";
while (!Rep001Stream.EndOfStream)
{
Rep001Record = Rep001Stream.ReadLine();
for (int i = 0; i < dsStdRep001SchemaDetails.Tables[0].Rows.Count; i++)
{
int temp = i;
System.Data.DataTable dtTemp1 = new System.Data.DataTable();
dtTemp1.Columns.Add(new DataColumn("Column" + i));
dtRep001.Columns[i].DataType = System.Type.GetType("System." + dsStdRep001SchemaDetails.Tables[0].Rows[i]["ColumnType"].ToString());
dtRep001.Columns[i].ColumnName = dsStdRep001SchemaDetails.Tables[0].Rows[i]["ColumnName"].ToString();
string ColumnId = dtRep001.Columns[i].ColumnName.ToString();
words = Rep001Record.Split(',');
if (words[0] != STR_RPT_END)
{
if (i == 0)
{
row = dtRep001.NewRow();
dtRep001.Rows.Add(row);
}
// row[ColumnId] = words[i].Trim();
}
}
}
}
catch (Exception ex)
{
}
}
Rep001Stream.Close();
Rep001Stream.Dispose();
}
catch (Exception ex)
{
objFileDetails.Status = "E";
objFileDetails.ErrorDescription = ex.Message;
bool Succeeded = SaveFile(objFileDetails);
Common.WriteLogService("Error : " + ex.Message, ex.StackTrace, true);
Rep001Stream.Close();
Rep001Stream.Dispose();
}
This is a duplicate of How to change datatype dynamically to a dynamic data table in ASP.NET with C#?[^]
Anyway,
Try CAST all your data to a single datatype first when extracting from database. Example: <pre>select cast(columnName as int) columnName from table
Reference: c# - How To Change DataType of a DataColumn in a DataTable? - Stack Overflow[^]
Define datatype here itself
Temp1.Columns.Add(new DataColumn("Column" + i));
like this
Temp1.Columns.Add(new DataColumn("Column" + i), typeof(int));
这篇关于如何在ASP.NET C#中动态更改数据列的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!