0

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]
4

3 回答 3

1

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):

tblDeviceDataTable dtService = new tblDeviceDataTable();
dtService.tblLocationID.AllowDbNull = true;
taDevice.FillDataByDeviceSN(dtService,txtDeviceSerial.Text);
于 2009-02-09T19:14:31.860 回答
0

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.

于 2009-02-09T18:52:25.860 回答
0

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.

于 2009-02-09T19:18:07.763 回答