
现在,我创建了该表的上层,为了测试,我将给出3个文本框值,如下所示。
SqlCommand cmd = new SqlCommand
("Insert Into dbo.PrnInf(PrnName, PrnSurName, PrnEgn ) Values
('"+txtPrnName.Text+", "+txtPrnSurName.Text+","+txtPrnEgn.Text+"')", conn);
cmd.ExecuteNonQuery(); 但是,我在"cmd.execute".Also中得到了错误,我在主键上定义了主键和标识规范。这个故事的寓意是,它是否需要填充数据库中的所有表?
诚挚的问候,
发布于 2011-06-22 10:41:42
不良训练警报!
您需要对每个字符串进行分隔。不是所有的人。
注意单引号为:
("Insert Into dbo.PrnInf(PrnName, PrnSurName, PrnEgn ) Values
('"+txtPrnName.Text+"', '"+txtPrnSurName.Text+"','"+txtPrnEgn.Text+"')", conn);然而,这可能导致SQL注入。所以..。
很好的练习!
将查询参数化
SqlCommand cmd = new SqlCommand
("Insert Into dbo.PrnInf(PrnName, PrnSurName, PrnEgn ) Values
(@PrnName, @PrnSurName, @PrnEgn)", conn);
cmd.Parameters.AddWithValue("@PrnName", txtPrnName.Text);
cmd.Parameters.AddWithValue("@PrnSurName", txtPrnSurName.Text);
cmd.Parameters.AddWithValue("@PrnEgn", txtPrnEgn.Text);
cmd.ExecuteNonQuery();https://dba.stackexchange.com/questions/3433
复制相似问题