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的那个字段或者叫列
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();
}
}
string id = "abc";
string sqlStr = "delete from xxx where id = " + id
此时
sqlStr = "delete .... where id = abc"
就是拼接字符串而已