如何使用循环C#在数据集中的数据表中添加多个记录 [英] How to add multiple records insert in datatable from dataset using loop C#
问题描述
请帮帮我......
<前一行=c#>< pre> DataTable dt = new DataTable();
dt.Columns.Add( Exp_upto_Premonth, typeof ( string ));
dt.Columns.Add( Exp Upto, typeof ( string ));
dt.Columns.Add( Exp, typeof ( string ));
dt.Columns.Add( ExpUptoNow, typeof ( string ));
dt.Columns.Add( schemeno, typeof ( string ));
dt.Columns.Add( sch_amount, typeof ( string ));
dt.Columns.Add( Desciption, typeof ( string ));
dt.Columns.Add( sdate, typeof ( string ));
// dt.Columns.Add(PreMonth,typeof(string));
// dt.Columns.Add(CurrMonth,typeof(string));
DataRow dr = dt.NewRow();
Insrtds = GetDataExpen(wrkno,strtdt,CreatdDate,preMonth,enddt,curmonyr);
if (Insrtds.Tables [ 0 ]。行.Count!= 0 )
{
for (i = 0 ; i < Insrtds.Tables [ 0 ] .Rows.Count; i ++)
{
// dr [schemeno] = Insrtds .Tables [1] .Rows [0] [i];
dr [ Exp_upto_Premonth ] =
Insrtds.Tables [ 0 ]。行[i] Premonth];
dr [ Exp Upto] = Insrtds.Tables [ 0 ]。行[i] [ ExpUpto];
dr [ Exp] = Insrtds.Tables [ 0 ]。行[i] [ CurrMonExp];
dr [ ExpUptoNow] = Insrtds.Tables [ 0 ]。行[i] [ ExpUptoNow];
dr [ schemeno] = Insrtds.Tables [ 0 ]。行[i] [ Schemeno];
dr [ sch_amount] = Insrtds.Tables [ 0 ]。行[i] [ sch_amount];
dr [ Desciption] = Insrtds.Tables [ 0 ]。行[i] [ disp];
dr [ sdate] = Insrtds.Tables [ 0 ]。行[i] [ Schdate];
dt.AcceptChanges();
// object [] row1 = dr.ItemArray;
}
// InsertData(Insrtds,PreMonthName,CurrMonthName);
}
}
}
dt.Rows.Add(dr.ItemArray);
ViewState [ dt] = dt;
我尝试了什么:
i想插入多个使用循环记录到数据表但是我只得到1记录
for(i = 0; i< Insrtds.Tables [0]。 Rows.Count; i ++)
{
dr = dt.NewRow(); //添加此代码
dr [Exp_upto_Premonth] =
Insrtds.Tables [0] .Rows [i]Premonth];
dr [Exp Upto] = Insrtds.Tables [0] .Rows [i] [ExpUpto];
dr [Exp During] = Insrtds.Tables [0] .Rows [i] [CurrMonExp];
dr [ExpUptoNow] = Insrtds.Tables [0] .Rows [i] [ExpUptoNow];
dr [schemeno] = Insrtds.Tables [0] .Rows [i] [Schemeno];
dr [sch_amount] = Insrtds.Tables [0] .Rows [i] [sch_amount];
dr [Desciption] = Insrtds.Tables [0] .Rows [i] [disp];
dr [sdate] = Insrtds.Tables [0] .Rows [i] [Schdate];
dt.AcceptChanges();
// object [] row1 = dr.ItemArray;
}
please help me...
<pre> DataTable dt = new DataTable();
dt.Columns.Add("Exp_upto_Premonth", typeof(string));
dt.Columns.Add("Exp Upto", typeof(string));
dt.Columns.Add("Exp During", typeof(string));
dt.Columns.Add("ExpUptoNow", typeof(string));
dt.Columns.Add("schemeno", typeof(string));
dt.Columns.Add("sch_amount", typeof(string));
dt.Columns.Add("Desciption", typeof(string));
dt.Columns.Add("sdate", typeof(string));
//dt.Columns.Add("PreMonth", typeof(string));
//dt.Columns.Add("CurrMonth", typeof(string));
DataRow dr = dt.NewRow();
Insrtds = GetDataExpen(wrkno, strtdt, CreatdDate,preMonth, enddt,curmonyr);
if (Insrtds.Tables[0].Rows.Count != 0)
{
for (i = 0; i < Insrtds.Tables[0].Rows.Count; i++)
{
//dr["schemeno"] = Insrtds.Tables[1].Rows[0][i];
dr["Exp_upto_Premonth"] =
Insrtds.Tables[0].Rows[i]"Premonth"];
dr["Exp Upto"] = Insrtds.Tables[0].Rows[i]["ExpUpto"];
dr["Exp During"] = Insrtds.Tables[0].Rows[i]["CurrMonExp"];
dr["ExpUptoNow"] = Insrtds.Tables[0].Rows[i]["ExpUptoNow"];
dr["schemeno"] = Insrtds.Tables[0].Rows[i]["Schemeno"];
dr["sch_amount"] = Insrtds.Tables[0].Rows[i]["sch_amount"];
dr["Desciption"] = Insrtds.Tables[0].Rows[i]["disp"];
dr["sdate"] = Insrtds.Tables[0].Rows[i]["Schdate"];
dt.AcceptChanges();
//object[] row1 = dr.ItemArray;
}
//InsertData(Insrtds,PreMonthName,CurrMonthName);
}
}
}
dt.Rows.Add(dr.ItemArray);
ViewState["dt"] = dt;
What I have tried:
i want to insert multiple records into datatable using loop but i get only 1 record
for (i = 0; i < Insrtds.Tables[0].Rows.Count; i++) { dr = dt.NewRow();//Add this code dr["Exp_upto_Premonth"] = Insrtds.Tables[0].Rows[i]"Premonth"]; dr["Exp Upto"] = Insrtds.Tables[0].Rows[i]["ExpUpto"]; dr["Exp During"] = Insrtds.Tables[0].Rows[i]["CurrMonExp"]; dr["ExpUptoNow"] = Insrtds.Tables[0].Rows[i]["ExpUptoNow"]; dr["schemeno"] = Insrtds.Tables[0].Rows[i]["Schemeno"]; dr["sch_amount"] = Insrtds.Tables[0].Rows[i]["sch_amount"]; dr["Desciption"] = Insrtds.Tables[0].Rows[i]["disp"]; dr["sdate"] = Insrtds.Tables[0].Rows[i]["Schdate"]; dt.AcceptChanges(); //object[] row1 = dr.ItemArray; }
这篇关于如何使用循环C#在数据集中的数据表中添加多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!