无法将默认值从 Win Forms 项目插入 SQL 数据库

Cannot insert default value into SQL database from Win Forms project(无法将默认值从 Win Forms 项目插入 SQL 数据库)
本文介绍了无法将默认值从 Win Forms 项目插入 SQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 MS SQL 数据库.

I have a MS SQL database.

在我的一个字段中,LotNumber 我将默认值设置为 [dbo].CalculateJulianDate 这是一个标量值函数,它采用当前日期并创建儒略日期格式的批号.

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.

当我使用 SSMS 将值插入到我的表中时,该值被正确填充,用 LotNumber 替换空值.

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

我正在尝试在 Visual Studio 2010 中为数据库创建一个可视化的基本 winforms 前端.我设置了我的数据源,将详细信息视图拖到表单上,我可以查看数据、编辑数据和删除数据.但是,当我尝试插入新数据时,它会用 NULL 填充 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.

我不明白为什么从我的 winForms 应用程序中输入数据时,MS SQL 不使用默认值而不是 NULL 值.我的印象是,在 SQL 模式中设置默认值的目的是用默认值替换 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

有谁知道为什么 SQL Server 没有像预期的那样插入默认值?如何从我的应用程序中获取要填充的默认值?

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?

推荐答案

要在 TableAdapter 上进行自定义 INSERT 查询,请右键单击您的 TableAdapter,选择添加查询",查询向导将出现.选择Use SQL Statements"并选择INSERT"作为查询类型.

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.

这篇关于无法将默认值从 Win Forms 项目插入 SQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending parameters to stored procedures vb.net(将参数发送到存储过程 vb.net)
Insert multiple rows, count based on another table columns(插入多行,根据另一个表列计数)
How to hold the location of an image in a SQL Server database?(如何在 SQL Server 数据库中保存图像的位置?)
How to send to email (outlook) the selected items in SQL Server database using vb.net(如何使用 vb.net 将 SQL Server 数据库中的选定项目发送到电子邮件(Outlook))
datagrid checkbox writes Null instead of 0 (false) into database(datagrid 复选框将 Null 而不是 0 (false) 写入数据库)
DBCC CheckDb-any ways to detect errors vb.net?(DBCC CheckDb-vb.net 有什么检测错误的方法吗?)