ASP.NET页面插入数据库时出错
提示:TherearemorecolumnsintheINSERTstatementthanvaluesspecifiedintheVALUESclause.Thenum...
提示:There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
代码段:
string sql1 = "insert into StudentInfo(Id, name, gender, class, address, nation, politics, profession, link, addressId, more ) ";
string sql2 = "VALUES ('"+ ID.Text.Trim()+"','"+name.Text.Trim()+"','"+gender.Text.Trim()+"','"+tclass.Text.Trim()+','+address.Text.Trim()+"','"+nation.Text.Trim()+"','"+politics.Text.Trim()+"','"+profession.Text.Trim()+"','"+link.Text.Trim()+"','"+addressID.Text.Trim()+"','"+more.Text.Trim() + "')";
string sql = sql1 + sql2;
SqlCommand cmd = new SqlCommand(sql, conn);
message.Text = Convert.ToString(cmd.ExecuteNonQuery());
我的字段明明和values一样多的呀~求救啊 展开
代码段:
string sql1 = "insert into StudentInfo(Id, name, gender, class, address, nation, politics, profession, link, addressId, more ) ";
string sql2 = "VALUES ('"+ ID.Text.Trim()+"','"+name.Text.Trim()+"','"+gender.Text.Trim()+"','"+tclass.Text.Trim()+','+address.Text.Trim()+"','"+nation.Text.Trim()+"','"+politics.Text.Trim()+"','"+profession.Text.Trim()+"','"+link.Text.Trim()+"','"+addressID.Text.Trim()+"','"+more.Text.Trim() + "')";
string sql = sql1 + sql2;
SqlCommand cmd = new SqlCommand(sql, conn);
message.Text = Convert.ToString(cmd.ExecuteNonQuery());
我的字段明明和values一样多的呀~求救啊 展开
2个回答
展开全部
那么你输出的SQL语句内容是什么呢?
最好不要使用这种将用户的输入直接拼接成SQL语句的方法,这样很容易被别人利用拼接SQL实现SQL注入的。应该使用参数化的查询SQL, 在执行时再将用户的输入值作为参数值传入, 就是使用SqlCommand.Parameters来传入参数, 示例:
SqlCommand cmd = new SqlCommand('', conn);
cmd.CommandText = @"insert into StudentInfo (Id,name, gender, class, address, nation, politics, profession, link, addressId, more)
values (@Id,@name, @gender, @class, @address, @nation, @politics, @profession, @link, @addressId, @more)";
cmd.Parameters.Add(@Id", SqlDbType.NVarChar, 100);
cmd.Parameters[0].Value = ID.Text.Trim();
// ...逐个添加参数
cmd.ExecuteNonQuery(); // 执行SQL语句
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询