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

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();
}
}

忽然笑
浏览 435回答 3
3回答

元芳怎么了

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();}}

慕的地8271018

string id = "abc";string sqlStr = "delete from xxx where id = " + id此时sqlStr = "delete .... where id = abc"就是拼接字符串而已
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

SQL Server