同时向主表和从表里面导入execl数据 (asp.net webform)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了同时向主表和从表里面导入execl数据 (asp.net webform)相关的知识,希望对你有一定的参考价值。

 

//前台

<asp:FileUpload ID="UPExecl" runat="server" />
<asp:Button ID="BtnExecl" runat="server" Text="确认" OnClick="BtnExecl_Click" />

//批量导入销售发货单
protected void BtnExecl_Click(object sender, EventArgs e)
{
if (UPExecl.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
{
Response.Write("<script>alert(‘请您选择Excel文件‘)</script> ");
return;//当无文件时,返回
}
string IsXls = System.IO.Path.GetExtension(UPExecl.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
if (IsXls != ".xls" && IsXls != ".xlsm" && IsXls != ".xlsx")
{
Response.Write("<script>alert(‘只可以选择Excel文件‘)</script>");
return;//当选择的不是Excel文件时,返回
}
string filename = UPExecl.FileName; //获取Execle文件名 DateTime日期函数
string savePath = Server.MapPath(("~\\Files\\") + filename);//Server.MapPath 获得虚拟服务器相对路径
UPExecl.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上
DataSet ds = ExcelSqlConnection(savePath, filename);
string resultmsg = ""; //结果信息
//保存执行的sql
ArrayList sqllist = new ArrayList();
///临时datatable
DataTable shrinfo = new DataTable();
int i = 0;
string sql = "";
DataSet rs = (DataSet)Session["e_xsfhd"];//销售发货单
DataTable dt = null;
if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
{
Response.Write("<script>alert(‘Excel表为空表,无数据!‘)</script>"); //当Excel表为空时,对用户进行提示
}
else
{
try
{
shrinfo = ds.Tables[0].Copy();//复制表结构
shrinfo.Clear();//清空数据
//循环读取execl表格里面的类容
foreach (DataRow dr in ds.Tables[0].Rows)
{
sqllist.Clear();//清空错误信息
string dhdw = dr["订货单位(必填)"].ToString();
string ckmc = dr["发货仓库(必填)"].ToString();
string ysgsbh = dr["运输公司(必填)"].ToString();
string shr = dr["收货人(必填)"].ToString();
string dhhm = dr["联系电话(必填)"].ToString();
string szcs = dr["所在城市(必填)"].ToString();
string sjrdz = dr["收件地址(必填)"].ToString();
string mjID = dr["买家ID(必填)"].ToString();
string xsr = dr["销售人"].ToString();
string bz = dr["备注"].ToString();
DataRow[] db2 = shrinfo.Select("收货人(必填)=‘" + shr + "‘ and 联系电话(必填)=‘" + dhhm + "‘ and 所在城市(必填)=‘" + szcs + "‘ and 收件地址(必填)=‘" + sjrdz + "‘ and 订货单位(必填)=‘" + dhdw + "‘ and 发货仓库(必填)=‘" + ckmc + "‘ and 运输公司(必填)=‘" + ysgsbh + "‘ and 买家ID(必填)=‘" + mjID + "‘");
//判断当前收件人信息是否已经生成订单
if (db2.Length == 0)
{
//对execl里面的数据行做一些简单的判断
if (ckmc.Length == 0)
{
resultmsg += "收货人" + shr + "对应发货仓库有空,请检查之后再导入!\\n";
continue;
}
if (ysgsbh.Length == 0)
{
resultmsg += "收货人" + shr + "对应运输公司有空,请检查之后再导入!\\n";
continue;
}
if (shr.Length == 0)
{
resultmsg += "收货人信息有空,请检查之后再导入!\\n";
continue;
}
if (dhhm.Length < 11)
{
resultmsg += "收货人" + shr + "对应电话号码有不正确,请检查之后再导入!\\n";
continue;
}
if (sjrdz.Length == 0)
{
resultmsg += "收货人" + shr + "对应地址有空,请检查之后再导入!\\n";
continue;
}
if (mjID.Length == 0)
{
resultmsg += "收货人" + shr + "对应买家ID有空,请检查之后再导入!\\n";
continue;
}
//对execl里面的一些必要字段做一些对比验证,看是否存在
//验证发货仓库在系统中是否存在(仓库代码)
string ckdm = "";
dt = IFACE.DBUtility.DbHelperSQL.QueryDT("select ckdm from tb_m_ckdm where ckmc=‘" + ckmc.Trim() + "‘ and ckzt=1");
if (dt.Rows.Count == 0)
{
resultmsg += "收货人" + shr + "对应发货仓库:" + ckmc + "在系统中不存在!";
continue;
}
else
{
ckdm = dt.Rows[0]["ckdm"].ToString();
}
//验证运输公司的代码是否存在
string ysgsdm = "";
sql = "select ysgsdm from dbo.tb_m_ysgsdm where ysgsmc=‘" + ysgsbh.Trim() + "‘";
dt = IFACE.DBUtility.DbHelperSQL.QueryDT(sql);
if (dt.Rows.Count == 0)
{
resultmsg += "收货人" + shr + "对应运输公司:" + ysgsbh + "在系统中不存在!";
continue;
}
else
{
ysgsdm = dt.Rows[0]["ysgsdm"].ToString();
}
//验证订货单位是否存在即验证客户代码是否存在
string khdm = "";
string khdj = "";
sql = "select khdm ,khdj from v_m_khdm where khmc=‘" + dhdw.Trim() + "‘";
dt = IFACE.DBUtility.DbHelperSQL.QueryDT(sql);
if (dt.Rows.Count == 0)
{
resultmsg += "收货人" + shr + "对应订货单位:" + dhdw + "在系统中不存在!";
continue;
}
else
{
khdm = dt.Rows[0]["khdm"].ToString();
khdj = dt.Rows[0]["khdj"].ToString();
}
//在生成销售单前需要做一些准备工作
System.Data.SqlClient.SqlConnection Conn = new System.Data.SqlClient.SqlConnection(Application["connectionstring"].ToString());
string djbh = DrpModule.RunID("tb_s_xsfhtzdzb", "djbh", "XSIFAC", Conn);
string fhdw = "IFAC";
string ghdw = "IFAC";//供货单位(默认IFAC)
string ddbh = "";//订单编号
string fph = "";//发票号
string crklx = "1";
string yffd = "1";
decimal totalMoney = 0;//总金额
decimal orderTotalMoney = 0;
decimal hkze = 0;//价格
int sl = 0;//数量
//得到货品代码
string hpdm = "";//
string hj = "";
//生成销售单
DataRow[] db = ds.Tables[0].Select("收货人(必填)=‘" + shr + "‘ and 联系电话(必填)=‘" + dhhm + "‘ and 所在城市(必填)=‘" + szcs + "‘ and 收件地址(必填)=‘" + sjrdz + "‘ and 订货单位(必填)=‘" + dhdw + "‘ and 发货仓库(必填)=‘" + ckmc + "‘ and 运输公司(必填)=‘" + ysgsbh + "‘ and 买家ID(必填)=‘" + mjID + "‘");
string jgdh = "";
int xh = 1;
string detailMsg = "";
string hpmc = "";
foreach (DataRow item in db)
{
totalMoney = 0;//第二次循环清空第一次的数据
//定义一个计算总金额的变量
hpmc = item["货品名称(必填)"].ToString();
sql = "select hpdm from tb_m_hpdm where hpmc=‘" + hpmc.Trim() + "‘";
dt = IFACE.DBUtility.DbHelperSQL.QueryDT(sql);
if (dt.Rows.Count == 0)
{
detailMsg += "收货人" + shr + "对应货品名称:" + hpmc + "在系统中不存在!";
continue;
}
else
{
hpdm = dt.Rows[0]["hpdm"].ToString();
}
//验证数据
try
{
sl = int.Parse(item["数量(必填)"].ToString());
if (sl < 1)
{
detailMsg += "收货人" + shr + "对应货品名称" + hpmc + "对应的数量格式不能小于或者等于零。\\n";
}
}
catch
{
detailMsg += "收货人" + shr + "对应货品名称" + hpmc + "对应的数量格式填写不正确。\\n";
continue;
}
try
{
hkze = decimal.Parse(item["价格(必填)"].ToString());
}
catch
{
detailMsg += "收货人" + shr + "对应货品名称" + hpmc + "对应的价格格式填写不正确。\\n";
continue;
}
//判断该商品是否有可分配数量
int ss = 0;
int hgpkfpsl = 0;
sql = @"select hgpkfpsl from tb_i_dqkcjs where hpdm=‘" + hpdm + "‘ and ckdm=‘" + ckdm + "‘";
dt = IFACE.DBUtility.DbHelperSQL.QueryDT(sql);
if (dt.Rows.Count > 0)
{
hgpkfpsl = int.Parse(dt.Rows[0]["hgpkfpsl"].ToString());
ss += sl;
if (ss > hgpkfpsl)
{
detailMsg += "收货人:" + shr + "名下货品名称" + hpmc + "对应的合格品可分配数量在库存中不足,请联系商务维护。\\n";
continue;
}
}
//判断该商品在仓库中的货架是否存在
sql = @"select hjdm from tb_i_hphj where ckdm=‘" + ckdm.Trim() + "‘ and hpdm=‘" + hpdm.Trim() + "‘ ";
dt = IFACE.DBUtility.DbHelperSQL.QueryDT(sql);
if (dt.Rows.Count > 0)
{
hj = dt.Rows[0]["hjdm"].ToString();
}
else
{
detailMsg += "收货人:" + shr + "名下货品名称" + hpmc + "对应的货架" + hj + "在系统中不存在,请联系商务或管理员维护。\\n";
continue;
}
//查询商品的成本价,批发价
//定义销售成本价的变量和批发价格的变量
decimal stockPrice = 0;
decimal price = 0;
//销售成本价
sql = "select stockprice from tb_p_stockprice where hpdm=‘" + hpdm.Trim() + "‘ and ckdm=‘" + ckdm.Trim() + "‘";
dt = IFACE.DBUtility.DbHelperSQL.QueryDT(sql);
if (dt.Rows.Count == 0)
{
detailMsg += "收货人:" + shr + "名下货品名称" + hpmc + "对应的成本价" + stockPrice + "在系统中不存在,请联系商务或管理员维护。\\n";
continue;
}
else
{
stockPrice = decimal.Parse(dt.Rows[0]["stockPrice"].ToString());
}
//批发价格
sql = "select price from tb_P_channelprice where hpdm=‘" + hpdm.Trim() + "‘ and khdj=‘" + khdj + "‘";
dt = IFACE.DBUtility.DbHelperSQL.QueryDT(sql);
if (dt.Rows.Count == 0)
{
detailMsg += "收货人:" + shr + "名下货品名称" + hpmc + "对应的批发价价" + price + "在系统中不存在,请联商务或管理员维护。\\n";
continue;
}
else
{
price = decimal.Parse(dt.Rows[0]["price"].ToString());
}
totalMoney += sl * hkze;//得到金额小结
orderTotalMoney = orderTotalMoney + sl * hkze;
//加入到销售单明细表
sql = @"insert into tb_s_xsfhmxb(djbh, xh, hpdm, sh, stockjg, channeljg, jg, sl, hj, sfsl, jexj, sfjyf, flcs, bjts, cbjg, jgdh, xscj, jgf, CreateDate)
values(‘" + djbh + "‘,‘" + xh + "‘,‘" + hpdm + "‘,‘" + 2012 + "‘,‘" + stockPrice + "‘,‘" + price + "‘,‘" + item["价格(必填)"].ToString() + "‘,‘" + item["数量(必填)"].ToString() + "‘,‘" + hj + "‘,‘" + 0 + "‘,‘" + totalMoney + "‘,‘" + 0 + "‘,‘" + 0 + "‘,‘" + 0 + "‘,‘" + 0 + "‘,‘" + jgdh + "‘,0,null,getdate())";
sqllist.Add(sql.ToString());
xh++;
}
//销售单主表
sql = @"insert into tb_s_xsfhtzdzb (djbh,ddbh,fph,crklx,ysfs,yffd,fhdw,fhck,ghdw,dhdw,zdr,zdsj,hpzt,fplx,jsfs,zrje,hkze,jghs,ysje,sfxysm,bz1,updatetime,shdz,shr,shrdh,shrcs,xsry,fkje,buyer_nick)
values(‘" + djbh + "‘,‘" + ddbh + "‘,‘" + fph + "‘,‘" + crklx + "‘,‘" + ysgsdm + "‘,‘" + yffd + "‘,‘" + fhdw + "‘,‘" + ckdm + "‘,‘" + ghdw + "‘,‘" + khdm + "‘,‘" + Session["UserID"].ToString() + "‘,getdate(),‘" + 1 + "‘,‘" + 1 + "‘,‘" + 1 + "‘,‘" + 0 + "‘,‘" + orderTotalMoney + "‘,‘" + 0 + "‘,‘" + 0 + "‘,0,‘" + bz + "‘,getdate(),‘" + sjrdz + "‘,‘" + shr + "‘,‘" + dhhm + "‘,‘" + szcs + "‘,‘" + xsr + "‘,‘" + orderTotalMoney + "‘,‘" + mjID + "‘)";
sqllist.Insert(0, sql.ToString());
//某一行出现错误那么在后面的那些数据都不会执行
// string errmesage = "";
if (detailMsg.Length > 0)
{
resultmsg += detailMsg;
//订单生成成功,将收件人信息保存至临时表
DataRow drs = shrinfo.NewRow();
drs["收货人(必填)"] = shr;
drs["联系电话(必填)"] = dhhm;
drs["所在城市(必填)"] = szcs;
drs["收件地址(必填)"] = sjrdz;
drs["订货单位(必填)"] = dhdw;
drs["发货仓库(必填)"] = ckmc;
drs["运输公司(必填)"] = ysgsbh;
drs["买家ID(必填)"] = mjID;
shrinfo.Rows.Add(drs);
}
else
{
string ymbh = "3001";
string shjg = "IFAC";
//string ymbh2 = "3002";
string qmsx = "1";
string qmsx2 = "2";
string lcbh = "1";
string sfls = "0";
string djlx = "1";
//默认的是零售流程,审核页面1
sql = @"insert into tb_sys_shjgb(djbh,ymbh,qmsx,shjg,CreateDate) values(‘" + djbh + "‘,‘" + ymbh + "‘,‘" + qmsx + "‘,‘" + shjg + "‘,getdate())";
sqllist.Add(sql.ToString());
//默认的是零售流程,审核页面2
sql = @"insert into tb_sys_shjgb(djbh,ymbh,qmsx,shjg,CreateDate) values(‘" + djbh + "‘,‘" + ymbh + "‘,‘" + qmsx2 + "‘,‘" + shjg + "‘,getdate())";
sqllist.Add(sql.ToString());
sql = @"insert into tb_sys_lctz(djbh,lcbh,sfls,djlx) values(‘" + djbh + "‘,‘" + lcbh + "‘,‘" + sfls + "‘,‘" + djlx + "‘)";
sqllist.Add(sql.ToString());
IFACE.DBUtility.DbHelperSQL.ExecuteSqlTran(sqllist);
//订单生成成功,将收件人信息保存至临时表
DataRow drs = shrinfo.NewRow();
drs["收货人(必填)"] = shr;
drs["联系电话(必填)"] = dhhm;
drs["所在城市(必填)"] = szcs;
drs["收件地址(必填)"] = sjrdz;
drs["订货单位(必填)"] = dhdw;
drs["发货仓库(必填)"] = ckmc;
drs["运输公司(必填)"] = ysgsbh;
drs["买家ID(必填)"] = mjID;
shrinfo.Rows.Add(drs);
i++;
}
}
else
{
continue;
}
}
}
catch (Exception ex)
{
Response.Write("<script>alert(‘" + ex.Message + "‘)</script>");
}
}
if (resultmsg.Length > 500)
{
resultmsg = resultmsg.Substring(0, 500) + "......";
}
Response.Write("<script>alert(‘成功导入" + i + "条数据\\n" + resultmsg + "‘)</script>");
}

以上是关于同时向主表和从表里面导入execl数据 (asp.net webform)的主要内容,如果未能解决你的问题,请参考以下文章

向主表和子表同时插入数据如何写SQL语句

SQL主表从表

数据库

JAVA数据库编程

Mybatis Collection查询集合只出现一条数据

mybatis 关联查询时,从表只返回第一条记录解决办法