I have a documents table in my database. The document table structure is like
CREATE TABLE [dbo].[Documents]
(
[DocumentId] [BIGINT] NOT NULL IDENTITY(1, 1),
[ObjectType] [VARCHAR](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[ObjectId] [BIGINT] NOT NULL,
[DocumentName] [VARCHAR](250) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DocumentFile] [VARBINARY](MAX) NOT NULL,
[isTemp] [BIT] NOT NULL CONSTRAINT [DF_Documents_isTemp] DEFAULT ((0)),
[IsActive] [BIT] NOT NULL,
[RowGuid] [UNIQUEIDENTIFIER] NOT NULL CONSTRAINT [DF_Documents_RowGuid] DEFAULT (newid()),
[CreatedBy] [BIGINT] NOT NULL,
[CreatedOn] [DATETIME] NOT NULL,
[UpdatedBy] [BIGINT] NULL,
[UpdatedOn] [DATETIME] NULL
)
GO
-- Constraints and Indexes
ALTER TABLE [dbo].[Documents]
ADD CONSTRAINT [PK_Documents]
PRIMARY KEY CLUSTERED ([DocumentId])
GO
CREATE NONCLUSTERED INDEX [ix_DocumentName]
ON [dbo].[Documents] ([DocumentName])
GO
CREATE NONCLUSTERED INDEX [ix_ObjectId]
ON [dbo].[Documents] ([ObjectId])
GO
CREATE NONCLUSTERED INDEX [ix_ObjectType]
ON [dbo].[Documents] ([ObjectType])
GO
In the documents table there are 100k records.
This is the C# method which is fetching single document on the basis of objectid.
public IQueryable<Document> GetDocumentData(long objectId, string objectType)
{
var searchResults = new MerrickEntities().Documents.Where(c => c.ObjectId == objectId && c.ObjectType == objectType && SqlFunctions.DataLength(c.DocumentFile) > 0);
return searchResults.AsQueryable();
}
In this query is not fetching data and it seems the issue is caused by datalength function.
How can I optimize this query?
I'm creating a meeting program in c# using SQL. How would i go about adding multiple users from the user table to one meeting and vice versa (multiple meetings to one user from the table)
Sorry I'm new to SQL
SQL DB Format: User
[UserID] VARCHAR (50) NOT NULL,
[FirstName] VARCHAR (50) NOT NULL,
[LastName] VARCHAR (50) NOT NULL,
[Username] VARCHAR (50) NOT NULL,
[Password] VARCHAR (50) NOT NULL,
[Exc1] BIT NULL,
[Exc2] BIT NULL,
[Exc3] BIT NULL,
[Exc4] BIT NULL,
[Exc5] BIT NULL,
[Importance] INT NOT NULL,
[Meetings] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([UserID] ASC),
CONSTRAINT [Meetings] FOREIGN KEY ([Meetings]) REFERENCES [dbo].[Meeting] ([MeetingID])
Meeting
[MeetingID] VARCHAR (50) NOT NULL,
[Title] VARCHAR (50) NOT NULL,
[Date] DATE NOT NULL,
[StartTime] TIME (7) NOT NULL,
[EndTime] TIME (7) NOT NULL,
[Location] VARCHAR (50) NOT NULL,
[Exc1] BIT NULL,
[Exc2] BIT NULL,
[Exc3] BIT NULL,
[Exc4] BIT NULL,
[Exc5] BIT NULL,
[Summary] VARCHAR (MAX) NULL,
[UserID] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([MeetingID] ASC),
CONSTRAINT [UserID] FOREIGN KEY ([UserID]) REFERENCES [dbo].[Users] ([UserID])
In this case it is a n to n relation and you need an additional table like
usermeetings
[UsermeetingsID]
[UserID]
[MeetingID]
And you can remove the userid columns from meeting table and meetings columns from user table
I want to implement a limited time period membership in asp.net MVC. For that, I manually introduced a new column ExpiryDate in the AspNetUsers table. When a new user registers itself this field will automatically generate an expiry date with the help of the getdate() method.
CREATE TABLE [dbo].[AspNetUsers] (
[Id] NVARCHAR (128) NOT NULL,
[Email] NVARCHAR (256) NULL,
[EmailConfirmed] BIT NOT NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[SecurityStamp] NVARCHAR (MAX) NULL,
[PhoneNumber] NVARCHAR (MAX) NULL,
[PhoneNumberConfirmed] BIT NOT NULL,
[TwoFactorEnabled] BIT NOT NULL,
[LockoutEndDateUtc] DATETIME NULL,
[LockoutEnabled] BIT NOT NULL,
[AccessFailedCount] INT NOT NULL,
[UserName] NVARCHAR (256) NOT NULL,
[RegisterDate ] DATETIME DEFAULT (getdate()) NULL,
[ExpiryDate] DATETIME DEFAULT (getdate()+(60)) NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);
Now, I want to check that expiry date value everytime when that user tries to login. Where and what logic should I implement in my code?
My create table code like :
CREATE TABLE [dbo].[DeTai](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MaDeTai] AS ('DT'+right('000000'+CONVERT([varchar](10),[ID]),(6))) PERSISTED NOT NULL,
[TenDeTai] [nvarchar](255) NOT NULL,
[LinhVuc] [nvarchar](255) NOT NULL,
[Nam] [int] NOT NULL,
[MaGV] [varchar](8) NOT NULL,
)
When I use linq to sql to insert a values this messages come out
The primary key column of type 'VarChar(8)' cannot be generated by the server.
what should i do ?
you have to designate the primary key, setting identity is not enough
CREATE TABLE [dbo].[DeTai](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MaDeTai] AS ('DT'+right('000000'+CONVERT([varchar](10),[ID]),(6))) PERSISTED NOT NULL,
[TenDeTai] [nvarchar](255) NOT NULL,
[LinhVuc] [nvarchar](255) NOT NULL,
[Nam] [int] NOT NULL,
[MaGV] [varchar](8) NOT NULL,
constraint PK_DeTail_ID primary key (ID)
)
I would like to import\export file from Sharepoint 2013 database via C#. Do you know how to do it? Because file in SharePoint is encrypted.
My question is: How import\export file in C# from SharePoint Foundation 2013 database?
CREATE SCRIPT [dbo].[AllDocs]
CREATE TABLE [dbo].[AllDocs](
[Id] [uniqueidentifier] NOT NULL,
[SiteId] [uniqueidentifier] NOT NULL,
[DirName] [nvarchar](256) NOT NULL,
[LeafName] [nvarchar](128) NOT NULL,
[Level] [tinyint] NOT NULL,
[ParentId] [uniqueidentifier] NOT NULL,
[DeleteTransactionId] [varbinary](16) NOT NULL,
[WebId] [uniqueidentifier] NOT NULL,
[ListId] [uniqueidentifier] NULL,
[DoclibRowId] [int] NULL,
[Type] [tinyint] NOT NULL,
[SortBehavior] [tinyint] NOT NULL,
[Size] [int] NULL,
[ETagVersion] AS (case when [InternalVersion] IS NULL then NULL else ([InternalVersion]+[BumpVersion]*(256))/(256) end),
[EffectiveVersion] AS (case when [InternalVersion] IS NULL then NULL else [InternalVersion]+[BumpVersion]*(256) end),
[InternalVersion] [int] NULL,
[ContentVersion] [int] NOT NULL,
[NextBSN] [bigint] NULL,
[MetadataNextBSN] [bigint] NULL,
[StreamSchema] [tinyint] NULL,
[HasStream] AS (case when [Type]=(0) AND ([DocFlags]&(256))=(256) AND [SetupPath] IS NULL OR [SetupPath] IS NOT NULL AND ([DocFlags]&(64))=(64) then (1) else (0) end),
[BumpVersion] [tinyint] NOT NULL,
[UIVersion] [int] NOT NULL,
[Dirty] AS (case when [BumpVersion]<>(0) then CONVERT([bit],(1)) else CONVERT([bit],(0)) end),
[ListDataDirty] [bit] NOT NULL,
[DocFlags] [int] NULL,
[ThicketFlag] [bit] NULL,
[CharSet] [int] NULL,
[ProgId] [nvarchar](255) NULL,
[TimeCreated] [datetime] NOT NULL,
[TimeLastModified] [datetime] NOT NULL,
[NextToLastTimeModified] [datetime] NULL,
[MetaInfoTimeLastModified] [datetime] NULL,
[TimeLastWritten] [datetime] NULL,
[SetupPathVersion] [tinyint] NOT NULL,
[SetupPath] [nvarchar](255) NULL,
[SetupPathUser] [nvarchar](255) NULL,
[CheckoutUserId] [int] NULL,
[DraftOwnerId] [int] NULL,
[CheckoutDate] [datetime] NULL,
[CheckoutExpires] [datetime] NULL,
[VersionCreatedSinceSTCheckout] [bit] NOT NULL,
[LTCheckoutUserId] AS (case when ([DocFlags]&(32))=(32) then [CheckoutUserId] end),
[CheckinComment] [nvarchar](1023) NULL,
[IsCheckoutToLocal] AS (case when ([DocFlags]&(512))=(512) then (1) else (0) end),
[VirusVendorID] [int] NULL,
[VirusStatus] [tinyint] NULL,
[VirusInfo] [nvarchar](255) SPARSE NULL,
[VirusInfoEx] [varbinary](max) NULL,
[MetaInfo] [dbo].[tCompressedBinary] NULL,
[MetaInfoSize] [int] NULL,
[MetaInfoVersion] [int] NOT NULL,
[UnVersionedMetaInfo] [dbo].[tCompressedBinary] NULL,
[UnVersionedMetaInfoSize] [int] NULL,
[UnVersionedMetaInfoVersion] [int] NULL,
[WelcomePageUrl] [nvarchar](260) NULL,
[WelcomePageParameters] [nvarchar](max) NULL,
[IsCurrentVersion] [bit] NOT NULL,
[AuditFlags] [int] NULL,
[InheritAuditFlags] [int] NULL,
[UIVersionString] AS ((CONVERT([nvarchar],[UIVersion]/(512))+'.')+CONVERT([nvarchar],[UIVersion]%(512))),
[ScopeId] [uniqueidentifier] NOT NULL,
[BuildDependencySet] [varbinary](max) NULL,
[ParentVersion] [int] NULL,
[ParentVersionString] AS ((CONVERT([nvarchar],[ParentVersion]/(512))+'.')+CONVERT([nvarchar],[ParentVersion]%(512))),
[TransformerId] [uniqueidentifier] NULL,
[ParentLeafName] [nvarchar](128) NULL,
[CtoOffset] [smallint] NULL,
[Extension] AS (case when charindex(N'.',[LeafName] collate Latin1_General_BIN)>(0) then right([LeafName],charindex(N'.',reverse([LeafName]) collate Latin1_General_BIN)-(1)) else N'' end),
[ExtensionForFile] AS (case when [Type]=(0) AND charindex(N'.',[LeafName] collate Latin1_General_BIN)>(0) then right([LeafName],charindex(N'.',reverse([LeafName]) collate Latin1_General_BIN)-(1)) else N'' end),
[ItemChildCount] [int] NOT NULL,
[FolderChildCount] [int] NOT NULL,
[FileFormatMetaInfo] [varbinary](max) NULL,
[FileFormatMetaInfoSize] [int] NOT NULL,
[FFMConsistent] [bit] NULL,
[ListSchemaVersion] [int] NULL,
[ClientId] [varbinary](16) NULL,
CONSTRAINT [AllDocs_ParentId] PRIMARY KEY CLUSTERED
(
[SiteId] ASC,
[DeleteTransactionId] ASC,
[ParentId] ASC,
[Id] ASC,
[Level] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = ON, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
I try use this:
1 Code:
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
2 Code
DECLARE #Command VARCHAR(4000),
#FileID VARCHAR(128),
#MyFilename nvarchar(max)
DECLARE curFile CURSOR FOR -- Cursor for each image in table
SELECT DocId FROM [WSS_Content].[dbo].[DocStreams]
OPEN curFile
FETCH NEXT FROM curFile
INTO #FileID
WHILE (##FETCH_STATUS = 0) -- Cursor loop
BEGIN
-- Keep the bcp command on ONE LINE - SINGLE LINE!!! - broken up for presentation
SELECT #MyFilename = LeafName from [WSS_Content].[dbo].[AllDocs] WHERE Id = #FileID
SET #Command = 'bcp "SELECT Content from [WSS_Content].[dbo].[DocStreams] WHERE DocId = ''' +
#FileID + '''" queryout "C:\' +
#MyFilename+'" -T -n -Slocalhost -fC:\bcp.fmt'
PRINT #Command -- debugging
EXEC xp_cmdshell #Command -- Carry out image export to file from db table
FETCH NEXT FROM curFile
INTO #FileID
END -- cursor loop
CLOSE curFile
DEALLOCATE curFile
But I get error:
It`s simple to import\export file in C# because it's binary file SELECT Content from [WSS_Content].[dbo].[DocStreams]