如何对数据集运行查询以将不同的列返回到表中?
我正在尝试使用 C# 从数据集中的 SQL 表中提取一些数据。
在这种情况下,我不需要所有列,只需要几个特定的列,但是,由于我没有拉回具有强制 NOT NULL 的列,因此表的副本会引发异常,
"Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."
我确信我可以通过以下方式解决此问题将不可空列返回到我的表中,但是我想避免返回不必要的数据。
我正在使用的引发此异常的查询是
SELECT DeviceSerialNumber, BuildID, LEMSCredentialsID, LEMSSoftwareID, OwnerID, RestagedDate
FROM tblDevice
WHERE (DeviceSerialNumber = @SerialNumber)
This排除强制列“tblLocationID”。 但实际上,只有在将数据库视为一个整体时,此列才是强制性的,而不是当我只需要在表单中使用构建和软件详细信息时。
我尝试按以下方式使用此查询。
private DataTable dtDevice;
dtDevice = taDevice.GetDataByDeviceSN_ForRestage(txtDeviceSerial.Text);
我注意到,在浏览预览数据时,Visual Studio 会绘制 SQL 中未指定的列,包括 tblLocationID 列,但它不会用数据填充这些列。
无论如何,我是否可以在临时表中使用这些数据,而无需导入列的不可空部分? 最好根本不拉动未选择的列?
为了完整起见,这是源表的定义(-减去外键定义):
CREATE TABLE [dbo].[tblDevice](
[DeviceSerialNumber] [nvarchar](50) NOT NULL,
[Model] [nvarchar](50) NULL,
[ManufactureDate] [smalldatetime] NULL,
[CleanBootDate] [smalldatetime] NULL,
[BuildID] [int] NULL,
[Notes] [nvarchar](3000) NULL,
[AuditID] [int] NULL,
[LocationID] [int] NOT NULL,
[SimID] [int] NULL,
[LEMSCredentialsID] [int] NULL,
[LEMSSoftwareID] [int] NULL,
[OwnerID] [int] NULL,
[RestagedDate] [smalldatetime] NULL,
[Boxed] [bit] NULL,
CONSTRAINT [PK_tblDevice_1] PRIMARY KEY CLUSTERED
([DeviceSerialNumber] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
I'm trying to pull some data from a SQL table in my dataset using C#.
In this case I do not need all the columns just a few specific ones, however as I am not pulling back a column with a mandatory NOT NULL, the copy of the table is throwing the exception
"Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."
I'm sure I can work around this by returning the unNullable column to my table however I want to avoid returning unnecessary data.
The query I am using which throws this exception is
SELECT DeviceSerialNumber, BuildID, LEMSCredentialsID, LEMSSoftwareID, OwnerID, RestagedDate
FROM tblDevice
WHERE (DeviceSerialNumber = @SerialNumber)
This excludes the mandatory column "tblLocationID". In reality though, this column is only mandatory when considering the database as a whole, not when I just need build and software detail for use in my form.
I am trying to use this query in the following manner.
private DataTable dtDevice;
dtDevice = taDevice.GetDataByDeviceSN_ForRestage(txtDeviceSerial.Text);
I notice when browsing the preview data, Visual Studio draws columns that are not specified in my SQL including the column tblLocationID it does not however populate these columns with data.
Is there anyway I can use this data in a temporary table without importing the non-nullable aspect of the column? preferably by not pulling through the non-selected columns at all?
For completeness, here's the definition (- minus foreign key definitions) of the source table:
CREATE TABLE [dbo].[tblDevice](
[DeviceSerialNumber] [nvarchar](50) NOT NULL,
[Model] [nvarchar](50) NULL,
[ManufactureDate] [smalldatetime] NULL,
[CleanBootDate] [smalldatetime] NULL,
[BuildID] [int] NULL,
[Notes] [nvarchar](3000) NULL,
[AuditID] [int] NULL,
[LocationID] [int] NOT NULL,
[SimID] [int] NULL,
[LEMSCredentialsID] [int] NULL,
[LEMSSoftwareID] [int] NULL,
[OwnerID] [int] NULL,
[RestagedDate] [smalldatetime] NULL,
[Boxed] [bit] NULL,
CONSTRAINT [PK_tblDevice_1] PRIMARY KEY CLUSTERED
([DeviceSerialNumber] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是一些选项:
仅为您要使用的字段创建“非类型化数据集”。
或者 将
类型化数据集中 NOT NULL 字段的 NullValue 属性从默认的“抛出异常”更改为“空”。
或者
尝试帕特里克建议的将 EnforceConstraints 设置为 False。
Here are a few options:
Create an "untyped dataset" for just the fields you want to use.
OR
Change the NullValue property of the NOT NULL field in your typed dataset from the default of "throw exception" to "empty".
OR
Try what Patrick suggested for setting the EnforceConstraints to False.
该列是强制性的,因为元数据告诉客户端要添加任何新行(或更改行),必须提供该列的值。 如果您不更改数据并且不需要双向映射,那么像 DataReader 这样更轻量级的东西可能更合适。
The column is mandatory because the metadata tells the client that to add any new rows (or alter rows), values for this column have to be provided. If you aren't altering the data and don't need two-way mapping, something more lightweight like a DataReader might be more appropriate.
我假设
设备
是一个表适配器? 使用类型化数据集生成?
您还可以生成“FillDataByDeviceSN”方法(您可以生成 Get 和 Fill),
然后得到(伪代码):
I assume
taDevice
is a tableadapter ? generated with the typed dataset ?
You could also generate the "FillDataByDeviceSN" method (you can generate a Get and a Fill)
then you get (pseudo-ish code):