更新功能不会更新到SQL数据库 [英] Update function does not update to SQL database

查看:108
本文介绍了更新功能不会更新到SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多方法来更新学生详细信息列表。我仍然无法更新到sql server。



我尝试过:



I try lots of way to update into student detail list. I still can't update to the sql server.

What I have tried:

<pre> protected void gvStudent_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    conn.Open();
                    string query = "UPDATE Student SET Name=@Name,Photo=@Photo,Course=@Course,EmailAddr=@EmailAddr,Password=@Password,Status=@Status,MentorID=@MentorID WHERE StudentID = @id";
                    SqlCommand cmd = new SqlCommand(query, conn);
                    cmd.Parameters.AddWithValue("@Name", (gvStudent.Rows[e.RowIndex].FindControl("txtName") as TextBox).Text.Trim());
                    cmd.Parameters.AddWithValue("@Photo", (gvStudent.Rows[e.RowIndex].FindControl("txtPhoto") as TextBox).Text.Trim());
                    cmd.Parameters.AddWithValue("@Course", (gvStudent.Rows[e.RowIndex].FindControl("txtCourse") as TextBox).Text.Trim());
                    cmd.Parameters.AddWithValue("@EmailAddr", (gvStudent.Rows[e.RowIndex].FindControl("txtEmail") as TextBox).Text.Trim());
                    cmd.Parameters.AddWithValue("@Password", (gvStudent.Rows[e.RowIndex].FindControl("txtPassword") as TextBox).Text.Trim());
                    cmd.Parameters.AddWithValue("@Status", (gvStudent.Rows[e.RowIndex].FindControl("txtStatus") as TextBox).Text.Trim());
                    cmd.Parameters.AddWithValue("@MentorID", (gvStudent.Rows[e.RowIndex].FindControl("txtMentorID") as TextBox).Text.Trim());
                    cmd.Parameters.AddWithValue("@id", Convert.ToInt32(gvStudent.DataKeys[e.RowIndex].Value.ToString()));
                    cmd.ExecuteNonQuery();
                    gvStudent.EditIndex = -1;
                    getStudentDetails();
                    lblSuccessMessage.Text = "Selected Record Updated";
                    lblErrorMessage.Text = "";
                }
            }
            catch (Exception ex)
            {
                lblSuccessMessage.Text = "";
                lblErrorMessage.Text = ex.Message;
            }
        }

推荐答案

没有您的数据,就无法分辨。

但是......你有你的数据,而且你有一个调试器。结合两个,你应该能够分辨。

做两件事。

1)改变这一行:
Without your data, it's impossible to tell.
But ... you have your data, and you have a debugger. Combine teh two and your should be able to tell.
So do two things.
1) Change this line:
cmd.ExecuteNonQuery();

对此:

int changed = cmd.ExecuteNonQuery();

并在其上放置一个断点。

当你运行您的应用程序,使用断点查看您作​​为 @id 参数传递给SQL的值。然后单行该行,并检查的值 - 如果它为零,则没有行被更改。



通常,当UPDATE没有做任何事情而没有任何异常时,这是因为WHERE子句没有匹配任何行 - 所以一旦你使用调试器确切地检查你传递的是什么(而不是你认为你是什么通过)你可以检查数据库,找出它不匹配的原因。


抱歉,我们不能为你做任何事 - 我们没有完全访问你的数据!

and put a breakpoint on it.
When you run your app, use the breakpoint to look at the value you are passing to SQL as the @id parameter. Then single step the line, and check the value of changed - if it's zero, then no rows were changed.

Normally, when an UPDATE "doesn't do anything" without any exception, it's because the WHERE clause matched no rows - so once you've used the debugger to check exactly what you are passing (rather than what you think you are passing) you can examine the DB to find out why it isn't matching.

Sorry, but we can't do any of that for you - we have no access to your data at all!


你应该在尝试更新之前检索学生;为了证明:



1)学生存在

2)你的连接字符串有效

3)数据库在线

4)你的查询逻辑工作

5)Blah,blah
You're supposed to "retrieve" the student before trying to update; in order to prove:

1) The student exists
2) Your "connection string" works
3) The database is online
4) Your query logic works
5) Blah, blah


这篇关于更新功能不会更新到SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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