use lhytv10
go
--启用2005以上加密锁
delete t_sys_system
where sys_var_id='rock_flag'

insert into t_sys_system (sys_var_id,sys_var_name,sys_var_value)
values('rock_flag','','2')
go
--更改数据库身份
EXEC sp_changedbowner 'lhytv10'
go
--创建加密锁读取驱动
DECLARE @DatabaseName					nvarchar(max)
DECLARE @Nox2ClrFileName				nvarchar(max)
DECLARE @SqlStatement					nvarchar(max)

SELECT @DatabaseName = DB_NAME()

-- full path name of the SMART1000 sql server dll 
-- 需根据实际情况将该变量设置为Nox2SqlClr.dll所在路径
SET @Nox2ClrFileName = 'C:\Windows\Nox2SqlClr.dll'

-- Enable clr supported in SQL Server
EXEC sp_configure 'clr enabled', 1
RECONFIGURE;

-- Enable the database trustworthy 
SET @SqlStatement = 'ALTER DATABASE ' + @DatabaseName + ' SET TRUSTWORTHY ON'
EXEC sp_executesql @SqlStatement

SET @SqlStatement = 'CREATE ASSEMBLY Nox2 FROM ' + '''' + @Nox2ClrFileName + '''' +
				 ' WITH PERMISSION_SET = UNSAFE '

EXEC sp_executesql @SqlStatement
				 
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Nox2WriteStorage]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[Nox2WriteStorage]
	@pageNumber [int],
	@szContent [nvarchar](32)
AS
EXTERNAL NAME [Nox2].[Nox2SqlClr].[Nox2WriteStorage]' 
END
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Nox2WriteMem]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[Nox2WriteMem]
	@data [bigint]
AS
EXTERNAL NAME [Nox2].[Nox2SqlClr].[Nox2WriteMem]' 
END
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Nox2ReadStorage]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[Nox2ReadStorage]
	@pageNumber [int],
	@szContent [nvarchar](32) OUTPUT
AS
EXTERNAL NAME [Nox2].[Nox2SqlClr].[Nox2ReadStorage]' 
END
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Nox2ReadMem]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[Nox2ReadMem]
	@data [bigint] OUTPUT
AS
EXTERNAL NAME [Nox2].[Nox2SqlClr].[Nox2ReadMem]' 
END
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Nox2Open]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[Nox2Open]
	@userPin [nvarchar](max)
AS
EXTERNAL NAME [Nox2].[Nox2SqlClr].[Nox2Open]' 
END
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Nox2GetUid]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[Nox2GetUid]
	@keyID [nvarchar](64) OUTPUT
AS
EXTERNAL NAME [Nox2].[Nox2SqlClr].[Nox2GetUid]' 
END
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Nox2GetLastError]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[Nox2GetLastError]
AS
EXTERNAL NAME [Nox2].[Nox2SqlClr].[Nox2GetLastError]' 
END
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Nox2Find]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[Nox2Find]
	@appId [int]
AS
EXTERNAL NAME [Nox2].[Nox2SqlClr].[Nox2Find]' 
END
GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Nox2Close]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[Nox2Close]
AS
EXTERNAL NAME [Nox2].[Nox2SqlClr].[Nox2Close]' 
END
GO
