且构网

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

INSERT语句到SQL Server数据库

更新时间:2023-02-08 17:47:46

有是那样的插入三种可能的情况:

There is three possible scenarios for an insert like that:


  1. 插入成功。

  2. 您得到一个异常。

  3. 您有一个触发器,它取代了与其他一些动作插入。

我猜你没有触发,和你没有得到一个记录该表中,必须有一个例外。

I guess that you don't have a trigger, and as you don't get a record in the table, there has to be an exception.

你有没有在任何其他级别捕获异常的代码?这可以解释为什么你没有看到它,它也将离开数据库连接未关闭的,这可以解释为什么在连接到后来的数据库问题。

Do you have any code that catches the exception at any other level? That would explain why you don't see it, and it would also leave the database connection unclosed, which would explain why you have problems connecting to the database afterwards.

使用一个使用数据库连接块将正确地关闭它,即使没有在代码中的错误。

Using a using block for the database connection would close it properly even if there is an error in the code.

您正在使用参数化查询,但我不能看到你在代码中的参数添加到命令对象的任何地方。这将是这样的:

You are using a parameterised query, but I can't see that you add the parameters to the command object anywhere in the code. That would be something like:

cmd.Parameters.Add("Price", SqlDbType.Decimal).Value = price;
cmd.Parameters.Add("User", SqlDbType.NChar, 20).Value = user;
cmd.Parameters.Add("Time", SqlDbType.NChar, 15).Value = time;
cmd.Parameters.Add("Customer", SqlDbType.NChar, 10).Value = customer;
cmd.Parameters.Add("Discount", SqlDbType.Decimal).Value = discount;
cmd.Parameters.Add("FullPrice", SqlDbType.Decimal).Value = fullPrice;