更新功能不会更新到SQL数据库

编程入门 行业动态 更新时间:2024-10-24 22:22:36
本文介绍了更新功能不会更新到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数据库

本文发布于:2023-10-31 04:06:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544951.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:新到   功能   数据库   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!