[英]Error when trying to INSERT INTO vb.net app
我的数据库中有名为ModifyUser和ModifyDate的列,这些列应显示以某种方式更新了特定产品的最后一个用户的用户名以及日期和时间。
我以为如果我只添加第二个查询,那会很好,但是由于某种原因,应用程序也希望将ProductName添加到Product表中。 为什么应用程序不只是插入我告诉它要插入到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()
看起来Product
表的ProductName
列已定义为NOT NULL
。
这意味着您必须为其提供价值。 这样可以确保您没有没有名字的产品。
由于您没有在此查询中提供ProductName
:
INSERT INTO Product (ModifyDate, ModifyUser)
VALUES (getdate(), @ModifyUser)
该语句失败。
我希望同一张表上还有其他NOT NULL
列。
错误消息非常清楚:您没有提供ProductName
,这是必填字段( NOT NULL
)。
为了修复它,您必须提供ProductName
,对此进行修改:
INSERT INTO Product (ModifyDate, ModifyUser, ProductName)
VALUES (getdate(), @ModifyUser, @ProductName)
并添加以下内容:
cmd.Parameters.Add(New SqlParameter("@ProductName", SomeVariableContainingProductName))
更新资料
由于您真正需要的是更新,因此您的陈述将是:
Update product
set ModifyDate = getdate(),
ModifyUser = @ModifyUser
where ProductID = @ProductID
记住要提供查询的所有参数值。
产品中的ProductName不接受null。 您要么必须传递ProductName,要么将column属性更改为接受空值,如下所示
ALTER TABLE产品ALTER COLUMN产品名称nvarchar(50)NULL
如果“产品”表中的“产品名称”字段设置为“非空”,则数据库将不接受产品表中的任何记录,除非它们包含“产品名称”字段的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.