通过在名称上给出条件来更新名称和其他记录 [英] Update name and other records by giving condition on name
问题描述
我正在从&我希望通过在名称上给出条件来更新名称和其他记录。
例如脚本 - >更新tbl_update_record set name = @ Name,mob_no = @ Mobile_No,address = @ Address where name = @ Name
我尝试过:
i am working on windows from & in which i want to update name and other records by giving condition on name.
e.g. script--> update tbl_update_record set name=@Name,mob_no=@Mobile_No, address=@Address where name=@Name
What I have tried:
public void LBUpdate_Click(object sender, EventArgs e)
{
cn = new SqlConnection(cs);
cn.Open();
SqlCommand cmd = new SqlCommand("Sp_Register_Login_Update", cn);
cmd.Parameters.AddWithValue("@Name", txtfname.Text);
cmd.Parameters.AddWithValue("@Mobile_No", txtmobileno.Text);
cmd.Parameters.AddWithValue("@Address", txtaddress.Text);
cmd.CommandType = CommandType.StoredProcedure;
int j=cmd.ExecuteNonQuery();
if (j != 0)
{
lblmsg.text("Record Updated Successfully..");
}
}
推荐答案
1)为什么要设置列的值你知道必须等于这个值吗?
1) Why are you setting the value of a column that you know must equal the value already?
UPDATE tbl_update_record SET name=@Name, ... WHERE name=@Name
2)连接,命令等等都是稀缺资源,你应该在完成它们后关闭并处理它们。
使用块是最简单的方式:
2) Connections, Commands, and so forth are scarce resources, you should Close and Dispose them when you are finished with them.
A using
block is the simplest way:
using (cn = new SqlConnection(cs))
{
cn.Open();
using (SqlCommand cmd = new SqlCommand("Sp_Register_Login_Update", cn))
{
cmd.Parameters.AddWithValue("@Name", txtfname.Text);
cmd.Parameters.AddWithValue("@Mobile_No", txtmobileno.Text);
cmd.Parameters.AddWithValue("@Address", txtaddress.Text);
cmd.CommandType = CommandType.StoredProcedure;
int j=cmd.ExecuteNonQuery();
if (j != 0)
{
lblmsg.text("Record Updated Successfully..");
}
}
}
3)如果该代码无效,请检查存储过程,然后使用调试器确切地看到 txtfname.Text
中的内容以及表格的 name
列。
3) If that code isn't working, check your stored procedure, then use the debugger to see exactly what is in txtfname.Text
and the name
column of your table.
这篇关于通过在名称上给出条件来更新名称和其他记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!