StringBuilder strSql=new StringBuilder(); strSql.Append("insert into HT_XunJiaMain("); strSql.Append("b_xunjiazhuti,b_xunjiariqi,b_lianxiren,b_lianxidianhua,b_dizhi)"); strSql.Append(" values ("); strSql.Append("@b_xunjiazhuti,@b_xunjiariqi,@b_lianxiren,@b_lianxidianhua,@b_dizhi)"); strSql.Append(";set @ReturnValue= @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@b_xunjiazhuti", SqlDbType.VarChar,200), new SqlParameter("@b_xunjiariqi", SqlDbType.VarChar,50), new SqlParameter("@b_lianxiren", SqlDbType.VarChar,50), new SqlParameter("@b_lianxidianhua", SqlDbType.VarChar,50), new SqlParameter("@b_dizhi", SqlDbType.VarChar,200), new SqlParameter("@ReturnValue",SqlDbType.Int)}; parameters[0].Value = model.b_xunjiazhuti; parameters[1].Value = model.b_xunjiariqi; parameters[2].Value = model.b_lianxiren; parameters[3].Value = model.b_lianxidianhua; parameters[4].Value = model.b_dizhi; parameters[5].Direction = ParameterDirection.Output; //这里输出的主表的ID List<CommandInfo> sqllist = new List<CommandInfo>(); CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters); sqllist.Add(cmd); StringBuilder strSql2; foreach (HT_Model.HT_XunJiaDetails models in model.HT_XunJiaDetailss) { strSql2=new StringBuilder(); strSql2.Append("insert into HT_XunJiaDetails("); strSql2.Append("d_baojialeixing,d_baojiayuan,d_beizhu,Pid,d_laiyuan,d_bianhao,d_zuzhiguige,d_buzhong,d_kezhong,d_baojiariqi,d_jiage)"); strSql2.Append(" values ("); strSql2.Append("@d_baojialeixing,@d_baojiayuan,@d_beizhu,@Pid,@d_laiyuan,@d_bianhao,@d_zuzhiguige,@d_buzhong,@d_kezhong,@d_baojiariqi,@d_jiage)"); SqlParameter[] parameters2 = { new SqlParameter("@d_baojialeixing", SqlDbType.VarChar,100), new SqlParameter("@d_baojiayuan", SqlDbType.VarChar,100), new SqlParameter("@d_beizhu", SqlDbType.VarChar,1000), new SqlParameter("@Pid", SqlDbType.Int,4), new SqlParameter("@d_laiyuan", SqlDbType.VarChar,100), new SqlParameter("@d_bianhao", SqlDbType.VarChar,100), new SqlParameter("@d_zuzhiguige", SqlDbType.VarChar,300), new SqlParameter("@d_buzhong", SqlDbType.VarChar,100), new SqlParameter("@d_kezhong", SqlDbType.VarChar,100), new SqlParameter("@d_baojiariqi", SqlDbType.VarChar,100), new SqlParameter("@d_jiage", SqlDbType.VarChar,100)}; parameters2[0].Value = models.d_baojialeixing; parameters2[1].Value = models.d_baojiayuan; parameters2[2].Value = models.d_beizhu; parameters2[3].Direction = ParameterDirection.InputOutput; //这里将主表的ID作为参数传入子表ParentID,这要是这这里的写法 //parameters2[3].Direction = ParameterDirection.InputOutput; //这样对应到后面的数据库处理就好了,代码自动生成的时候,这里不能自动转换,需要手动修改 parameters2[4].Value = models.d_laiyuan; parameters2[5].Value = models.d_bianhao; parameters2[6].Value = models.d_zuzhiguige; parameters2[7].Value = models.d_buzhong; parameters2[8].Value = models.d_kezhong; parameters2[9].Value = models.d_baojiariqi; parameters2[10].Value = models.d_jiage; cmd = new CommandInfo(strSql2.ToString(), parameters2); sqllist.Add(cmd); } DbHelperSQL.ExecuteSqlTranWithIndentity(sqllist); return (int)parameters[5].Value;