简体   繁体   English

带参数的更新命令不会更新数据表

[英]Update command with parameters is not updating data table

The whole night I've tried many ways BUT sql Update command with parameters is not updating data table. 整个晚上,我尝试了多种方法,但是带有参数的sql Update命令不会更新数据表。 I am totally fade up. 我完全淡了。 The execution is successful and returns 1 row affected BUT old data is not being replaced. 执行成功并返回1行受影响的BUT旧数据,但该数据不会被替换。 Please help me out. 请帮帮我。

conn.Open();
string updateSql = "UPDATE tbl_cust SET fname=@fname, lname=@lname WHERE email=@email";  
SqlCommand UpdateCmd = new SqlCommand(updateSql, conn);

UpdateCmd.Parameters.Add("@fname", SqlDbType.NVarChar, 50, "fname");
UpdateCmd.Parameters.Add("@lname", SqlDbType.NVarChar, 50, "lname");
UpdateCmd.Parameters.Add("@email", SqlDbType.NVarChar, 50, "email");

UpdateCmd.Parameters["@fname"].Value = txtFirstName.Text;
UpdateCmd.Parameters["@lname"].Value = txtLastName.Text;
UpdateCmd.Parameters["@email"].Value = Session["sesEmail"].ToString();         
UpdateCmd.ExecuteNonQuery(); 

Another method: (same result) 另一种方法:(结果相同)

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Hib"].ConnectionString);
string upData = "UPDATE tbl_cust SET fname=@fname, lname=@lname, addrs=@address WHERE email=@email";

SqlCommand cmd = new SqlCommand(upData, conn);
conn.Open();

cmd.Parameters.AddWithValue("@fname", fName);
cmd.Parameters.AddWithValue("@lname", lName);
cmd.Parameters.AddWithValue("@addrs", address);
cmd.Parameters.AddWithValue("@email", Session["sesEmail"].ToString());

int n = cmd.ExecuteNonQuery();
Response.Write("<script>alert('rows affected = " + n + "');</script>");
Response.Write("<script>alert('sesEmail = " + Session["sesEmail"].ToString() + "');</script>");

I would debug this by creating a stored procedure that accepts the parameters, then I would run the procedure manually from SSMS (SQL Server Management Studio). 我将通过创建一个接受参数的存储过程来调试它,然后从SSMS(SQL Server Management Studio)手动运行该过程。 Confirm that the procedure itself, given the correct parameters, works, then call the procedure instead of inline SQL from the front end (this is a better approach anyway). 确认给定正确参数的过程本身可以正常工作,然后从前端调用过程而不是内联SQL(无论如何,这是一种更好的方法)。

IE IE浏览器

CREATE PROCEDURE usp_UpdateCustTable
@fname varchar(50),
@lname varchar (50),
@email varchar(50) 

AS

--EXEC usp_UpdateCustTable 'Joe', 'Blow', 'joe.blow@acmecompany.com'


UPDATE tbl_cust SET fname=@fname, lname=@lname WHERE email=@email

After you create the proc, run the commented out EXEC portion of the sample. 创建proc后,运行示例的注释掉的EXEC部分。 Once you know the procedure is working, the issue can only boil down to the Session variable being empty, invalid connect string, or some related issues. 一旦知道该程序正在运行,问题就可以归结为Session变量为空,无效的连接字符串或某些相关问题。 Have you set a break point and monitored the code? 您是否设置了断点并监控了代码?

Let me know; 让我知道; I feel your pain, :-) 我感到你的痛苦,:-)

And the magic is...Problem solved. 而魔术就是...问题解决了。 Thank you, everyone. 谢谢大家。 You are all very helpful. 你们都非常有帮助。 It was my very first post here and I'm so happy to see the prompt response from the expert panel. 这是我的第一篇文章,我很高兴看到专家小组的及时回应。 Thanks again. 再次感谢。

if (!IsPostBack)
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM