C# ADO.NET Access 为啥更 新不了表,帮帮忙,在线等

xtgopl 2010-11-14 03:45:51
大哥大姐,帮帮忙,后天教作业呢,还有两层没做了,卡在这里了,谢谢 ^_^

/// <summary>
/// 根据学员ID更新学员信息
/// </summary>
/// <param name="student">学员实体</param>
/// <returns>更新数目</returns>
public int UpdateStudentInfoByID(Student student)
{
int amount = -1;

if ( student.ID == 0 || student.Name == string.Empty )
return amount;

try
{
OleDbCommand command = new OleDbCommand();

command.CommandType = CommandType.Text;
command.Connection = DBAssistant.Connection;
command.Parameters.Add("@id", OleDbType.BigInt).Value = student.ID;
command.Parameters.Add("@name", OleDbType.VarChar, 50).Value = student.Name;
command.Parameters.Add("@sex", OleDbType.Boolean).Value = student.Sex == Genders.Male ? true : false;
command.Parameters.Add("@birthday", OleDbType.DBDate).Value = student.Birthday;
command.Parameters.Add("@mobile", OleDbType.VarChar, 20).Value = student.Mobile;
command.Parameters.Add("@address", OleDbType.VarChar, 50).Value = student.Address;
command.CommandText =
"UPDATE StuInfo SET "
+ "SName = @name, Sex = @sex, Birthday = @birthday, Mobile = @mobile, Address = @address "
+ "WHERE ID = @id;";

DBAssistant.OpenTheConnection(); // 打开
amount = command.ExecuteNonQuery(); // 这里一直是 返回 0 ,数据库里也不变化
}
catch ( Exception exp )
{
Console.WriteLine( exp.Message );
throw;
}
finally
{
DBAssistant.ShutTheConnection();
}

return amount;
} // end method UpdateStudentInfoByID


internal class DALTest
{
static void Main(string [] args)
{
Access.StudentService accStuService = new Access.StudentService(); // 数据层

// 学生对象,参数为:“姓名”、“性别”
Models.Student accModify = new School.Models.Student("更新更新更新", Models.Genders.Male);

accModify.ID = 1; // 学生 ID

accModify.Mobile = "3683297"; // 电话

int accAmount = accStuService.UpdateStudentInfoByID(accModify); // 调用更新方法

Console.WriteLine( accAmount.ToString() ); // 返回受影响的行数 (一直是 0 )为什么更新不了
...全文
85 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
特别 2010-11-14
  • 打赏
  • 举报
回复
[Quote=引用楼主 xtgopl 的回复:]
command.Parameters.Add("@id", OleDbType.BigInt).Value = student.ID;
command.Parameters.Add("@name", OleDbType.VarChar, 50).Value = student.Name;
command.Parameters.Add("@sex", OleDbType.Boolean).Value = student.Sex == Genders.Male ? true : false;
command.Parameters.Add("@birthday", OleDbType.DBDate).Value = student.Birthday;
command.Parameters.Add("@mobile", OleDbType.VarChar, 20).Value = student.Mobile;
command.Parameters.Add("@address", OleDbType.VarChar, 50).Value = student.Address;
command.CommandText =
"UPDATE StuInfo SET "
+ "SName = @name, Sex = @sex, Birthday = @birthday, Mobile = @mobile, Address = @address "
+ "WHERE ID = @id;";
[/Quote]
Access数据库的参数不是用参数名来匹配的
而是用问号(?),必须注意顺序,第一个问号匹配你给出的第一个参数
这些代码要改成



command.Parameters.Add("@name", OleDbType.VarChar, 50).Value = student.Name;
command.Parameters.Add("@sex", OleDbType.Boolean).Value = student.Sex == Genders.Male ? true : false;
command.Parameters.Add("@birthday", OleDbType.DBDate).Value = student.Birthday;
command.Parameters.Add("@mobile", OleDbType.VarChar, 20).Value = student.Mobile;
command.Parameters.Add("@address", OleDbType.VarChar, 50).Value = student.Address;
//这个一定要调到最后,因为你在SQL中最后一个用到
command.Parameters.Add("@id", OleDbType.BigInt).Value = student.ID;
command.CommandText =
"UPDATE StuInfo SET "
+ "SName = ?, Sex = ?, Birthday =?, Mobile = ?, Address = ? "
+ "WHERE ID = ?;";


如果不行,将command.Parameters中的参数名也换成?试试
xtgopl 2010-11-14
  • 打赏
  • 举报
回复

//这个一定要调到最后,因为你在SQL中最后一个用到
command.Parameters.Add("@id", OleDbType.BigInt).Value = student.ID;


sdfkfkd : 谢谢,问题解决了,就是这里!!
SunnyBoy 2010-11-14
  • 打赏
  • 举报
回复
最近也在学数据库,关注!
qkqlqq0288 2010-11-14
  • 打赏
  • 举报
回复
检查一下你的表,是否设置关键字段,如果没有就不能更新和修改

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧