Cannot insert default value into SQL database from Win Forms project

邮差的信 提交于 2019-12-12 02:57:26

问题


I have a MS SQL database.

On one of my fields, LotNumber I set the default value to be [dbo].CalculateJulianDate which is a scalar-valued function that takes the current date and creates a Julian date formatted lot number.

When I insert values into my table using SSMS, the value is populated correctly replacing the empty value with the LotNumber.

I am trying to create a visual basic winforms front end for the database in visual studio 2010. I set my datasource, dragged a details view onto a form and I can see the data, make edits to the data, and delete data. However, when I try to insert new data it fills the LotNumber field with NULL.

I do not understand why MS SQL doesn't use the default value rather than the NULL value when data is entered from within my winForms application. I was under the impression that the point of setting up a default value in the SQL schema was to replace NULL values with the default value.

The code for the insert is auto-generated and I can't seem to find it in my application. The only code visible is:

Private Sub Dt_InventoryBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles Dt_InventoryBindingNavigatorSaveItem.Click
Me.Validate()
Me.Dt_InventoryBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CSRPRDDataSet)
End Sub

Does anyone know why SQL Server doesn't insert the default value like it's supposed to? How do I get the default value to populate from within my application?


回答1:


To make a custom INSERT query on the TableAdapter, right-click on your Table Adapter, choose Add Query and the Query Wizard will appear. Select "Use SQL Statements" and select "INSERT" for the query type.




回答2:


The default value is used in SQL Server when you don't set that field value at all. If you set it to NULL then it stays set to NULL. If you remove that field from your INSERT statement then the default value will be used.



来源:https://stackoverflow.com/questions/25577337/cannot-insert-default-value-into-sql-database-from-win-forms-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!