I was working with VisualStudio 2017 C# and localdb2016 I created a table in server explorer's database desginer window with 10 columns added X rows.
I have already set copy if newer so rows wont get deleted everytime
but when i try to add a new column (11th column) all of my rows (even in other tables!) get deleted !
why ?
Here is my Table code generated from desginer .... :
CREATE TABLE [dbo].[CustomerTable] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[name] NVARCHAR (50) NULL,
[company] NVARCHAR (50) NULL,
[email] NVARCHAR (50) NULL,
[phone1] NVARCHAR (50) NULL,
[phone2] NVARCHAR (50) NULL,
[address] NVARCHAR (50) NULL,
[fax] NVARCHAR (50) NULL,
[phone3] NVARCHAR (50) NULL,
[date] DATETIME DEFAULT (getdate()) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
UPDATE 1 :
ALTER TABLE dbo.CustomerTable ADD column_b VARCHAR(20) NULL, column_c INT NULL ;
does that code remove old columns (b and c) if they exists?
is it a good idea to do that everytime app starts? (for upgrading purpose)
Try this Transact-SQL query
ALTER TABLE dbo.CustomerTable ADD column_b VARCHAR(20) NULL, column_c INT NULL ;
This query will add 2 columns in your table -> First, b (VARCHAR[20]) & Second, c (INT).
To read more,
ALTER TABLE (Transact-SQL)
The query will not remove any existing column because it is an alter query that means it alter the table as you mention. Adding existing column doesn't alter table. So, no changes.
Related
We have written a c# MVC ASP.NET CORE project which is hosted as an azure app-service and is driven by an azure hosted sql server.
I have the following sql Table as a part of the database:
CREATE TABLE [dbo].[Users] (
[UserID] INT IDENTITY (1, 1) NOT NULL,
[UserName] NVARCHAR (50) NOT NULL,
[Email] NVARCHAR (50) NOT NULL,
[Active] BIT DEFAULT ((1)) NOT NULL,
[TeamID] INT NULL,
[UserType] INT DEFAULT ((0)) NOT NULL,
[UserCpM] FLOAT (53) DEFAULT ((0)) NOT NULL,
[CpMSet] BIT DEFAULT ((0)) NOT NULL,
[CurrentProject] INT DEFAULT ((-2)) NOT NULL,
[CurrentNotes] NVARCHAR (MAX) NULL,
[TimeStarted] DATETIME NOT NULL,
[Failure] BIT DEFAULT ((0)) NOT NULL,
[Failure_Project] INT NULL,
[Failure_TimeStarted] DATETIME NULL,
[Failure_TimeEnded] DATETIME NULL,
[Failure_Notes] NVARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([UserID] ASC),
CONSTRAINT [FK_Users_ToTeams] FOREIGN KEY ([TeamID]) REFERENCES [dbo].[Teams] ([TeamID]),
CONSTRAINT [FK_Users_CurrentProject_ToProjects] FOREIGN KEY ([CurrentProject]) REFERENCES [dbo].[Projects] ([ProjectID]),
CONSTRAINT [FK_Users_FailureProject_ToProjects] FOREIGN KEY ([Failure_Project]) REFERENCES [dbo].[Projects] ([ProjectID])
);
The UserCpM field is an indication of employees cost-per-minute and as such is cleared before displaying to any user who is not of the highest privilege level. Over the last couple of days this field has been randomly resetting itself back to 0 for every entry in the table in the database.
I have hunted through the code and confirmed that on any view where the CpM is cleared it is re-set before saving the User back to the database so I'm fairly certain that that isn't the cause of the issue.
Is there any reason that the context would save it's changes without being told to do so? or alternatively is it possible that it will track changes to anything set from it and transfer them back to the database.
Is there any reason that the context would save it's changes without being told to do so?
No, Azure SQL or SQL Server hosted on Azure will not modify your data.
is it possible that it will track changes to anything set from it and transfer them back to the database.
I suggest you create a log table and 2 triggers for your Users table. If any updates has been applies on Users table. It triggers will write the operation time and the UserCpM changes to the log table.
Here is a insert trigger code sample.
CREATE TRIGGER dbo.UsersInsert
ON [dbo].[Users]
AFTER Insert
AS
BEGIN
insert into dbo.UserLog(UserID, OriginalUserCpM, NewUserCpm, ModifiedDate)
select UserID, null, UserCpm, GETDATE() from inserted
SET NOCOUNT ON;
END
Here is a update trigger code sample.
CREATE TRIGGER dbo.UsersUpdate
ON [dbo].[Users]
AFTER Update
AS
BEGIN
insert into dbo.UserLog(UserID, OriginalUserCpM, NewUserCpm, ModifiedDate)
select UserID, UserCpm, null, GETDATE() from deleted
insert into dbo.UserLog(UserID, OriginalUserCpM, NewUserCpm, ModifiedDate)
select UserID, null, UserCpm, GETDATE() from inserted
SET NOCOUNT ON;
END
I'm trying to insert data into the database using LINQ. In me SQL server side I wrote a sequence to generate a custom id with a prefix. Here what I did in my code,
ALLibraryDataClassesDataContext dbContext = new ALLibraryDataClassesDataContext();
dbContext.Books.InsertOnSubmit(book);
dbContext.SubmitChanges();
dbContext.Dispose();
In my book object I'm not setting the BookID because it's generating from the sequence. When I'm executing
INSERT INTO library_db.dbo.Book([BookName]) VALUES ('SH')
This inserts data with the auto generated id. But when I'm running the code, It gives an SQLException,
Cannot insert the value NULL into column 'BookId', table 'library_db.dbo.Book'; column does not allow nulls. INSERT fails.
EDITED
My sequence and table creation,
CREATE TABLE [dbo].[Book] (
[BookId] VARCHAR (50) NOT NULL,
[BookName] VARCHAR (50) NULL,
[AuthorName] VARCHAR (50) NULL,
[PublisherName] VARCHAR (50) NULL,
[PublishedDate] DATE NULL,
[price] MONEY NULL,
[PurchasedDate] DATE NULL,
[PurchasedBillNo] VARCHAR (50) NULL,
[CategoryId] VARCHAR (50) NULL,
[NoOfCopies] VARCHAR (50) NULL,
[FinePerDay] MONEY NULL,
[BookImage] VARCHAR (2000) NULL,
PRIMARY KEY CLUSTERED ([BookId] ASC),
CONSTRAINT [FK_Book_Category] FOREIGN KEY ([CategoryId]) REFERENCES [dbo].
[Category] ([CategoryId])
);
GO
CREATE SEQUENCE dbo.BookId_Seq AS
INT START WITH 1
INCREMENT BY 1 ;
GO
ALTER TABLE dbo.Book
ADD CONSTRAINT Const_BookId_Seq
DEFAULT FORMAT((NEXT VALUE FOR dbo.BookId_Seq),'B0000#') FOR [BookId];
GO
What is the different between running a query manually and through LINQ? Is there a possible way to insert data into code(LINQ) with a custom id?
I'm not sure if you can do this with LINQ-to-SQL, but give it a try:
In the context designer, set "Auto Generated Value" = true for BookId. This tells LINQ-to-SQL to exclude the column from insert statements. Now the database will fall back to the default constraint, which doesn't happen when an insert statement supplies a value.
However, there may be one problem. When auto-generated value is defined for a primary key column, LINQ-to-SQL will try to read the generated value afterwards by a select statement looking like
SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
Obviously, drawing the value from a sequence will leave SCOPE_IDENTITY() undefined. At best this will prevent you from reading the generated key value from the book variable after SubmitChanges, but in the worst case it will cause an error that you can't work around.
Currently i have 2 tables, which is user_table and application_table.
The main table is application_table, but i need some information to display which only can find in user_table.
CREATE TABLE [dbo].[Application_Table] (
[Id] INT IDENTITY (0, 1) NOT NULL,
[Name] VARCHAR (50) NULL,
[Date] DATE NULL,
[Vehicle] VARCHAR (50) NULL,
[DestinationFrom] VARCHAR (50) NULL,
[DestinationTo] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
here is the second table:
CREATE TABLE [dbo].[User_table] (
[Name] VARCHAR (50) NOT NULL,
[Email] VARCHAR (50) NULL,
[Password] VARCHAR (50) NULL,
[Department] VARCHAR (50) NULL,
CONSTRAINT [PK_User_table] PRIMARY KEY CLUSTERED ([Name] ASC)
);
I need to display all information from application_table and some information such as email, department from user_table based on Name in application_table in gridview.
Is that any simple way to do it? I am quite new in asp net and c#, please guide me as i am a slow learner.
You could achieve that using a multi level grid.
Refer to this for details
ASP Multi Level Grid
BTW you should add a foreign key to your second table.
I have an Excel sheet that is populated by HR employee with thousands of client records it looks like this one:
User Friendly Excel Sheet Example Screenshot
My client's SQL Server table schema looks like this
CREATE TABLE [dbo].[Clients] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
[Photo] VARCHAR (200) NOT NULL,
[PolicyID] INT NOT NULL,
[BirthDay] DATE NOT NULL,
[Gender] BIT NOT NULL,
[Title] NVARCHAR (100) NULL,
[Nationality] NVARCHAR (100) NOT NULL,
[Relationship] NVARCHAR (50) NOT NULL,
[ClassID] INT NOT NULL,
[SponsorID] INT NULL,
[HRID] INT NOT NULL,
[Active] BIT CONSTRAINT [DF_Clients_Active] DEFAULT ((1)) NOT NULL,
[StartingDate] DATE NOT NULL,
[EndingDate] DATE NOT NULL,
[AddingDate] DATETIME NOT NULL,
[Creator] INT NOT NULL,
[UniqueID] NVARCHAR (50) NULL,
[PassportNo] NVARCHAR (50) NULL,
CONSTRAINT [PK_Clients] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [FK_Clients_Clients] FOREIGN KEY ([SponsorID]) REFERENCES [dbo].[Clients] ([ID]),
CONSTRAINT [FK_Clients_Employees] FOREIGN KEY ([HRID]) REFERENCES [dbo].[Employees] ([ID]),
CONSTRAINT [FK_Clients_Employees1] FOREIGN KEY ([Creator]) REFERENCES [dbo].[Employees] ([ID]),
CONSTRAINT [FK_Clients_Policy] FOREIGN KEY ([PolicyID]) REFERENCES [dbo].[Policy] ([ID]),
CONSTRAINT [FK_Clients_Classes] FOREIGN KEY ([ClassID]) REFERENCES [dbo].[Classes] ([ID])
);
What is the best approach to achieve such inserts?
I've tried using SqlBulkCopy but it doesn't allow any manipulation on the inserted rows.
I've tried also using SqlAdapter.Update(Datatable) but it failed since I've read the Excel sheet using ExcelDataReader then tried to add some columns like Creator and Adding Date at runtime and when I tried to run Adapter.Update(ModifiedDatatable) it throws an exception
Update requires a valid UpdateCommand when passed DataRow collection with modified rows
When I tried to use SqlBulkCopy to insert this Excel sheet it worked as expected
Excel Sheet with Foreign Keys Screenshot
But it's not right to force the end user to put some foreign keys in the Excel sheet before import.
Notice:
Sorry for uploading screenshots to Tinypic but I couldn't upload them here because of my Rep Points.
Thanks in advance
I would be creating an SSIS package in this scenario. SSIS can read from Excel, and you can get it to query the database for the extra information to build a valid dataset that will not violate the FK constraints.
I'm trying to update a table on Windows Azure but I don't know how.
This is my table. I need to update detallePeriodo from nvarchar(2) to nvarchar(4).
CREATE TABLE [dbo].[CLIENTE_LIDER]
(
[Cedula] NVARCHAR (12) NOT NULL,
[Nombre] NVARCHAR (100) NULL,
[TelCelular] NVARCHAR (9) NULL,
[TelCasa] NVARCHAR (9) NULL,
[Direccion] NVARCHAR (200) NULL,
[Distrito] INT NULL,
[Periosidad] INT NULL,
[DetallePeriodo] NVARCHAR (2) NULL,
[Monto_Total] FLOAT (53) DEFAULT ((0)) NULL,
[Monto_Actual] FLOAT (53) DEFAULT ((0)) NULL,
[Cuota] FLOAT (53) DEFAULT ((0)) NULL,
[Codigo] VARCHAR (12) NULL,
[Ruta] INT NULL,
[FechaIngresoSistema] DATE NULL,
PRIMARY KEY CLUSTERED ([Cedula] ASC),
FOREIGN KEY ([Distrito]) REFERENCES [dbo].[DISTRITOS] ([codigo]),
CONSTRAINT [FK_CLIENTE_LIDER_0] FOREIGN KEY ([Ruta]) REFERENCES [dbo].[RUTA] ([Codigo])
);
From the documentation, which applies to both SQL Server and the Azure SQL Database, it appears you just need a fairly standard ALTER statement.
ALTER TABLE dbo.CLIENTE_LIDER ALTER COLUMN DetallePeriodo NVARCHAR(4);