Clean data from database - c#

I using ORM telerik open access in my project. I can use it to create and add data to my database.
Now i want to clean all data from my database. I can do it by delete data from each table but it will take long code. I have google how to clean it using DBContext and found nothing. There another way to clean database but not looping to call delete function for each table in my DB?

(SQL SERVER only) You could use the following sql script to clean up the data in your database:
/* 1. Disable all constraints and triggers*/
exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
exec sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'
/* 2. Delete all of table data */
exec sp_MSforeachtable 'DELETE ?'
/* 3. Enable all constraints and triggers */
exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
exec sp_MSforeachtable 'ALTER TABLE ? ENABLE TRIGGER ALL'
/* 4. Reset tables identity */
exec sp_MSforeachtable 'IF OBJECTPROPERTY(OBJECT_ID(''?''), ''TableHasIdentity'') = 1 BEGIN DBCC CHECKIDENT (''?'',RESEED,0) END'

Create scripts from database for drop and create.
Create stored procedure for the same with the code u have taken.
Execute the stored procedure from code behind..
Drop and create query will be something like this..This is a sample..
/* Object: Table [dbo].[EmployeeList] Script Date: 09/12/2013 09:35:01 */
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EmployeeList]') AND type in (N'U'))
DROP TABLE [dbo].[EmployeeList]
GO
USE [dbname]
GO
/****** Object: Table [dbo].[EmployeeList] Script Date: 09/12/2013 09:35:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[EmployeeList](
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Category] [nvarchar](50) NOT NULL,
[id] [int] IDENTITY(1,1) NOT NULL,
[alphanumeric] [varchar](50) NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

Related

Minimizing select time in Varbinary(max) column

I am facing a issue with latency when I select images which are saved in a varbinary(max) column in SQL Server, it takes around 3-5 mins to select at least 5 images. This table (galleryDetail) contains images with different sizes like 2mb, 40kb, 1mb likewise. As suggested in this link SQL server slow select query from type varbinary(max) (last comment),if somebody could give me a road map/suggestion, to achieve this task, it would be a big Help.
My table structure is as follows,
CREATE TABLE [dbo].[GalleryDetail]
(
[Id] [INT] IDENTITY(1,1) NOT NULL,
[Image] [VARBINARY](MAX) NULL,
[Title] [VARCHAR](250) NULL,
[Active] [BIT] NULL,
[CreatedDate] [DATETIME] NULL,
CONSTRAINT [PK_ImageGallery]
PRIMARY KEY CLUSTERED ([Id] ASC)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[GalleryDetail]
ADD CONSTRAINT [DF_GalleryDetail_Active] DEFAULT ((1)) FOR [Active]
GO
If you select by Title column then add index to make select faster:
create index idx_Title on dbo.GalleryDetail(Title)
This only helps if Title is in the where clause of your query, e.g.
select * from GalleryDetail where Title = 'My picture'
or
select * from GalleryDetail where Title like 'picture%'

SSIS Parent table Child relation migration

I'm trying to use SSIS to move some data from one SQL server to my Destimation SQL server, the source has a table "Parent" with Identity field ID that is a Foreign key to the "Child" table.
1 - N relation
The question is simple, what is the best way to transfer the data to a different SQL Server with still a parent child relation.
Note: Both ID (Parent and Child) are identity fields that we do not want to migrate since the destination source wont necessary need to have them.
Please share your comments and ideas.
FYI: We create a .Net Code (C#) that does this, we have a query that gets parent data, a query that get childs data and using linq we join the data and we loop parent getting the new ID and inserting as reference of second table. This is working but we want to create the same on SSIS to be able to scale later.
You have to import Parent Table Before Child Table:
First You have to Create Tables On Destination Server, you can achieve this using an query like the following:
CREATE TABLE [dbo].[Tbl_Child](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Parent_ID] [int] NULL,
[Name] [varchar](50) NULL,
CONSTRAINT [PK_Tbl_Child] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Tbl_Parent](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
CONSTRAINT [PK_Tbl_Parent] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tbl_Child] WITH CHECK ADD CONSTRAINT [FK_Tbl_Child_Tbl_Parent] FOREIGN KEY([Parent_ID])
REFERENCES [dbo].[Tbl_Parent] ([ID])
GO
ALTER TABLE [dbo].[Tbl_Child] CHECK CONSTRAINT [FK_Tbl_Child_Tbl_Parent]
GO
Add two OLEDB Connection manager (Source & Destination)
Next you have to add a DataFlow Task to Import Parent Table Data From Source. You have to check Keep Identity option
Next you have to add a DataFlow Task to Import Child Table Data From Source. You have to check Keep Identity option
Package May Look like the following
WorkAround: you can disable constraint and import data then enabling it by adding a SQL Task before and after Importing
Disable Constraint:
ALTER TABLE Tbl_Child NOCHECK CONSTRAINT FK_Tbl_Child_Tbl_Parent
Enable Constraint:
ALTER TABLE Tbl_Child CHECK CONSTRAINT FK_Tbl_Child_Tbl_Parent
if using this Workaround it is not necessary to follow an order when importing

How to FIX this message in ASP.NET (Visual Studio) with my query?

This is my script in SQL but when I try use a form view in ASP.NET (visual studio). I worked with SQL Server Management Studio
CREATE TABLE [odl].[MaterialDetail]
(
[Material] [nvarchar](50) NOT NULL,
[PlantID] [nvarchar](50) NOT NULL,
[LabelPos] [char](1) NOT NULL,
[UseProdLabel] [tinyint] NOT NULL,
[UseContLabel] [tinyint] NOT NULL,
[UseTote] [tinyint] NOT NULL,
[COO] [nvarchar](50) NOT NULL,
[LabelsPerCont] [decimal](9, 2) NOT NULL,
[TareWeight] [decimal](9, 2) NOT NULL,
[AltTareWeight] [decimal](9, 2) NOT NULL,
[ModDate] [datetime] NOT NULL,
[CreateDate] [datetime] NOT NULL,
[rowguid] [uniqueidentifier] ROWGUIDCOL NOT NULL,
CONSTRAINT [PK_MaterialDetail] PRIMARY KEY CLUSTERED
(
[Material] ASC,
[PlantID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_Material] DEFAULT ('') FOR [Material]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_PlantID] DEFAULT ('') FOR [PlantID]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_LabelPos] DEFAULT ('0') FOR [LabelPos]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_UseProdLabel] DEFAULT ((0)) FOR [UseProdLabel]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_UseContLabel] DEFAULT ((1)) FOR [UseContLabel]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_UseTote] DEFAULT ((0)) FOR [UseTote]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_COO] DEFAULT ('') FOR [COO]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_LabelPerCont] DEFAULT ((2)) FOR [LabelsPerCont]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_TareWeight] DEFAULT ((0)) FOR [TareWeight]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_AltTareWeight] DEFAULT ((0)) FOR [AltTareWeight]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_ModDate] DEFAULT (getdate()) FOR [ModDate]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_CreateDate] DEFAULT (getdate()) FOR [CreateDate]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_rowguid] DEFAULT (newsequentialid()) FOR [rowguid]
GO
I get this error in the moment when i try test my query
There was an error executing the query. Please check the syntax of
the command and if, present, the types and values of the parameters
and ensure they are correct Invalid object name 'MaterialDetail'
First of all, that SQL code doesn't produce any data or results. It just sets up a brand new empty table. I don't know what you'd expect your form-view to show, because there's not data here.
Secondly, if the table already exists in the database, or if the constraints you are adding already exist, I would expect this code to throw an error when run for a second time, because you are trying to create an object that already exists.
Finally, that SQL makes extensive use of the GO separator. GO is not a part of the SQL language. It is a feature of Sql Server Management Studio that is used for separating queries into different batches. The GO keyword is never sent to the database server, and Sql Server wouldn't know what to do with it if it were.

Reverse Engineer Code First - Generates a context file with error

Got the following Database:
GO
/****** Object: Table [dbo].[Emp] Script Date: 2/25/2013 09:52:26 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Emp](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NOT NULL,
[Age] [int] NOT NULL,
[DateOfBirth] [date] NOT NULL,
CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
When i try to do Entity Framework -> reverse engineer code first on an empty project in Visual Studio 2012 c# i get the following error in the DBContext as soon as it is done
Error 1 An object reference is required for the non-static field, method, or property 'System.Data.Entity.DbContext.Database.get'
why might that be?
EDIT:
Worked in an empty project
doesn't work on a non empty project
"Database" is a commonly used class/namespace/variable name, so it's getting confused as to which one you mean. Notice that the error mentioned "System.Data.Entity.DbContext.Database". Just qualify it with "System.Data.Entity" to get the correct one:
System.Data.Entity.Database.SetInitializer(null);

execute sql query from c# code for creating new sql table

I am trying to execute a sql query which will create a new table in sql database. When I am doing this:
string queryString = #"
CREATE TABLE [dbo].[peep_searchresults_live](
[id] [int] IDENTITY(1,1) NOT NULL,
[SKU] [varchar](150) NULL,
[ManufacturerBrand] [varchar](150) NULL,
);
The query gets executed just fine and the table gets created in the database.
But when I am trying this:
string queryString = #"
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[peep_searchresults_live](
[id] [int] IDENTITY(1,1) NOT NULL,
[SKU] [varchar](150) NULL,
[ManufacturerBrand] [varchar](150) NULL,
CONSTRAINT [PK_peep_searchresults_live] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO";
I am receiving error like this one:
SQL server doesn't understand what GO is - only the query analyzer knows (eg. SSMS).
If you need to execute multiple commands, simply execute them in the proper order, one at a time.
GO statements are not valid in ADO.net
Do you need all of those other qualifying statements?
You can break each into a separate command, and execute each command in turn with a single connection.
You cannot use go, you need to split the command in to multiple sets.
http://social.msdn.microsoft.com/Forums/ar/csharpgeneral/thread/2907541f-f1cf-40ea-8291-771734de55f2

Categories

Resources