简体   繁体   English

尝试插入vb.net应用程序时出错

[英]Error when trying to INSERT INTO vb.net app

I have columns in my database called ModifyUser and ModifyDate that are supposed to show the username and date and time of the last person that has updated a specific product somehow. 我的数据库中有名为ModifyUser和ModifyDate的列,这些列应显示以某种方式更新了特定产品的最后一个用户的用户名以及日期和时间。

I thought if I just added the second query then I would be fine, but for some reason the application wants to add ProductName into the Product table too. 我以为如果我只添加第二个查询,那会很好,但是由于某种原因,应用程序也希望将ProductName添加到Product表中。 Why isn't the application just inserting the 2 values I told it to insert in the Product table INSERT? 为什么应用程序不只是插入我告诉它要插入到Product表INSERT中的2个值?

Error message: Cannot insert the value NULL into column 'ProductName', table
'Products.dbo.Product'; column does not allow nulls. INSERT fails.
The statement has been terminated.

'SQL INSERT: CompanyLink Table
        Dim strSQL As String = "INSERT INTO CompanyLink (ProductID, CompanyID) 
        VALUES (@ProductID, @CompanyID);INSERT INTO Product (ModifyDate, ModifyUser)
        VALUES (getdate(), @ModifyUser)"

    Using cn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)

    Using cmd As New SqlCommand(strSQL, cn)

    cmd.Parameters.Add(New SqlParameter("@ProductID", ProductID.Value))
    cmd.Parameters.Add(New SqlParameter("@CompanyID", company.Value))
    cmd.Parameters.Add(New SqlParameter("@ModifyUser",
    System.Web.HttpContext.Current.User.Identity.Name))

    cn.Open()
    cmd.ExecuteNonQuery()

Looks like the ProductName column of the Product table has been defined as NOT NULL . 看起来Product表的ProductName列已定义为NOT NULL

That means that you have to supply a value for it. 这意味着您必须为其提供价值。 This ensures that you don't have products that do not have a name. 这样可以确保您没有没有名字的产品。

Since you are not supplying a ProductName in this query: 由于您没有在此查询中提供ProductName

INSERT INTO Product (ModifyDate, ModifyUser)
    VALUES (getdate(), @ModifyUser)

The statement fails. 该语句失败。

I expect there are other NOT NULL columns on the same table. 我希望同一张表上还有其他NOT NULL列。

The error message is pretty clear: your are not providing ProductName , which is a required ( NOT NULL ) field. 错误消息非常清楚:您没有提供ProductName ,这是必填字段( NOT NULL )。

In order to fix it, you have to provide the ProductName , modifying this: 为了修复它,您必须提供ProductName ,对此进行修改:

INSERT INTO Product (ModifyDate, ModifyUser, ProductName)
    VALUES (getdate(), @ModifyUser, @ProductName)

and add this: 并添加以下内容:

cmd.Parameters.Add(New SqlParameter("@ProductName", SomeVariableContainingProductName))

Update 更新资料

Since what you really need is an update, your statement will be: 由于您真正需要的是更新,因此您的陈述将是:

Update product
set ModifyDate = getdate(),
    ModifyUser = @ModifyUser
where ProductID = @ProductID

Remember to provide all parameters values for the query. 记住要提供查询的所有参数值。

ProductName in Product does not accept null. 产品中的ProductName不接受null。 you would either have to pass in ProductName or change the column property to accept null values as below 您要么必须传递ProductName,要么将column属性更改为接受空值,如下所示

ALTER TABLE Product ALTER COLUMN ProductName nvarchar(50) NULL ALTER TABLE产品ALTER COLUMN产品名称nvarchar(50)NULL

如果“产品”表中的“产品名称”字段设置为“非空”,则数据库将不接受产品表中的任何记录,除非它们包含“产品名称”字段的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM