Data Access Application Block 利用DataSet 操作DB
这几天在做一个项目的CR.此项目利用了Data Access Application Block来进行DB操作.
以下代码是对一个Table 用DB.UpdateDataSet方法来操
Code
DbCommand dc_summary = DB.GetSqlStringCommand("select * from APPLY_NPRB_SUMMARY where 1=2");
DataSet summary_dataset = DB.ExecuteDataSet(dc_summary);
summary_dataset.Tables[0].TableName = "summary";
//Business strategy
DropDownList[] DDL_BS = {this.ddlBsLev1,this.ddlBsLev2 ,this.ddlBsLev3 };
Label[] LBL_BS ={this.lblBsLev1Desc,this.lblBsLev2Desc ,this.lblBsLev3Desc};
TextBox[] TXT_BS = { this.txtBsLev1, this.txtBsLev2, this.txtBsLev3 };
for (int i = 0; i < DDL_BS.Length; i ++ )
{
if(DDL_BS[i].SelectedIndex != 0)
{
DataRow new_dr = summary_dataset.Tables[0].NewRow();
new_dr["FORM_NO"] = form_no;
new_dr["ITEM_NAME"] = "Business strategy";
new_dr["ITEM_CATEGORY"] = DDL_BS[i].SelectedValue;
new_dr["ITEM_DESCRIPTION"] = LBL_BS[i].Text;
new_dr["ITEM_VALUE"] = TXT_BS[i].Text;
new_dr["ITEM_PRIORITY"] = i + 1;
summary_dataset.Tables[0].Rows.Add(new_dr);
}
}
//提昇產品競爭力
DropDownList[] DDL_PC = { this.ddlPcLev1, this.ddlPcLev2, this.ddlPcLev3 };
Label[] LBL_PC ={ this.lblPcLev1Desc, this.lblPcLev2Desc, this.lblPcLev3Desc };
TextBox[] TXT_PC = { this.txtPcLev1, this.txtPcLev2, this.txtPcLev3 };
for (int i = 0; i < DDL_PC.Length; i++)
{
if (DDL_PC[i].SelectedIndex != 0)
{
DataRow new_dr = summary_dataset.Tables[0].NewRow();
new_dr["FORM_NO"] = form_no;
new_dr["ITEM_NAME"] = "提昇產品競爭力";
new_dr["ITEM_CATEGORY"] = DDL_PC[i].SelectedValue;
new_dr["ITEM_DESCRIPTION"] = LBL_PC[i].Text;
new_dr["ITEM_VALUE"] = TXT_PC[i].Text;
new_dr["ITEM_PRIORITY"] = i + 1;
summary_dataset.Tables[0].Rows.Add(new_dr);
}
}
//Green label
DataRow new_dr_gl = summary_dataset.Tables[0].NewRow();
new_dr_gl["FORM_NO"] = form_no;
new_dr_gl["ITEM_NAME"] = "Green label";
new_dr_gl["ITEM_CATEGORY"] = "";
new_dr_gl["ITEM_DESCRIPTION"] = "";
new_dr_gl["ITEM_VALUE"] = this.ddlGreenlabel.SelectedValue;
new_dr_gl["ITEM_PRIORITY"] = "";
summary_dataset.Tables[0].Rows.Add(new_dr_gl);
//MP date
DataRow new_dr_mp = summary_dataset.Tables[0].NewRow();
new_dr_mp["FORM_NO"] = form_no;
new_dr_mp["ITEM_NAME"] = "MP date";
new_dr_mp["ITEM_CATEGORY"] = "";
new_dr_mp["ITEM_DESCRIPTION"] = "";
new_dr_mp["ITEM_VALUE"] =this.lblMpDateValue.Text ;
new_dr_mp["ITEM_PRIORITY"] = "";
summary_dataset.Tables[0].Rows.Add(new_dr_mp);
//CMI,Risk Assessment,Downgrade treatment,Reference Model
TextBox[] TBs ={this.txtCMI , this.txtRisk, this.txtDowngrade, this.txtReference };
string[] ItemNames = { "CMI", "Risk Assessment", "Downgrade treatment", "Reference Model"};
for (int i = 0; i < TBs.Length; i++)
{
DataRow new_dr = summary_dataset.Tables[0].NewRow();
new_dr["FORM_NO"] = form_no;
new_dr["ITEM_NAME"] = ItemNames[i];
new_dr["ITEM_CATEGORY"] = "";
new_dr["ITEM_DESCRIPTION"] = "";
new_dr["ITEM_VALUE"] = TBs[i].Text;
new_dr["ITEM_PRIORITY"] = "";
summary_dataset.Tables[0].Rows.Add(new_dr);
}
DbCommand dc_summary_insert = DB.GetSqlStringCommand("insert into APPLY_NPRB_SUMMARY (FORM_NO, ITEM_NAME,ITEM_CATEGORY,ITEM_DESCRIPTION,ITEM_VALUE,ITEM_PRIORITY) values (@FORM_NO ,@ITEM_NAME ,@ITEM_CATEGORY,@ITEM_DESCRIPTION,@ITEM_VALUE,@ITEM_PRIORITY ) ");
foreach (DataColumn column in summary_dataset.Tables[0].Columns)
{
DB.AddInParameter(dc_summary_insert, "@" + column.ColumnName, DbType.String, column.ColumnName, DataRowVersion.Current);
}
int resultSummary = DB.UpdateDataSet(summary_dataset, "summary", dc_summary_insert,null ,null,UpdateBehavior.Continue);
DbCommand dc_summary = DB.GetSqlStringCommand("select * from APPLY_NPRB_SUMMARY where 1=2");
DataSet summary_dataset = DB.ExecuteDataSet(dc_summary);
summary_dataset.Tables[0].TableName = "summary";
//Business strategy
DropDownList[] DDL_BS = {this.ddlBsLev1,this.ddlBsLev2 ,this.ddlBsLev3 };
Label[] LBL_BS ={this.lblBsLev1Desc,this.lblBsLev2Desc ,this.lblBsLev3Desc};
TextBox[] TXT_BS = { this.txtBsLev1, this.txtBsLev2, this.txtBsLev3 };
for (int i = 0; i < DDL_BS.Length; i ++ )
{
if(DDL_BS[i].SelectedIndex != 0)
{
DataRow new_dr = summary_dataset.Tables[0].NewRow();
new_dr["FORM_NO"] = form_no;
new_dr["ITEM_NAME"] = "Business strategy";
new_dr["ITEM_CATEGORY"] = DDL_BS[i].SelectedValue;
new_dr["ITEM_DESCRIPTION"] = LBL_BS[i].Text;
new_dr["ITEM_VALUE"] = TXT_BS[i].Text;
new_dr["ITEM_PRIORITY"] = i + 1;
summary_dataset.Tables[0].Rows.Add(new_dr);
}
}
//提昇產品競爭力
DropDownList[] DDL_PC = { this.ddlPcLev1, this.ddlPcLev2, this.ddlPcLev3 };
Label[] LBL_PC ={ this.lblPcLev1Desc, this.lblPcLev2Desc, this.lblPcLev3Desc };
TextBox[] TXT_PC = { this.txtPcLev1, this.txtPcLev2, this.txtPcLev3 };
for (int i = 0; i < DDL_PC.Length; i++)
{
if (DDL_PC[i].SelectedIndex != 0)
{
DataRow new_dr = summary_dataset.Tables[0].NewRow();
new_dr["FORM_NO"] = form_no;
new_dr["ITEM_NAME"] = "提昇產品競爭力";
new_dr["ITEM_CATEGORY"] = DDL_PC[i].SelectedValue;
new_dr["ITEM_DESCRIPTION"] = LBL_PC[i].Text;
new_dr["ITEM_VALUE"] = TXT_PC[i].Text;
new_dr["ITEM_PRIORITY"] = i + 1;
summary_dataset.Tables[0].Rows.Add(new_dr);
}
}
//Green label
DataRow new_dr_gl = summary_dataset.Tables[0].NewRow();
new_dr_gl["FORM_NO"] = form_no;
new_dr_gl["ITEM_NAME"] = "Green label";
new_dr_gl["ITEM_CATEGORY"] = "";
new_dr_gl["ITEM_DESCRIPTION"] = "";
new_dr_gl["ITEM_VALUE"] = this.ddlGreenlabel.SelectedValue;
new_dr_gl["ITEM_PRIORITY"] = "";
summary_dataset.Tables[0].Rows.Add(new_dr_gl);
//MP date
DataRow new_dr_mp = summary_dataset.Tables[0].NewRow();
new_dr_mp["FORM_NO"] = form_no;
new_dr_mp["ITEM_NAME"] = "MP date";
new_dr_mp["ITEM_CATEGORY"] = "";
new_dr_mp["ITEM_DESCRIPTION"] = "";
new_dr_mp["ITEM_VALUE"] =this.lblMpDateValue.Text ;
new_dr_mp["ITEM_PRIORITY"] = "";
summary_dataset.Tables[0].Rows.Add(new_dr_mp);
//CMI,Risk Assessment,Downgrade treatment,Reference Model
TextBox[] TBs ={this.txtCMI , this.txtRisk, this.txtDowngrade, this.txtReference };
string[] ItemNames = { "CMI", "Risk Assessment", "Downgrade treatment", "Reference Model"};
for (int i = 0; i < TBs.Length; i++)
{
DataRow new_dr = summary_dataset.Tables[0].NewRow();
new_dr["FORM_NO"] = form_no;
new_dr["ITEM_NAME"] = ItemNames[i];
new_dr["ITEM_CATEGORY"] = "";
new_dr["ITEM_DESCRIPTION"] = "";
new_dr["ITEM_VALUE"] = TBs[i].Text;
new_dr["ITEM_PRIORITY"] = "";
summary_dataset.Tables[0].Rows.Add(new_dr);
}
DbCommand dc_summary_insert = DB.GetSqlStringCommand("insert into APPLY_NPRB_SUMMARY (FORM_NO, ITEM_NAME,ITEM_CATEGORY,ITEM_DESCRIPTION,ITEM_VALUE,ITEM_PRIORITY) values (@FORM_NO ,@ITEM_NAME ,@ITEM_CATEGORY,@ITEM_DESCRIPTION,@ITEM_VALUE,@ITEM_PRIORITY ) ");
foreach (DataColumn column in summary_dataset.Tables[0].Columns)
{
DB.AddInParameter(dc_summary_insert, "@" + column.ColumnName, DbType.String, column.ColumnName, DataRowVersion.Current);
}
int resultSummary = DB.UpdateDataSet(summary_dataset, "summary", dc_summary_insert,null ,null,UpdateBehavior.Continue);
作.