为了账号安全,请及时绑定邮箱和手机立即绑定

以下代码中的id=" + id是什么意思?看不明白!

以下代码中的id=" + id是什么意思?看不明白!

忽然笑 2021-12-02 11:07:53
private void cmdDelete(GridView g1,GridViewDeleteEventArgs e){string id = g1.DataKeys[e.RowIndex].Value.ToString();string SqlStr = "delete from PersonInfo where id=" + id;using (SqlConnection conn = new SqlConnection(ConnStr)){SqlCommand cmd = new SqlCommand(SqlStr, conn);try{conn.Open();int iValue = cmd.ExecuteNonQuery();if (iValue > 0){cmd.CommandText = "Select * from PersonInfo";SqlDataReader dr = cmd.ExecuteReader();this.GridView1.Caption = "人员信息表";this.GridView1.DataSource = dr;this.GridView1.DataBind();}}
查看完整描述

3 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

string sqlStr = "delete from PersonInfo where id=" + id;
中的外面的id是引号里面id的参数,也就是说假设id=3,那么此时string sqlStr=“delete from PersonInfo where id=3”,那么在执行这条语句的时候就会将表PersonInfo表中id为3的那条记录删掉。外面的id是变量,要传入sql语句中的值,引号里面的id是PersonInfo表中名字为id的那个字段或者叫列

查看完整回答
反对 回复 2021-12-06
?
胡说叔叔

TA贡献1804条经验 获得超8个赞

string id = g1.DataKeys[e.RowIndex].Value.ToString(); //获取 GridView DataKeys 属性的值(当前选中的)
string SqlStr = "delete from PersonInfo where id=" + id; // sql 语句 字符串拼接(就是删除语句, 你可以打个断点停住 看看是什么 你就知道了);

using (SqlConnection conn = new SqlConnection(ConnStr))
{// 以下是查询了, 把修改后的数据 从新从数据库里读出来 在显示
SqlCommand cmd = new SqlCommand(SqlStr, conn);
try
{
conn.Open();
int iValue = cmd.ExecuteNonQuery();
if (iValue > 0)
{
cmd.CommandText = "Select * from PersonInfo";
SqlDataReader dr = cmd.ExecuteReader();
this.GridView1.Caption = "人员信息表";
this.GridView1.DataSource = dr;
this.GridView1.DataBind();
}
}



查看完整回答
反对 回复 2021-12-06
?
慕的地8271018

TA贡献1796条经验 获得超4个赞

string id = "abc";
string sqlStr = "delete from xxx where id = " + id
此时
sqlStr = "delete .... where id = abc"
就是拼接字符串而已

查看完整回答
反对 回复 2021-12-06
  • 3 回答
  • 0 关注
  • 430 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信