将数据插入数据库

时间:2016-11-23 08:01:16

标签: c# asp.net asp.net-mvc visual-studio

我有一个小问题,我无法在我的数据库中执行插入操作,我认为它没有错过很多工作,但我没有看到它可以来自^^。

实际上我的视图中有一个按钮,当我点击它时会直接在我的数据库中插入一个插入,这是我的视图代码:

using (Html.BeginForm("interface_prevision_action", "PIC", FormMethod.Post, new { @class = "form" }))
{
    <div class="row">
        <!-- Colonne 1 -->
        <div class="col-md-4">
            <div class="form-group">
                <label for="Tdate_d">Date de début</label>
                @Html.TextBox("Tdate_d", "01/" + DateTime.Now.Month + "/" + DateTime.Now.Year, new { id = "Tdate_d", @class = "form-control" })
            </div>
        </div>
        <div class="col-md-4">
            <div class="form-group">
                <label for="Tdate_f">Date de fin</label>
                @Html.TextBox("Tdate_f", (DateTime.Now.Year + 1) + "" + (DateTime.Now.Month - 1), new { @readonly = "readonly", id = "Tdate_f", @class = "form-control" })
            </div>
        </div>
        <div class="col-md-4">
            <div class="form-group" style="padding-top:24px;">
                @Html.DropDownList("Ddl_entite", listEntite, new { id = "Ddl_entite", @class = "form-control" })
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-4">
            @Html.ActionLink("Intégration des prévisions", "interface_prevision", new { mode = "insert" }, new { @id = "btnIntegrationPrevision", @class = "btn btn-primary non-printable" })
        </div>
        <div class="col-md-4"></div>
        <div class="col-md-4"></div>
    </div>
}

我的控制器:

[ModuleFilter(FilterFormulaire = false)]
    public ActionResult interface_prevision()
    {
        ViewData["Ddl_entite"] = model.getEntite();
        return View();
    }


    public ActionResult interface_prevision_action(string Tdate_d, string Tdate_f, string Ddl_entite)
    {
        model.IntegrationPrevision(Tdate_d, Tdate_f, Ddl_entite);

        return RedirectToAction("interface_prevision");
    }

我的模特:

public void IntegrationPrevision(string Tdate_d, string Tdate_f, string Ddl_entite)
    {
        SqlConnection cn;
        SqlCommand cmd;
        string sql;
        SqlDataAdapter dam;
        DataSet dsm = new DataSet();
        DataSet dsp = new DataSet();
        DataTable dtm = new DataTable();
        DataTable dtp = new DataTable();

        cn = new SqlConnection(CS_PMI);
        cn.Open();

        /* contrôle avant traitement */

        // lecture des mois sur la période

        sql = "SELECT   year(CAKJDATE),convert(varchar,DateName( month , DateAdd( month , month(CAKJDATE) , -1 ))),'01/'+substring(CAKJDATE,5,2)+'/'+substring(CAKJDATE,1,4) ";
        sql = sql + "FROM [PMI].[dbo].[CALEND] ";
        sql = sql + "where CAKTSOC='100' and CAKTTYPE='01' and CAKTCODE='' ";
        sql = sql + "and cast(CAKJDATE as datetime)>=convert(varchar,'" + @Tdate_d + "',103) and cast(CAKJDATE as datetime)< convert(varchar,cast('" + @Tdate_d + "' as datetime)+375,103) ";
        sql = sql + "group by convert(varchar,DateName( month , DateAdd( month , month([CAKJDATE]) , -1 ))),month([CAKJDATE]),year([CAKJDATE]),'01/'+substring(CAKJDATE,5,2)+'/'+substring(CAKJDATE,1,4) ";
        sql = sql + "order by year([CAKJDATE]),month([CAKJDATE]); ";

        string testRequete = "and cast(CAKJDATE as datetime)>=convert(varchar,'" + @Tdate_d + "',103) and cast(CAKJDATE as datetime)< convert(varchar,cast('" + @Tdate_d + "' as datetime)+375,103) ";

        try
        {
            dam = new SqlDataAdapter(sql, cn);
            dsm = new DataSet();
            dam.Fill(dsm, "lstmois");
            dtm = dsm.Tables["lstmois"];

        }
        catch (SqlException e5)
        {
            cn.Close();

        }

        /* Suppression des prévisions */
        sql = "delete FROM [PMI].[dbo].[PREVIS] where SCKTSOC='100' and SCKTTYPENR='PR' and SCKTTYPCUM='9' and SCKTCODART like 'PDP_PI%'";
        cmd = cn.CreateCommand();
        cmd.CommandText = sql;
        cmd.CommandTimeout = 0;
        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception e5)
        {

        }

        /* Integration des previsions */

        sql = "INSERT INTO [PMI].[dbo].[PREVIS] ";
        sql = sql + "([SCKTTYPENR],[SCKTTYPCUM],[SCKTSOC],[SCKTCOMPTE],[SCKTCODART],[SCKTCOMART] ";
        sql = sql + ",[SCCTCRI_01],[SCCTCRI_02],[SCCTCRI_03],[SCCTCRI_04],[SCCTCRI_05],[SCCTCRI_06],[SCCTCRI_07],[SCCTCRI_08],[SCCTCRI_09],[SCCTCRI_10] ";
        sql = sql + ",[SCCNQTE_01],[SCCNQTE_02],[SCCNQTE_03],[SCCNQTE_04],[SCCNQTE_05],[SCCNQTE_06],[SCCNQTE_07],[SCCNQTE_08],[SCCNQTE_09],[SCCNQTE_10],[SCCNQTE_11],[SCCNQTE_12],[SCCNQTE_13],[SCCNQTE_14],[SCCNQTE_15],[SCCNQTE_16],[SCCNQTE_17],[SCCNQTE_18],[SCCNQTE_19],[SCCNQTE_20],[SCCNQTE_21],[SCCNQTE_22],[SCCNQTE_23],[SCCNQTE_24],[SCCNQTE_25],[SCCNQTE_26],[SCCNQTE_27],[SCCNQTE_28],[SCCNQTE_29],[SCCNQTE_30],[SCCNQTE_31],[SCCNQTE_32],[SCCNQTE_33],[SCCNQTE_34],[SCCNQTE_35],[SCCNQTE_36] ";
        sql = sql + ",[SCCNMON_01],[SCCNMON_02],[SCCNMON_03],[SCCNMON_04],[SCCNMON_05],[SCCNMON_06],[SCCNMON_07],[SCCNMON_08],[SCCNMON_09],[SCCNMON_10],[SCCNMON_11],[SCCNMON_12],[SCCNMON_13],[SCCNMON_14],[SCCNMON_15],[SCCNMON_16],[SCCNMON_17],[SCCNMON_18],[SCCNMON_19],[SCCNMON_20],[SCCNMON_21],[SCCNMON_22],[SCCNMON_23],[SCCNMON_24],[SCCNMON_25],[SCCNMON_26],[SCCNMON_27],[SCCNMON_28],[SCCNMON_29],[SCCNMON_30],[SCCNMON_31],[SCCNMON_32],[SCCNMON_33],[SCCNMON_34],[SCCNMON_35],[SCCNMON_36] ";
        sql = sql + ",[SCCNBAS_01],[SCCNBAS_02],[SCCNBAS_03],[SCCNBAS_04],[SCCNBAS_05],[SCCNBAS_06],[SCCNBAS_07],[SCCNBAS_08],[SCCNBAS_09],[SCCNBAS_10],[SCCNBAS_11],[SCCNBAS_12],[SCCNBAS_13],[SCCNBAS_14],[SCCNBAS_15],[SCCNBAS_16],[SCCNBAS_17],[SCCNBAS_18],[SCCNBAS_19],[SCCNBAS_20],[SCCNBAS_21],[SCCNBAS_22],[SCCNBAS_23],[SCCNBAS_24],[SCCNBAS_25],[SCCNBAS_26],[SCCNBAS_27],[SCCNBAS_28],[SCCNBAS_29],[SCCNBAS_30],[SCCNBAS_31],[SCCNBAS_32],[SCCNBAS_33],[SCCNBAS_34],[SCCNBAS_35],[SCCNBAS_36] ";
        sql = sql + ",[SCCTDERCUM]) ";

        sql = sql + "SELECT 'PR','9','100','',b.article_plan,'',aa.ARITNATURE,aa.ARCTTYPART,aa.ARCTCODFAM,'PIC','','','','','','' ";
        sql = sql + ",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ";

        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[0][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[1][2].ToString() + "'  then a.prevision else 0 end)) ";
        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[1][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[2][2].ToString() + "'  then a.prevision else 0 end)) ";
        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[2][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[3][2].ToString() + "'  then a.prevision else 0 end)) ";
        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[3][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[4][2].ToString() + "'  then a.prevision else 0 end)) ";
        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[4][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[5][2].ToString() + "'  then a.prevision else 0 end)) ";
        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[5][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[6][2].ToString() + "'  then a.prevision else 0 end)) ";
        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[6][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[7][2].ToString() + "'  then a.prevision else 0 end)) ";
        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[7][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[8][2].ToString() + "'  then a.prevision else 0 end)) ";
        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[8][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[9][2].ToString() + "'  then a.prevision else 0 end)) ";
        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[9][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[10][2].ToString() + "'  then a.prevision else 0 end))";
        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[10][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[11][2].ToString() + "'  then a.prevision else 0 end)) ";
        sql = sql + ",convert(money,sum(case when a.date_prev>='" + dtm.Rows[11][2].ToString() + "' and  a.date_prev<'" + dtm.Rows[12][2].ToString() + "'  then a.prevision else 0 end)) ";

        sql = sql + ",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ";
        sql = sql + ",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'" + @Tdate_f + "' ";
        sql = sql + "FROM [DW_MASSELIN].[dbo].[PIC_PREVISIONS] a,[DW_MASSELIN].[dbo].[PIC_FAMILLE] b,[PMI].[dbo].[ARTICLE] aa ";
        sql = sql + "where a.code_prev='" + @Ddl_entite + "' and (a.date_prev>='" + dtm.Rows[0][2].ToString() + "' and a.date_prev<'" + dtm.Rows[12][2].ToString() + "') and a.statut='E' ";

        sql = sql + "and b.fam_pic=a.fam_pic ";
        sql = sql + "and aa.ARKTSOC='100' ";
        sql = sql + "and aa.ARKTCODART collate FRENCH_CI_AI=b.article_plan ";
        sql = sql + "group by a.fam_pic,b.article_plan,aa.ARITNATURE,aa.ARCTTYPART,aa.ARCTCODFAM ";
        sql = sql + "order by a.fam_pic,b.article_plan ";

        cmd = cn.CreateCommand();
        cmd.CommandText = sql;
        cmd.CommandTimeout = 0;

        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception e5)
        {
        }

        cn.Close();
    }

提前感谢您的帮助

0 个答案:

没有答案