如何在3层架构中插入数据 [英] How to insert data in 3 tier architecture
本文介绍了如何在3层架构中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里我的问题是如何在这里插入数据我的代码
DAL:
#region InsertData
public int Insert(NCBAL ObjInsert)
{
con = new SqlConnection(constr);
con.Open();
cmd = new SqlCommand( InsertNewComp_SP ,con);
cmd.CommandType = CommandType.StoredProcedure;
尝试
{
cmd.Parameters.AddWithValue( < span class =code-string> @ compliantID,ObjInsert.compid).ToString();
cmd.Parameters.AddWithValue( @ customerid,ObjInsert.cusid).ToString( );
cmd.Parameters.AddWithValue( @ customername,ObjInsert.cusname).ToString( );
cmd.Parameters.AddWithValue( @ branchid,ObjInsert.ddlbranch).ToString( );
cmd.Parameters.AddWithValue( @ servicetype,ObjInsert.servicetype);
return cmd.ExecuteNonQuery();
}
catch
{
throw ;
}
最后
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}
#endregion
BAL:
#region插入数据
public int 插入(NCBAL ObjInsert)
{
尝试
{
return ncd.Insert(ObjInsert);
}
catch
{
throw ;
}
最后
{
ncd = null ;
}
}
#endregion
UI:
ncb.compid = txtcompid.Text;
ncb.cusid = txtcusid.Text;
ncb.cusname = txtcusname.Text;
ncb.cusnum = txtcusnumber.Text;
ncb.compname = txtcompname.Text;
ncb.ddlproduct = DDlProductname.Text;
ncb.compliant = txtCompliant.Text;
ncb.compdate = Convert.ToDateTime(txtcompdate.Text);
ncb.solution = txtsolution.Text;
ncb.ddlassign = DDlAssign.Text;
ncb.datetocomp = Convert.ToDateTime(txtdatetocom.Text);
ncb.ddlpriorty = DDlPriorty.Text;
ncb.ddlstatus = DDlStatus.Text;
ncb.complianttime = txtcomplianttime.Text;
ncb.datetocomp = txtdatecomptime.Text;
ncb.ddlbranch = DDLbranch.Text;
字符串 str = ;
for ( int i = 0 ; i < = CBLservicetype.Items.Count - 1 ; i ++)
{
if (CBLservicetype.Items [i] .Selected)
{
if (str == )
{
str = CBLservicetype .Items [I]。文本;
}
else
{
str + = , + CBLservicetype.Items [i] .Text;
}
}
}
ncb.servicetype = str;
}
数据未保存在数据库中,而另一件事是我要检查(结果> 0)怎么做那...
解决方案
BAL bal = new BAL() ;
int returnVal = bal.Insert(ncb);
if (returnVal > 0 )
// 插入成功
其他
// 插入失败
Here my problem is how to inset data here my code
DAL:
#region InsertData
public int Insert(NCBAL ObjInsert)
{
con = new SqlConnection(constr);
con.Open();
cmd = new SqlCommand("InsertNewComp_SP", con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
cmd.Parameters.AddWithValue("@compliantID",ObjInsert.compid).ToString();
cmd.Parameters.AddWithValue("@customerid", ObjInsert.cusid).ToString();
cmd.Parameters.AddWithValue("@customername", ObjInsert.cusname).ToString();
cmd.Parameters.AddWithValue("@branchid", ObjInsert.ddlbranch).ToString();
cmd.Parameters.AddWithValue("@servicetype",ObjInsert.servicetype);
return cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}
#endregion
BAL:
#region Insert Data
public int Insert(NCBAL ObjInsert)
{
try
{
return ncd.Insert(ObjInsert);
}
catch
{
throw;
}
finally
{
ncd = null;
}
}
#endregion
UI:
ncb.compid = txtcompid.Text;
ncb.cusid = txtcusid.Text;
ncb.cusname = txtcusname.Text;
ncb.cusnum = txtcusnumber.Text;
ncb.compname = txtcompname.Text;
ncb.ddlproduct = DDlProductname.Text;
ncb.compliant = txtCompliant.Text;
ncb.compdate = Convert.ToDateTime(txtcompdate.Text);
ncb.solution = txtsolution.Text;
ncb.ddlassign = DDlAssign.Text;
ncb.datetocomp = Convert.ToDateTime(txtdatetocom.Text);
ncb.ddlpriorty = DDlPriorty.Text;
ncb.ddlstatus = DDlStatus.Text;
ncb.complianttime = txtcomplianttime.Text;
ncb.datetocomp = txtdatecomptime.Text;
ncb.ddlbranch = DDLbranch.Text;
String str = "";
for (int i = 0; i <= CBLservicetype.Items.Count - 1; i++)
{
if (CBLservicetype.Items[i].Selected)
{
if (str == "")
{
str = CBLservicetype.Items[i].Text;
}
else
{
str += "," + CBLservicetype.Items[i].Text;
}
}
}
ncb.servicetype = str;
}
the data not saved in database and another things is i want to check (result>0) how to do that...
解决方案
BAL bal = new BAL(); int returnVal = bal.Insert(ncb); if( returnVal > 0) //Insert Success else //Insert Failed
这篇关于如何在3层架构中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文