且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在数据库中两次插入查询创建条目。

更新时间:2023-11-28 21:15:58

一些事情:

首先,不要这样做:不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

其次,当您使用DataAdapter并在同一命令上调用ExecuteNonQuery时:

A couple of things:
Firstly, don't do it like that: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Secondly, When you use a DataAdapter and call ExecuteNonQuery on teh same command:
SqlDataAdapter objda = new SqlDataAdapter(objcmd);
objda.Fill(objds);
objcmd.ExecuteNonQuery();



两次插入同一行...



三,从不吞下例外:记录它们,告诉用户,无论如何 - 用它们做点什么。当您使用空的 catch 块时,您隐藏了代码可能出现问题的任何证据,即使是您自己。而且你经常需要它来修复它......


It is going to insert the same row twice...

Thirdy, never "swallow" exceptions: log them, tell the user, whatever - do something with them. When you use an empty catch block you hide any evidence of what your code's problems might be, even from yourself. And you often need that to fix it...