我的更新命令在ASP .NET C中不起作用# [英] My update command is not working in ASP .NET C#

查看:66
本文介绍了我的更新命令在ASP .NET C中不起作用#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I need to update the text of a particular coulumn in database each time when i click save button that means it should not get submitted but it should get partially saved in database and next time when i click in that particular id
i should see the text that i previously saved in database the problem is it is not getting updated how can i resolve this


protected void btnsavenow_Click(object sender, EventArgs e)
    {
        if (Editor.Text.Length > 0)
        {
            string text = Editor.Text;
            string idnew = ViewState["ID"].ToString();
            string naenew = Session["uid"].ToString();
            string mnamenew = Session["modID"].ToString();
            string snew = Session["subId"].ToString();
            string anew = Session["audID"].ToString();
            string qucon = "select * from Status where TID='" + idnew + "' and CBy ='" + naenew  + "' and MID='" + mnamenew + "' and SID='" + snew + "' and AID='" + anew + "'";
            SqlCommand cmd = new SqlCommand(qucon, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
  
                lblerrmsg.Text = "want to update";
                string partialtext = "UPDATE Status SET partiallysavedtext = @partiallysavedtext, Completed = 0 Where TID =' " + idnew  + "' and MID='" + mnamenew  + "' and SID='" + snew  + "' and aid='" + anew  + "' and CBy ='" + naenew   + "' ";
                cmd = new SqlCommand(partialtext,con);
                da = new SqlDataAdapter(cmd);
                cmd.Parameters.AddWithValue("@partiallysavedtext", temporarytext.ToString());
                con.Open();
                int i = cmd.ExecuteNonQuery();
                con.Close();
             
            
        }





我的尝试:





What I have tried:

i tried above code but it is not getting updated

推荐答案

不是那样的!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



一旦你完成了整个应用程序并修复了它,你就可以开始使用调试器找出确切的内容在你的WHERE子句中:如果没有更新它,因为WHERE子句与任何记录都不匹配,或者临时文本不是你认为应该的...

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Once you have gone through your whole app and fixed that, you can start using the debugger to find out exactly what is in your WHERE clause: if nothing is updating it's because the WHERE clause does not match any records, or temporarytext is not what you think it should be...


还有一个额外的空间在 TID 参数的更新命令中:

There is an additional space in your update command for the TID parameter:
Where TID =' " + idnew  + "' and MID='"
            ^ here


这篇关于我的更新命令在ASP .NET C中不起作用#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆