请问这段写入sql的update代码有什么错误的地方?不管我怎么更新都无法写入数据库中,按下按钮就复原了
privatevoidbutton1_Click(objectsender,EventArgse){using(SqlCommandcmd=newSqlCommand()...
private void button1_Click(object sender, EventArgs e)
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
string select_id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
cmd.CommandText = "update vip.dbo.custom set 姓名='this.textBox2.Text.toString()' where 会员卡号='select_id'";
//"delete from vip.dbo.custom where 会员卡号="+select_id;/
SqlConnection con = new SqlConnection(consqlserver);
con.Open();
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("已经写入数据库");
}
catch
{
MessageBox.Show("写入失败");
}
}
} 展开
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
string select_id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
cmd.CommandText = "update vip.dbo.custom set 姓名='this.textBox2.Text.toString()' where 会员卡号='select_id'";
//"delete from vip.dbo.custom where 会员卡号="+select_id;/
SqlConnection con = new SqlConnection(consqlserver);
con.Open();
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("已经写入数据库");
}
catch
{
MessageBox.Show("写入失败");
}
}
} 展开
2个回答
展开全部
cmd.CommandText = "update vip.dbo.custom set 姓名='this.textBox2.Text.toString()' where 会员卡号='select_id'";
"update vip.dbo.custom set 姓名='“+this.textBox2.Text.toString()+”' where 会员卡号='“+select_id+”'";
楼主明显的少了 “+文本框的值+”
update vip.dbo.custom set 姓名='AAAAA' where 会员卡号=1";这样写肯定是对的但你上面那个可能就不对了。会员卡号是不是整形的呢。如果是整形在强制转convert.toint32(select_id)就没必要加 ' ' 单引号。
"update vip.dbo.custom set 姓名='“+this.textBox2.Text.toString()+”' where 会员卡号='“+select_id+”'";
楼主明显的少了 “+文本框的值+”
update vip.dbo.custom set 姓名='AAAAA' where 会员卡号=1";这样写肯定是对的但你上面那个可能就不对了。会员卡号是不是整形的呢。如果是整形在强制转convert.toint32(select_id)就没必要加 ' ' 单引号。
更多追问追答
追问
会员卡号因为可能是0开头的,所以不是整形,设置为char类型
还有能不能请把这个格式给我说明一下呢,这种语句结构我一直不是很清楚,感觉引号和加号有点混乱了,初学者请见谅。
追答
cmd.CommandText = "update vip.dbo.custom set 姓名='this.textBox2.Text.toString()' where 会员卡号='select_id'";
string a="'0'";
int select_id=convert.toint32(a);
int a=0;
这就是区别在sql中整形还好不用转换直接,字符型就要‘’单引号
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询