我有2个表,即个人资料和信息。
我的桌子看起来像这样:
PROFILE
| p_Id | FirstName | LastName |
| 1 | Jack | Cole |
| 2 | Cynthia | Cole |
| 3 | Robert | Cole |
INFO
| I_Id | childsID | fathersID | mothersID | Country |
| 1 | 1 | 3 | 2 | USA |
我通过在文本框中显示这些表来检索这些值,我的选择查询是:
SELECT p.p_Id, p.FirstName, p.LastName, i.*,
(SELECT pp.FirstName+' '+pp.LastName FROM Profile pp WHERE pp.p_Id=i.childsID) AS child,
(SELECT pp.FirstName+' '+pp.LastName FROM Profile pp WHERE pp.p_Id=i.fathersID) AS father,
(SELECT pp.FirstName+' '+pp.LastName FROM Profile pp WHERE pp.p_Id=i.mothersID) AS mother
FROM Info i
INNER JOIN Profile p
ON p.p_Id=i.childsID
选择没问题,我能够在文本框上显示值,但问题是,我无法更新它们,到目前为止我已尝试过:
using (SqlCommand cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = @"UPDATE Profile SET FirstName=@fname, LastName=@lname WHERE p_Id = @pid;
UPDATE Info SET childsID=@child, fathersID=@father, mothersID=@mother, Country=@country WHERE I_Id = @iid;";
cmd.Parameters.Add(new SqlParameter("@fname", txtfname.Text));
cmd.Parameters.Add(new SqlParameter("@lname", txtlname.Text));
cmd.Parameters.Add(new SqlParameter("@child", txtchild.Text));
cmd.Parameters.Add(new SqlParameter("@father", txtfather.Text));
cmd.Parameters.Add(new SqlParameter("@mother", txtmother.Text));
cmd.Parameters.Add(new SqlParameter("@country", txtcountry.Text));
cmd.Parameters.Add(new SqlParameter("@pid", txtpid.Text));
cmd.Parameters.Add(new SqlParameter("@iid", txtiid.Text));
cmd.ExecuteNonQuery();
Response.Write("alert('DATA UPDATED')");
}
我正在使用c#和Asp.net 在此先感谢:)上帝保佑
答案 0 :(得分:0)
检查MSDN:SqlParameter Constructor (String, Object)
在value参数中指定Object时,将从Object .NET的Microsoft .NET Framework类型推断出SqlDbType。
因此,当您传递int类型参数时,需要将文本值转换为整数:
cmd.Parameters.Add(new SqlParameter("@pid", int.Parse(txtpid.Text)));
或更好:
int tempIntValue;
cmd.Parameters.Add(new SqlParameter("@pid", int.TryParse(txtpid.Text, out tempIntValue)?
(object)intTempValue: (object)DbNull.Value ));