请各位前辈看一下下面这段C#代码,update方法的参数如何传递的?

bookworm1987 2012-03-20 03:43:29
代码如下:我想知道这里显示传入的是一个表employee,具体的各个列的参数如何传入的?


//// Update the Employee by ID.// This method assumes that ConflictDetection is set to OverwriteValues.publicint UpdateEmployee(NorthwindEmployee employee)
{
if (String.IsNullOrEmpty(employee.FirstName))
thrownew ArgumentException("FirstName cannot be null or an empty string.");
if (String.IsNullOrEmpty(employee.LastName))
thrownew ArgumentException("LastName cannot be null or an empty string.");

if (employee.Address == null) { employee.Address = String.Empty; }
if (employee.City == null) { employee.City = String.Empty; }
if (employee.Region == null) { employee.Region = String.Empty; }
if (employee.PostalCode == null) { employee.PostalCode = String.Empty; }

SqlConnection conn = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand("UPDATE Employees " +
" SET FirstName=@FirstName, LastName=@LastName, " +
" Address=@Address, City=@City, Region=@Region, " +
" PostalCode=@PostalCode " +
" WHERE EmployeeID=@EmployeeID", conn);

cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 10).Value = employee.FirstName;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 20).Value = employee.LastName;
cmd.Parameters.Add("@Address", SqlDbType.VarChar, 60).Value = employee.Address;
cmd.Parameters.Add("@City", SqlDbType.VarChar, 15).Value = employee.City;
cmd.Parameters.Add("@Region", SqlDbType.VarChar, 15).Value = employee.Region;
cmd.Parameters.Add("@PostalCode", SqlDbType.VarChar, 10).Value = employee.PostalCode;
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = employee.EmployeeID;

int result = 0;

try
{
conn.Open();

result = cmd.ExecuteNonQuery();
}
catch (SqlException e)
{
// Handle exception.
}
finally
{
conn.Close();
}

return result;
}

...全文
126 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
porschev 2012-03-20
  • 打赏
  • 举报
回复

你传入一个employee就可以了
SomethingJack 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 truecoffeefox 的回复:]

cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 10).Value = employee.FirstName;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 20).Value = employee.LastName;
cmd.Parameters.A……
[/Quote]
+1
truecoffeefox 2012-03-20
  • 打赏
  • 举报
回复
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 10).Value = employee.FirstName;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 20).Value = employee.LastName;
cmd.Parameters.Add("@Address", SqlDbType.VarChar, 60).Value = employee.Address;
cmd.Parameters.Add("@City", SqlDbType.VarChar, 15).Value = employee.City;
cmd.Parameters.Add("@Region", SqlDbType.VarChar, 15).Value = employee.Region;
cmd.Parameters.Add("@PostalCode", SqlDbType.VarChar, 10).Value = employee.PostalCode;
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = employee.EmployeeID;


在这里就是嘛
xiangzaopao 2012-03-20
  • 打赏
  • 举报
回复
实体类
xuan.ye 2012-03-20
  • 打赏
  • 举报
回复
封装的真的挺好的,自己一般就是写个sql语句就更新了,更多的时候真的很怕麻烦。
bdmh 2012-03-20
  • 打赏
  • 举报
回复
cmd.Parameters.Add,这不就是传递参数吗,sql语句中带 @ 符号的都是变量
shawn771 2012-03-20
  • 打赏
  • 举报
回复
cmd.Parameters.Add("@...", SqlDbType.VarChar, 10).Value = 值;

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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