尝试在ASP.NET中更新SQL查询但没有错误,没有更新 [英] Trying to update SQL query in ASP.NET but no errors, no update

查看:71
本文介绍了尝试在ASP.NET中更新SQL查询但没有错误,没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用asp.net中的更新查询来更新用户个人资料详细信息。但没有更新也没有错误显示。我试着从2天到现在来这个问题。请帮我。我的代码在这里:



我的尝试:



i am trying to update user profile details using update query in asp.net. But no update also no error showing. i am trying from 2 days to over come this problem. please help me. my code is here:

What I have tried:

con.Open();

method 1:
    id is primary key in sql server database.

        string cmd = "UPDATE login SET firstname=@fname, lastname=@lname, username=@username, email=@email, phone=@phone, gender=@gender, professionality=@pro where id=@id";
        SqlCommand sql = new SqlCommand(cmd,con);
        sql.Parameters.AddWithValue("@id",id);
        sql.Parameters.AddWithValue("@fname", fname.Text);
        sql.Parameters.AddWithValue("@lname", lname.Text);
        sql.Parameters.AddWithValue("@username", username.Text);
        sql.Parameters.AddWithValue("@email", email.Text);
        sql.Parameters.AddWithValue("@phone", phone.Text);
        sql.Parameters.AddWithValue("@gender",gender.SelectedItem+"");
        sql.Parameters.AddWithValue("@pro", pro.Text);
        sql.ExecuteNonQuery();
        con.Close();


method 2:
         here session["user"] is not primary key. but username column having unique data.

        con.Open();
        SqlCommand cmd = new SqlCommand("update login set firstname='" + fname.Text + 
        "',lastname='" + lname.Text + "'username='" + username.Text + "',email='" + 
        email.Text + "',phone='" + phone.Text + "',gender='" + gender.SelectedItem + 
        "',professionality='" + pro.Text + "' where username='"+Session["user"]+"'", 
        con);
        cmd.ExecuteNonQuery();
        con.Close();

推荐答案

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



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

Don't use method 2. Never use method 2. 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

--'

其他一切都是评论。

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



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



如果您的代码没有收到错误但数据库没有得到更新,那么这意味着UPDATE WHERE子句没有匹配。

为什么不呢?嗯,这是个大问题!可能是您传递的ID值不是您想的,或者您可能正在访问错误的数据库,或者您设置的新值与旧值相同。我们无法分辨。



所以,它取决于你。

幸运的是,你有一个工具可供你使用将帮助您了解正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。查看您的 id 变量并确切地检查它包含的内容 - 它是您期望的ID吗?如果不是,为什么不呢?

如果是,请使用SSMS查看数据库内容并检查该行。它存在吗?它目前的价值是多少?它们与您传递的值有什么不同吗?

然后单步执行每一行检查您预期发生的情况究竟是什么。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。
我也想看看SQL操作的返回值:

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?

If your code is not getting an error but the database is not getting updated, then that means the UPDATE WHERE clause matched no rows.
Why not? Well, that's the big question! It could be that the ID value you are passing is not what you thought, or you may be accessing the wrong DB, or the new values you are setting them to are the same as the old ones. We can't tell.

So, its going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Look at your id variable and check exactly what it contains - is it the ID you expect? If it isn't, why not?
If it is, use SSMS to look at the DB content and examine that row. Does it exist? What are its current values? Are they different to the values you are passing?
Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
I'd also want to look at the return value from the SQL operation:

int rowsAffected = sql.ExecuteNonQuery();





很抱歉,但我们不能为您做到这一点 - 时间让您学习一种新的(非常非常有用的)技能:调试!



Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


检查cmd中的查询字符串并从查询分析器中复制查询sting,然后检查查询。
check query string in "cmd" and copy query sting from your code and past in query analyzer then check your query.


这篇关于尝试在ASP.NET中更新SQL查询但没有错误,没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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