2016-09-26 52 views
0

我想創建數據庫和相關表。除了'EmployeeDocuments'表(數據庫和4個表格已創建)之外,一切都可以工作。它給出瞭如下錯誤:EmployeeDocuments表不能在數據庫中創建

數據庫中已有一個名爲'FK_Employees_Companies1'的對象。

我的代碼如下:

private void UserEntryForm_Activated(object sender, EventArgs e) 
    { 
     CheckIfDBExist(); 
    } 
    int i; 

    private void CheckIfDBExist() 
    { 

     SqlConnection tmpConn = new SqlConnection("server=(local)\\SQLEXPRESS;user=sa;pwd=123"); 

     string sqlCreateDBQuery = string.Format("if not exists (select name from sys.databases where name = 'Devrimer_v2222')create database Devrimer_v2222"); 

     SqlCommand sqlCmd = new SqlCommand(sqlCreateDBQuery, tmpConn); 

     string constr = Tools.Baglanti.ConnectionString.ToString(); 

     try 
     { 
      tmpConn.Open(); 
      int sonuc = sqlCmd.ExecuteNonQuery(); 

      if (sonuc != 0) 
      { 
       tmpConn.Close(); 

       string query = "if not exists(select * from sys.tables where name = 'Companies' "; 
       query += ")"; 
       query += "CREATE TABLE [dbo].[Companies]([Id][int] IDENTITY(1, 1) NOT NULL,"; 
       query += "[Name][nvarchar](100) NULL,"; 
       query += "[Adress][nvarchar](500) NULL,"; 
       query += "[TelNo][char](20) NULL,"; 
       query += "[Active][bit] NOT NULL CONSTRAINT[DF_Companies_Active] DEFAULT((1)),"; 
       query += "CONSTRAINT[PK_Companies] PRIMARY KEY CLUSTERED([Id] ASC) WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,"; 
       query += "IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,"; 
       query += "ALLOW_PAGE_LOCKS = ON) ON[PRIMARY]) ON[PRIMARY]"; 
       query += " SET ANSI_PADDING OFF"; 


       using (SqlConnection con = new SqlConnection(constr)) 
       { 
        using (SqlCommand cmd = new SqlCommand(query)) 
        { 
         cmd.Connection = con; 
         con.Open(); 
         cmd.ExecuteNonQuery(); 
         con.Close(); 
        } 
       } 

       string docQuery = "if not exists(select * from sys.tables where name = 'Documents' "; 
       docQuery += ")"; 
       docQuery += "CREATE TABLE [dbo].[Documents]([Id] [int] IDENTITY(1,1) NOT NULL,"; 
       docQuery += "[Name] [nvarchar](50) NULL,"; 
       docQuery += "[Description] [nvarchar](500) NULL,"; 
       docQuery += "[ValidPeriod] [int] NULL,"; 
       docQuery += "[PeriodType] [nchar](10) NULL,"; 
       docQuery += "[Active] [bit] NOT NULL CONSTRAINT [DF_Documents_Active] DEFAULT ((1)), "; 
       docQuery += "CONSTRAINT [PK_Documents] PRIMARY KEY CLUSTERED ( [Id] ASC)WITH (PAD_INDEX = OFF, "; 
       docQuery += "STATISTICS_NORECOMPUTE = OFF, "; 
       docQuery += "IGNORE_DUP_KEY = OFF, "; 
       docQuery += "ALLOW_ROW_LOCKS = ON, "; 
       docQuery += " ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]"; 

       using (SqlConnection con = new SqlConnection(constr)) 
       { 
        using (SqlCommand cmd = new SqlCommand(docQuery)) 
        { 
         cmd.Connection = con; 
         con.Open(); 
         cmd.ExecuteNonQuery(); 
         con.Close(); 
        } 
       } 
       string EmpQuery = "if not exists(select * from sys.tables where name = 'Employees' "; 
       EmpQuery += ")"; 
       EmpQuery += "CREATE TABLE [dbo].[Employees]([Id] [int] IDENTITY(1,1) NOT NULL,"; 
       EmpQuery += "[Name] [nvarchar](50) NULL,"; 
       EmpQuery += "[SurName] [nvarchar](50) NULL,"; 
       EmpQuery += "[Title] [nvarchar](50) NULL,"; 
       EmpQuery += "[Gender] [tinyint] NULL,"; 
       EmpQuery += "[MaritalStatus] [tinyint] NULL,"; 
       EmpQuery += "[Tckn] [char](11) NULL,"; 
       EmpQuery += "[ReportTo] [int] NULL,"; 
       EmpQuery += "[BirthDate] [date] NULL, "; 
       EmpQuery += "[TelNo] [char](20) NULL, "; 
       EmpQuery += "[Adress] [nvarchar](500) NULL, "; 
       EmpQuery += "[Email] [nvarchar](250) NULL, "; 
       EmpQuery += "[CompanyID] [int] NULL,"; 
       EmpQuery += "[StartWorkingDate] [date] NULL, "; 
       EmpQuery += "[Active] [bit] NOT NULL CONSTRAINT [DF_Employees_Active] DEFAULT ((1)), "; 
       EmpQuery += " CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED ([Id] ASC)WITH (PAD_INDEX = OFF, "; 
       EmpQuery += "STATISTICS_NORECOMPUTE = OFF, "; 
       EmpQuery += "IGNORE_DUP_KEY = OFF, "; 
       EmpQuery += "ALLOW_ROW_LOCKS = ON, "; 
       EmpQuery += "ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] "; 
       EmpQuery += ";"; 
       EmpQuery += "SET ANSI_PADDING OFF "; 
       EmpQuery += "ALTER TABLE [dbo].[Employees] WITH CHECK ADD CONSTRAINT [FK_Employees_Companies1] FOREIGN KEY([CompanyID]) REFERENCES [dbo].[Companies] ([Id]) "; 
       EmpQuery += ";"; 
       EmpQuery += "ALTER TABLE [dbo].[Employees] CHECK CONSTRAINT [FK_Employees_Companies1] "; 

       using (SqlConnection con = new SqlConnection(constr)) 
       { 
        using (SqlCommand cmd = new SqlCommand(EmpQuery)) 
        { 
         cmd.Connection = con; 
         con.Open(); 
         cmd.ExecuteNonQuery(); 
         con.Close(); 
        } 
       } 

       string userQuery = "if not exists(select * from sys.tables where name = 'Users' "; 
       userQuery += ")"; 
       userQuery += "CREATE TABLE [dbo].[Users]( [Id] [int] IDENTITY(1,1) NOT NULL, "; 
       userQuery += "[EmployeID] [int] NULL, "; 
       userQuery += "[UserName] [nvarchar](50) NULL, "; 
       userQuery += "[Password] [nchar](10) NULL, "; 
       userQuery += "[Active] [bit] NOT NULL CONSTRAINT [DF_Users_Aktif] DEFAULT ((1)), "; 
       userQuery += " CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED ([Id] ASC)WITH (PAD_INDEX = OFF, "; 
       userQuery += "STATISTICS_NORECOMPUTE = OFF, "; 
       userQuery += "IGNORE_DUP_KEY = OFF, "; 
       userQuery += "ALLOW_ROW_LOCKS = ON,"; 
       userQuery += "ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] "; 

       using (SqlConnection con = new SqlConnection(constr)) 
       { 
        using (SqlCommand cmd = new SqlCommand(userQuery)) 
        { 
         cmd.Connection = con; 
         con.Open(); 
         cmd.ExecuteNonQuery(); 
         con.Close(); 
        } 
       } 

       string docEmpQuery = "if not exists(select * from sys.tables where name = 'EmployeeDocuments' "; 
       docEmpQuery += ")"; 
       docEmpQuery += "CREATE TABLE [dbo].[EmployeeDocuments]([EmployeeID] [int] NOT NULL, "; 
       docEmpQuery += "[DocumentID] [int] NOT NULL, "; 
       docEmpQuery += "[GivenDate] [date] NULL, "; 
       docEmpQuery += "[LastValidDate] [date] NULL, "; 
       docEmpQuery += "CONSTRAINT [PK_EmployeeDocuments] PRIMARY KEY CLUSTERED ([EmployeeID] ASC, "; 
       docEmpQuery += "[DocumentID] ASC)WITH (PAD_INDEX = OFF, "; 
       docEmpQuery += "STATISTICS_NORECOMPUTE = OFF, "; 
       docEmpQuery += "IGNORE_DUP_KEY = OFF, "; 
       docEmpQuery += "ALLOW_ROW_LOCKS = ON,"; 
       docEmpQuery += "ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]"; 
       docEmpQuery += ";"; 
       docEmpQuery += "ALTER TABLE [dbo].[EmployeeDocuments] WITH CHECK ADD CONSTRAINT [FK_EmployeeDocuments_Documents1] FOREIGN KEY([DocumentID])REFERENCES [dbo].[Documents] ([Id]) "; 
       docEmpQuery += ";"; 
       docEmpQuery += "ALTER TABLE [dbo].[EmployeeDocuments] CHECK CONSTRAINT [FK_EmployeeDocuments_Documents1] "; 
       docEmpQuery += "; "; 
       docEmpQuery += "ALTER TABLE [dbo].[EmployeeDocuments] WITH CHECK ADD CONSTRAINT [FK_EmployeeDocuments_Employees1] FOREIGN KEY([EmployeeID]) REFERENCES [dbo].Employees] ([Id]) "; 
       docEmpQuery += ";"; 
       docEmpQuery += "ALTER TABLE [dbo].[EmployeeDocuments] CHECK CONSTRAINT [FK_EmployeeDocuments_Employees1] "; 

       using (SqlConnection con = new SqlConnection(constr)) 
       { 
        using (SqlCommand cmd = new SqlCommand(docEmpQuery)) 
        { 
         cmd.Connection = con; 
         con.Open(); 
         cmd.ExecuteNonQuery(); 
         con.Close(); 
        } 
       } 
      } 
     } 

     catch (Exception hata) 
     { 
      MessageBox.Show(string.Format(sqlCmd.CommandText.ToString() + "\n\nHata: " + hata.Message.ToString())); 
     } 
     finally 
     { 
      tmpConn.Close(); 
     } 
    } 
} 

回答

0

我有使用MyDbUtils.exe和出口的所有過程和觸發器,並列入了表格和數據庫的創建語法, 效果很不錯;

我已經使用的方法(SQLTEXTexec())來運行的所有文本串:

然後; 使用CheckIfDBExist())方法創建數據庫和表; 迄今爲止工作得很好 ;

private void SQLTEXTexec(string sql, SqlConnection conn) 
     { 
      string sqlBatch = string.Empty; 
      SqlCommand cmd = new SqlCommand(string.Empty, conn); 
      conn.Open(); 
      sql += "\nGO"; // make sure last batch is executed. 
      try 
      { 
       foreach (string line in sql.Split(new string[2] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries)) 
       { 
        if (line.ToUpperInvariant().Trim() == "GO") 
        { 
         cmd.CommandText = sqlBatch; 
         cmd.ExecuteNonQuery(); 
         sqlBatch = string.Empty; 
        } 
        else 
        { 
         sqlBatch += line + "\n"; 
        } 
       } 
      } 
      finally 
      { 
       conn.Close(); 
      } 

private void CheckIfDBExist() 
     { 

      SqlConnection tmpConn = new SqlConnection("server=(local)\\SQLEXPRESS;user=sa;pwd=123"); 
      //("if not exists (select name from sys.databases where name = 'Devrimer_v2222') create database Devrimer_v2222"); 

      string sqlCreateDBQuery = string.Format("if not exists (select name from sys.databases where name = 'Devrimer_v2222')create database Devrimer_v2222"); 

      SqlCommand sqlCmd = new SqlCommand(sqlCreateDBQuery, tmpConn); 

      string constr = Tools.Baglanti.ConnectionString.ToString(); 

      try 
      { 
       tmpConn.Open(); 

       int sonuc = sqlCmd.ExecuteNonQuery(); 

       if (sonuc != 0) // database i silince çalışacak... 


       { 

        tmpConn.Close(); 

        string query = @"if not exists (select name from sys.databases where name = 'Devrimer_v2222') 
create database Devrimer_v2222 
GO 

use Devrimer_v2222 
if not exists(select * from sys.tables where name = 'Companies') 

CREATE TABLE [dbo].[Companies](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [Name] [nvarchar](100) NULL, 
    [Adress] [nvarchar](500) NULL, 
    [TelNo] [char](20) NULL, 
    [Active] [bit] NOT NULL CONSTRAINT [DF_Companies_Active] DEFAULT ((1)), 
CONSTRAINT [PK_Companies] 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 

USE Devrimer_v2222 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Companies_Delete]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Companies_Delete] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Companies_Delete] 
@Id int 
as 
delete Companies 
where 
[email protected] 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Companies_Insert]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Companies_Insert] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Companies_Insert] 
[email protected] int, primary kolonu almıyoruz. 
@Name nvarchar(100), 
@Adress nvarchar(500), 
@TelNo char(20), 
@Active bit 
as 
insert Companies 
values (@Name,@Adress,@TelNo,@Active) 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Companies_Select]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Companies_Select] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Companies_Select] 
as 
select * from Companies where Active=1 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Companies_Update]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Companies_Update] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Companies_Update] 
@Id int, 
@Name nvarchar(100), 
@Adress nvarchar(500), 
@TelNo char(20), 
@Active bit 
as 
update Companies 
set 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected] 
where 
[email protected] 
GO 

USE Devrimer_v2222 

GO 

INSERT INTO [dbo].[Companies] 
      ([Name] 
      ,[Adress] 
      ,[TelNo] 
      ,[Active]) 
    VALUES 
      ('Anagold','Erzincan',0543456,1) 
GO 


use Devrimer_v2222 
if not exists(select * from sys.tables where name = 'Documents') 
begin 
CREATE TABLE [dbo].[Documents](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [Name] [nvarchar](50) NULL, 
    [Description] [nvarchar](500) NULL, 
    [ValidPeriod] [int] NULL, 
    [PeriodType] [nchar](10) NULL, 
    [Active] [bit] NOT NULL CONSTRAINT [DF_Documents_Active] DEFAULT ((1)), 
CONSTRAINT [PK_Documents] 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] 
END 
GO 

use Devrimer_v2222 
GO 


IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Documents_Delete]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Documents_Delete] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Documents_Delete] 
@Id int 
as 
delete Documents 
where 
[email protected] 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Documents_Insert]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Documents_Insert] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Documents_Insert] 
[email protected] int, primary kolonu almıyoruz. 
@Name nvarchar(50), 
@Description nvarchar(500), 
@ValidPeriod int, 
@periodType char(10), 
@Active bit 
as 
insert Documents 
values (@Name,@Description,@ValidPeriod,@periodType,@Active) 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Documents_Select]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Documents_Select] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Documents_Select] 
as 
select * from Documents where Active=1 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Documents_Update]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Documents_Update] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Documents_Update] 
@Id int, 
@Name nvarchar(50), 
@Description nvarchar(500), 
@ValidPeriod int, 
@PeriodType nchar(10), 
@Active bit 
as 
update Documents 
set 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected] 
where 
[email protected] 
GO 

use Devrimer_v2222 
GO 

if not exists(select * from sys.tables where name = 'Employees') 
begin 
CREATE TABLE [dbo].[Employees](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [Name] [nvarchar](50) NULL, 
    [SurName] [nvarchar](50) NULL, 
    [Title] [nvarchar](50) NULL, 
    [Gender] [tinyint] NULL, 
    [MaritalStatus] [tinyint] NULL, 
    [Tckn] [char](11) NULL, 
    [ReportTo] [int] NULL, 
    [BirthDate] [date] NULL, 
    [TelNo] [char](20) NULL, 
    [Adress] [nvarchar](500) NULL, 
    [Email] [nvarchar](250) NULL, 
    [CompanyID] [int] NULL, 
    [StartWorkingDate] [date] NULL, 
    [Active] [bit] NOT NULL CONSTRAINT [DF_Employees_Active] DEFAULT ((1)), 
CONSTRAINT [PK_Employees] 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] 
END 

GO 

SET ANSI_PADDING OFF 
GO 

ALTER TABLE [dbo].[Employees] WITH CHECK ADD CONSTRAINT [FK_Employees_Companies1] FOREIGN KEY([CompanyID]) 
REFERENCES [dbo].[Companies] ([Id]) 
GO 

ALTER TABLE [dbo].[Employees] CHECK CONSTRAINT [FK_Employees_Companies1] 
GO 
use Devrimer_v2222 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Employees_Delete]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Employees_Delete] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Employees_Delete] 
@Id int 
as 
delete Employees 
where 
[email protected] 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Employees_Insert]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Employees_Insert] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Employees_Insert] 
[email protected] int, primary kolonu almıyoruz. 
@Name nvarchar(50), 
@SurName nvarchar(50), 
@Title nvarchar(50), 
@Gender char(10), 
@MaritalStatus char(10), 
@Tckn char(11), 
@ReportTo nvarchar(50), 
@BirthDate date, 
@TelNo char(20), 
@Adress nvarchar(500), 
@Email nvarchar(250), 
@CompanyID int, 
@StartWorkingDate date, 
@Active bit 
as 
insert Employees 
values (@Name,@SurName,@Title,@Gender,@MaritalStatus,@Tckn,@ReportTo,@BirthDate, 
@TelNo,@Adress,@Email,@CompanyID,@StartWorkingDate,@Active) 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Employees_Select]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Employees_Select] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Employees_Select] 
as 
Select 
Id, 
Name, 
SurName, 
Title, 

case MaritalStatus 
when 1 then 'Married' 
when 2 then 'Single' 
end as MaritalStatus, 

case Gender 
when 1 then 'Male' 
when 2 then 'Female' 
end as Gender, 

Tckn, 
ReportTo, 
BirthDate, 
TelNo, 
Adress, 
Email, 
CompanyID, 
StartWorkingDate 

from Employees where Active = 1 
GO 

use Devrimer_v2222 
GO 
INSERT INTO [dbo].[Employees] 
      ([Name] 
      ,[SurName] 
      ,[Title] 
      ,[Gender] 
      ,[MaritalStatus] 
      ,[Tckn] 
      ,[ReportTo] 
      ,[BirthDate] 
      ,[TelNo] 
      ,[Adress] 
      ,[Email] 
      ,[CompanyID] 
      ,[StartWorkingDate] 
      ,[Active]) 
    VALUES 
      ('Özgür','Acar','MadenMühendisi',1,1,12345,1,'01.03.1974',1234,'Adres','[email protected]',1,'01.03.2002',1) 
GO 
use Devrimer_v2222 

if not exists(select * from sys.tables where name = 'Users') 
begin 
CREATE TABLE [dbo].[Users](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [EmployeID] [int] NULL, 
    [UserName] [nvarchar](50) NULL, 
    [Password] [nchar](10) NULL, 
    [Active] [bit] NOT NULL CONSTRAINT [DF_Users_Aktif] DEFAULT ((1)), 
CONSTRAINT [PK_Users] 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] 
END 
GO 
USE [Devrimer_v2222] 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Users_Delete]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Users_Delete] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Users_Delete] 
@Id int 
as 
delete Users 
where 
[email protected] 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Users_Entry]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Users_Entry] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Users_Entry] 
@unm nvarchar (50), 
@pas nvarchar (15) 
as 
select * from Users 
where [email protected] and [email protected] and Active=1 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Users_Insert]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Users_Insert] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Users_Insert] 
@EmployeID int, 
@UserName nvarchar (50), 
@Password nvarchar (10), 
@Active bit 
as 
insert Users 
values (@EmployeID, @UserName,@Password,@Active) 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Users_Select]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Users_Select] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Users_Select] 
as 
select * from Users where Active=1 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_Users_Update]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_Users_Update] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_Users_Update] 
@Id int, 
@EmployeID int, 
@UserName nvarchar(50), 
@Password nvarchar(50), 
@Active bit 
as 
update Users 
set 
[email protected], 
[email protected], 
[email protected], 
[email protected] 

where 
[email protected] 
GO 
use Devrimer_v2222 
GO 


INSERT INTO [dbo].[Users] 
      ([EmployeID] 
      ,[UserName] 
      ,[Password] 
      ,[Active]) 
    VALUES 
      (1,1,1,1) 
GO 

use Devrimer_v2222 
if not exists(select * from sys.tables where name = 'EmployeeDocuments') 
begin 
CREATE TABLE [dbo].[EmployeeDocuments](
    [EmployeeID] [int] NOT NULL, 
    [DocumentID] [int] NOT NULL, 
    [GivenDate] [date] NULL, 
    [LastValidDate] [date] NULL, 
CONSTRAINT [PK_EmployeeDocuments] PRIMARY KEY CLUSTERED 
(
    [EmployeeID] ASC, 
    [DocumentID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 
END 
GO 

ALTER TABLE [dbo].[EmployeeDocuments] WITH CHECK ADD CONSTRAINT [FK_EmployeeDocuments_Documents1] FOREIGN KEY([DocumentID]) 
REFERENCES [dbo].[Documents] ([Id]) 
GO 

ALTER TABLE [dbo].[EmployeeDocuments] CHECK CONSTRAINT [FK_EmployeeDocuments_Documents1] 
GO 
use Devrimer_v2222 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_EmployeeDocuments_Delete]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_EmployeeDocuments_Delete] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_EmployeeDocuments_Delete] 
@EmployeID int, 
@DocumentID int 
as 
delete EmployeeDocuments 
where 
[email protected] and [email protected] 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_EmployeeDocuments_Insert]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_EmployeeDocuments_Insert] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_EmployeeDocuments_Insert] 

@EmployeeID int, 
@DocumentID int, 
@GivenDate date, 
@LastValidDate date 
as 
insert EmployeeDocuments 
values (@EmployeeID,@DocumentID,@GivenDate,@LastValidDate) 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_EmployeeDocuments_Select]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_EmployeeDocuments_Select] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_EmployeeDocuments_Select] 
as 
select * from EmployeeDocuments 
GO 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[prc_EmployeeDocuments_Update]') AND type = N'P') 
exec('CREATE PROCEDURE [dbo].[prc_EmployeeDocuments_Update] AS BEGIN SET NOCOUNT ON; END') 
GO 
ALTER proc [dbo].[prc_EmployeeDocuments_Update] 
@EmployeeID int, 
@DocumentID int, 
@GivenDate date, 
@LastValidDate date 
as 
update EmployeeDocuments 
set 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected] 

where 
@[email protected] 
GO 

use Devrimer_v2222 

IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[trg_Delete_Companies]')) 
DROP TRIGGER [dbo].[trg_Delete_Companies] 
GO 
create trigger [dbo].[trg_Delete_Companies] 
on [dbo].[Companies] 
instead of delete 
as 
declare @Id int 
select @Id= Id from deleted -- 1. Atama yöntemi 
set @Id =(select Id from deleted) -- 2. Atama Yöntemi - tek kayıt varsa 

update Companies set Active=0 where [email protected] 
GO 

IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[trg_Delete_Documents]')) 
DROP TRIGGER [dbo].[trg_Delete_Documents] 
GO 
create trigger [dbo].[trg_Delete_Documents] 
on [dbo].[Documents] 
instead of delete 
as 
declare @Id int 
select @Id= Id from deleted -- 1. Atama yöntemi 
set @Id =(select Id from deleted) -- 2. Atama Yöntemi - tek kayıt varsa 

update Documents set Active=0 where [email protected] 
GO 

IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[trg_Delete_Employees]')) 
DROP TRIGGER [dbo].[trg_Delete_Employees] 
GO 
create trigger [dbo].[trg_Delete_Employees] 
on [dbo].[Employees] 
instead of delete 
as 
declare @Id int 
select @Id= Id from deleted -- 1. Atama yöntemi 
set @Id =(select Id from deleted) -- 2. Atama Yöntemi - tek kayıt varsa 

update Employees set Active=0 where [email protected] 
GO 

IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[trg_Delete_Users]')) 
DROP TRIGGER [dbo].[trg_Delete_Users] 
GO 
create trigger [dbo].[trg_Delete_Users] 
on [dbo].[Users] 
instead of delete 
as 
declare @Id int 
--select @Id= Id from deleted -- 1. Atama yöntemi 
set @Id =(select Id from deleted) -- 2. Atama Yöntemi - tek kayıt varsa 

update Users set Active=0 where [email protected] "; 

        SqlConnection con = Tools.Baglanti; 
        SQLTEXTexec(query, con); 
       } 
      } 


      catch (Exception hata) 
      { 
       // MessageBox.Show(string.Format(sqlCmd.CommandText.ToString() + "\n\nHata: " + hata.Message.ToString())); 
      } 
      finally 
      { 
       tmpConn.Close(); 
      } 


     } 
    } 
+0

到目前爲止,我還沒有更好的辦法。 ver有用的工具,我已經使用https://mydbutils.codeplex.com/感謝所有幫助我們業餘愛好者的人。 –

0

不斷變化的,你約束名嘗試,似乎與名稱相同的數據庫中創建

1

這是因爲你檢查,看看錶中存在在創建它之前,您總是嘗試創建約束條件,無論它們是否存在,您都有類似的東西:

IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Employees') 
    CREATE TABLE dbo.Employees (....) 
ALTER TABLE dbo.Employees WITH CHECK ADD CONSTRAINT .... 

有了這個語法,只有緊隨IF這將是有條件的聲明,一個簡單的例子是:

IF 1 = 0 
PRINT '1'; 
PRINT '2'; 

這將簡單地打印「2」。如果你想有多個語句有條件地執行,你需要用他們在一份聲明中塊(BEGIN/END

IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Employees') 
BEGIN 
    CREATE TABLE dbo.Employees (....) 
    ALTER TABLE dbo.Employees WITH CHECK ADD CONSTRAINT .... 
END 

對於什麼是值得的,你可以使用一個逐字字符串使一切更清晰一點(並且更容易編輯)。例如

string query = @"IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Employees') 
       BEGIN 
        CREATE TABLE dbo.Employees (....) 
        ALTER TABLE dbo.Employees WITH CHECK ADD CONSTRAINT .... 
       END;"; 
+0

感謝您的評論,你給了我一些想法。 –