
string SqlStr = "delete from PersonInfo where id=" + id;这里的id=" + id是什么意思?看不明白。
privatevoidcmdDelete(GridViewg1,GridViewDeleteEventArgse){stringid=g1.DataKeys[e.RowI...
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();
}
} 展开
{
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个回答
展开全部
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的那个字段或者叫列
中的外面的id是引号里面id的参数,也就是说假设id=3,那么此时string sqlStr=“delete from PersonInfo where id=3”,那么在执行这条语句的时候就会将表PersonInfo表中id为3的那条记录删掉。外面的id是变量,要传入sql语句中的值,引号里面的id是PersonInfo表中名字为id的那个字段或者叫列
追问
string sqlStr = "delete from xxx where id = " + id
我直接写所string sqlStr = "delete from xxx where id = id"有没有区别?一样的吗?
追答
有区别,如果直接写入引号内id就不是参数了,你通过string id = g1.DataKeys[e.RowIndex].Value.ToString();获取来的id值就没有办法传递进sql语句中
展开全部
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 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"
就是拼接字符串而已
string sqlStr = "delete from xxx where id = " + id
此时
sqlStr = "delete .... where id = abc"
就是拼接字符串而已
追问
string sqlStr = "delete from xxx where id = " + id
我直接写所string sqlStr = "delete from xxx where id = id"有没有区别?一样的吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询