【问题标题】:asp.net sql update reports that it's working, but without real resultsasp.net sql update 报告它正在工作,但没有实际结果
【发布时间】:2016-02-09 16:09:24
【问题描述】:

我有一个长而具体的问题。

我正在创建 asp.net (C#) 网络应用程序。
我已经创建了一个自定义的 DATABASE.cs 类,女巫用于与我在真实服务器上拥有的数据库的所有通信(不是我正在开发的 pc网络应用程序),它工作正常 - 完全没有错误。

我以编程方式在 formReports.aspx 中创建了一个表格,在每一行中,都有一些来自数据库的信息,显示在 (asp.net's)TextBox-es 中,带有按钮 保存删除。在这个问题中,我对为什么“保存”按钮不起作用感兴趣。

了解一些可能有用的事情:
- formReports.aspx 显示在 iFrame 中的另一个 .aspx 页面中。
-保存按钮的点击功能是这样添加的:btn.Click += new EventHandler(Update);之前我将btn.CommandArgument设置为一些我需要保存的变量,用“/”分隔。
-Update 函数工作正常,获取按钮 CommandArgument's,将它们拆分为变量并使用这些调用 DATABASE.cs 函数 Update变量。
-取自 CommandArgument 的变量没问题,就像我需要的那样,所以没有错误。
-DATABASE.cs 不是静态类。
-在 DATABASE.cs 中,我使用的是 SqlConnectionSqlCommand
-我在代码运行时通过逐步检查检查了所有内容。

现在,这是来自 DATABASE.Update 的代码:

public string Update(ReportContent rcOld, string newAccountCodeID, string newAmount)
{
    string queryFrom = "FROM [FinReports].[dbo].[ReportContent] ";
    string queryWhere = 
        " WHERE ReportID = '" + rcOld.ReportID +
        "' AND AccountCodeID = '" + rcOld.AccountCodeID + 
        "' AND Amount = '" + rcOld.Amount.ToString().Replace(',', '.') + "'";

    //check number of rows
    int rowCount;
    string query = "SELECT COUNT(*) " + queryFrom + queryWhere;
    try { rowCount = int.Parse(Single(query)); }
    catch { throw new NotSupportedException(command.CommandText + ""); }

    //throw new NotSupportedException(query + "");

    //return if eny errors are found
    if (rowCount < 1) return "Ne postoji u bazi podataka";
    if (rowCount > 1) return "Postoji vise od 1 reda sa istim celijama";

    //update row
    query = "UPDATE [FinReports].[dbo].[ReportContent] " +
        "SET AccountCodeID = '" + newAccountCodeID + "', Amount = " + newAmount.Replace(',', '.') +
         queryWhere;

    //throw new NotSupportedException(query + "");

    //try {
    if (NonQuery(query) != 1)
        return "Greska";
    //} catch { throw new NotSupportedException(query + ""); }
    return "";
    //}catch{return "Fatalna greska"; }
}

ReportContent.cs 是一个仅用于包含 ReportID、AccountCodeID 和 Amount 的类。当我在 CommandArgument 中创建 EACH 行时填充表时,我将来自此类的信息。
btn.CommandArgument 示例:u/1/220/43267,14 其中:
u - 代表“更新”
1 - 代表表行号(也是从 DATABASE.cs 类中提取的预制列表中的列表索引。
220 - 代表 ReportContent 中的 AccountCodeID(来自真实数据库的内容)
43267,14 - 代表来自 ReportContent 的金额(来自真实数据库的内容)

正如您可能知道的那样,使用throw new NotSupportedException(),我已经准确地完成了 SQL 查询,在真实服务器上进行了尝试,它运行良好 - 没有错误,并且行已更新。

函数 Single()NonQuery() 只需打开连接,执行 ExecuteScalar(),即 ExecuteNonQuery(),然后关闭连接 - 那里没有错误,它们从左上角的单个单元格返回 (Single()) 字符串,即受影响行的 (NonQuery()) nuber。

问题:我单击 保存按钮,调用 formReport.aspx 中的函数 Update,然后调用 DATABASE.Update 函数,该函数返回 空字符串 (""),因为真实数据库中的行已更新,但是当我检查真实数据库时,这些行根本不受影响

一切顺利。我什至补充说,如果 DATABASE.Update 返回“”,则表格行的背景变为黄色 - 如果不是“”,则变为红色(单击它时变为黄色)。

怎么从我的(客户端)端得到更新成功的信息,但是当我到达真实服务器时,刷新结果,我瞄准的行没有受到影响?

【问题讨论】:

  • 在 SQL Server 上运行探查器跟踪,查看更新时从 .net 应用程序发送了哪些命令(如果有)。
  • 你能显示NonQuery()的代码吗?
  • 你应该重写你的类 DATABASE.cs 并开始使用 parameterised queries。字符串连接容易受到恶意 Sql Injection 的影响,但也容易受到格式错误的 sql、类型转换、数据截断和无法利用缓存的查询计划的影响。此更改还应该使调试更容易。正如建议的那样,您可能需要运行跟踪以检查服务器上正在执行的操作。
  • 我将在服务器上运行分析器跟踪,并再次评论结果。 NonQuery 函数: private int NonQuery(string nonQuery) { Open(); command.CommandText = 非查询;诠释我; i = command.ExecuteNonQuery();关闭();返回我; } 其中 Open() 和 Close() 打开/关闭与数据库的连接(工作正常)

标签: c# sql asp.net sql-server windows-server-2008


【解决方案1】:

我在这里回答我自己的问题。

从我在问题中发布的任何内容都看不到问题。
我很抱歉大惊小怪,并且会给出一个回答,以便其他开发人员可以从我的错误中吸取教训。

当我说我正在以编程方式创建 Save 按钮时,在 CommandArgument 中,我将刚刚创建的 TextBox 的当前文本作为参数,因此,对数据库的任何更新或删除都将作为参数具有一个空字符串。

现在,我通过为这些文本框创建唯一 ID,将其作为 CommandArgument 中的参数发送,然后检索该文本框的当前文本,解决了这个问题。

我创建表格的方式 protected void Page_Load(object sender, EventArgs e) { //if (!IsPostBack) { 如果(会话 [“用户”] == 空) { Server.Transfer("formMain.aspx");返回; }

            if (Request.QueryString["reportID"] == null)
            { //error code here 
            }

            database = new DATABASE();

            //load report from db
            reportID = int.Parse(Request.QueryString["reportID"]);
            List<ReportContent> rows = database.ReportContents(reportID);

            //add rows
            for (int i = 0; i < rows.Count; i++)
            report.Rows.Add(TRow(rows[i].AccountCodeID.ToString(),SetDecimals(rows[i].Amount.ToString()), i));
        }
    }

所以,DON'T-CODE-FOR-TRow

public TableRow TRow(string AccountCodeID, string Amount, int rowNo)
    {
        TableCell cell = new TableCell();
        TableRow row = new TableRow();
        Label label = new Label();
        string tbACIDID = "tbAccountCodeID" + rowNo;
        string tbAID = "tbAmount" + rowNo;

        //add row number
        label.Text = (rowNo + 1) + ".";
        cell.Controls.Add(label);
        row.Cells.Add(cell);

        //add AccountCodeID
        TextBox tbac = new TextBox();
        tbac.ID = tbACIDID;
        tbac.Text = AccountCodeID;
        tbac.Width = 50;
        tbac.CssClass = "textbox";
        cell = new TableCell();
        cell.Controls.Add(tbac);
        cell.Attributes.Add("class", "cell");
        row.Cells.Add(cell);

        //add Amount
        tbam = new TextBox();
        tbam.ID = tbAID;
        tbam.Text = Amount;
        tbam.Width = 100;
        tbam.CssClass = "textbox";
        cell.Controls.Add(tbam);
        cell.Attributes.Add("class", "cell");
        row.Cells.Add(cell);

        //add save button
        Button btn = new Button();
        btn.Text = "Sacuvaj";
        btn.CssClass = "saveButton";
        btn.CommandArgument = 
            "u" + separator + 
            rowNo + separator + 
            tbac.Text + separator + 
            tbam.Text;
        btn.Click += new EventHandler(Update);
        cell.Controls.Add(btn);

        //style cell
        cell.Attributes.Add("class", "tableCell");
        row.Cells.Add(cell);

        //style row
        row.Attributes.Add("class", "row");

        return row;
    }



还有 DO-CODE-FOR-TRow

public TableRow TRow(string AccountCodeID, string Amount, int rowNo)
    {
        TableCell cell = new TableCell();
        TableRow row = new TableRow();
        Label label = new Label();
        string tbACIDID = "tbAccountCodeID" + rowNo;
        string tbAID = "tbAmount" + rowNo;

        //add row number
        label.Text = (rowNo + 1) + ".";
        cell.Controls.Add(label);
        row.Cells.Add(cell);

        //add AccountCodeID
        TextBox tb = new TextBox();
        tb.ID = tbACIDID;
        tb.Text = AccountCodeID;
        tb.Width = 50;
        tb.CssClass = "textbox";
        cell = new TableCell();
        cell.Controls.Add(tb);
        cell.Attributes.Add("class", "cell");
        row.Cells.Add(cell);

        //add Amount
        tb = new TextBox();
        tb.ID = tbAID;
        tb.Text = Amount;
        tb.Width = 100;
        tb.CssClass = "textbox";
        cell.Controls.Add(tb);
        cell.Attributes.Add("class", "cell");
        row.Cells.Add(cell);

        //add save button
        Button btn = new Button();
        btn.Text = "Sacuvaj";
        btn.CssClass = "saveButton";
        btn.CommandArgument = 
            "u" + separator + 
            rowNo + separator + 
            tbACIDID + separator + 
            tbAID;
        btn.Click += new EventHandler(Update);
        cell.Controls.Add(btn);

        //style cell
        cell.Attributes.Add("class", "tableCell");
        row.Cells.Add(cell);

        //style row
        row.Attributes.Add("class", "row");

        return row;
    }



所以,你总结一下,这是一个非常愚蠢的错误。但是直到我尝试删除并插入更新的行,我才弄清楚。我希望这个“答案”能节省一些人的时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多