无法将默认值从 Win Forms 项目插入 SQL 数据库 [英] Cannot insert default value into SQL database from Win Forms project

查看:34
本文介绍了无法将默认值从 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 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆