【问题标题】:ASP.net c# command.ExecuteNonQuery not saving complete recordASP.net c# command.ExecuteNonQuery 不保存完整记录
【发布时间】:2018-03-04 18:25:14
【问题描述】:

我有一个 ITL_ReportLog 表,当我编写命令时,我必须在其中保存每次打印报告的记录

string tempSql = "";
    if (logCheck == "true") {
        tempSql = reportSql.Replace("'","''");
        tempSql = "INSERT INTO ITL_ReportLog ([username],[runOn],[query]) VALUES ('" + nvc["login"] + "','"+DateTime.Now+"','" + tempSql + "')";
        cmd = new SqlCommand(tempSql, conn);
        cmd.ExecuteNonQuery();
    }
    conn.Close();
    Response.Write(tempSql);

它显示正确的结果,如图所示

enter image description here

但插入后,当我签入 sql 时,它显示一半查询

SELECT 
  'CARTZ Link' AS CompanyName,'Mezzanine Floor, 23-C, Lane 8, 
   Sehar Comm. Area Phase 7 DHA Karachi Pakistan'AS CompanyAddress,
   'admin' login_user,'crm.cartzlink.local' selhost,'admin' sellogin,
   'LEDGER' selreportTitle,'V' selledgerDate,'01-Jul-2015' selfrmDate,
   '23-Sep-2017' seltoDate,'' selaccountId,'' selac_accountId_ac,
   '' selcity,'' selarea,'' selsalesmanId,'S' seldst,
   'A' seldisplayTransaction,'' selgodownId,'D' selsortBy,
   '1' selcurrencyId,'gr_lgr_fcr' seltoprint,'Report

但是当我手动运行查询时,它会完美保存

【问题讨论】:

  • 你的列可能太小了,那么你的插入被截断了
  • 列大小为 4000,顺便说一下 trim 使用 cmd = new SqlCommand(tempSql.Trim(), conn);

标签: c# sql asp.net sql-server visual-studio


【解决方案1】:

您的列大小可能太小,然后您的插入被截断。尝试将您的列查询调整为 ITL_ReportLog 表,或尝试修剪您的变量

【讨论】:

  • 列大小为 varchar(4000)
  • 这是一个大小问题,但只是没有修剪变量;)
【解决方案2】:

在插入代码之前,请尝试将单引号字符替换为其他字符。稍后更新它,使用替换功能。

【讨论】:

  • 插入时会出错,因为我在 cmd = new SqlCommand(tempSql, conn); 中使用相同的查询;
  • tempSql.Trim() 节省了一天:-)
猜你喜欢
  • 1970-01-01
  • 2013-08-11
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多