当 Integrated security= true 时,数据库所有者 SID 记录错误;
The database owner SID recorded error when Integrated security= true;
我有一个 C# 应用程序,它使用 SqlCommand
class 执行的 T-SQL 代码创建数据库和表格。
一些由SqlCommand
执行的脚本:
exec sp_configure 'show advanced options', 1;
RECONFIGURE;
exec sp_configure 'clr enabled', 1;
RECONFIGURE;
use FooDatabase;
if exists (select * from sys.objects where name = 'CreateLineString')
drop aggregate dbo.CreateLineString;
if exists (select * from sys.objects where name = 'GeographyUnion')
drop aggregate dbo.GeographyUnion;
if exists (select * from sys.objects where name = 'ConvertToPolygon')
drop function dbo.ConvertToPolygon;
if exists (select * from sys.assemblies where name = 'osm2mssqlSqlExtension')
drop assembly osm2mssqlSqlExtension;
create assembly osm2mssqlSqlExtension FROM 0x4D5A900 /* some numbers more here ...*/
300000 WITH PERMISSION_SET = UNSAFE;
GO
create aggregate dbo.CreateLineString(@lat float,@lon float,@sort int) returns geography
external name osm2mssqlSqlExtension.[OsmImporter.DbExtensions.LineStringBuilder];
GO
create aggregate dbo.GeographyUnion(@geo geography) returns geography
external name osm2mssqlSqlExtension.[OsmImporter.DbExtensions.GeographyUnion];
GO
create function dbo.ConvertToPolygon(@geo geography) returns geography
as external name [osm2mssqlSqlExtension].[OsmImporter.DbExtensions.Functions].ConvertToPolygon;
GO
执行上述sql代码的C#代码:
protected void ExecuteSqlCmd(string sqlCommand)
{
var sqlCommands = sqlCommand.Split(
new[]
{
"GO"
}, StringSplitOptions.RemoveEmptyEntries);
var connString = Connection.ToString();
using (var con = new SqlConnection() { ConnectionString = Connection.ToString() })
{
foreach (var sql in sqlCommands)
{
con.Open();
using (var cmd = new SqlCommand() { Connection = con })
{
cmd.CommandTimeout = int.MaxValue;
cmd.CommandText = sql;
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw;
}
}
con.Close();
}
}
}
当我使用以下连接字符串时:
"Data Source=SQL100;Initial Catalog=;Integrated Security=True"
然后我看到以下错误:
The database owner SID recorded in the master database differs from
the database owner SID recorded in database 'FooDatabase'. You should
correct this situation by resetting the owner of database
'FooDatabase' using the ALTER AUTHORIZATION statement. Configuration
option 'show advanced options' changed from 1 to 1. Run the
RECONFIGURE statement to install. Configuration option 'clr enabled'
changed from 1 to 1. Run the RECONFIGURE statement to install. Changed
database context to 'FooDatabase'.
如果我使用下面的连接字符串,没有错误:
"Data Source=SQL100;Initial Catalog=;User ID=foouser;Password=foopassword
我做错了什么?怎么可能解决这个问题?任何帮助将不胜感激!
更新:
我已尝试 use the accepted answer from this question,但我看到以下错误:
The proposed new database owner is already a user or aliased in the
database. The database owner SID recorded in the master database
differs from the database owner SID recorded in database
'FooDatabase'. You should correct this situation by resetting the
owner of database 'FooDatabase' using the ALTER AUTHORIZATION
statement. Configuration option 'show advanced options' changed from 1
to 1. Run the RECONFIGURE statement to install. Configuration option
'clr enabled' changed from 1 to 1. Run the RECONFIGURE statement to
install. Changed database context to 'FooDatabase'.
当从备份中恢复的数据库和数据库所有者的 SID 与主数据库中列出的所有者 SID 不匹配时,可能会出现此问题 database.Try This One
DECLARE @Cmd VARCHAR(MAX) = 'ALTER AUTHORIZATION ON DATABASE::[<<DatabaseName>>] TO
[<<LoginName>>]'
SELECT @Cmd = REPLACE(REPLACE(@Cmd
, '<<DatabaseName>>', SD.Name)
, '<<LoginName>>', SL.Name)
FROM master..sysdatabases SD
JOIN master..syslogins SL ON SD.SID = SL.SID
WHERE SD.Name = DB_NAME()
PRINT @Cmd
EXEC(@Cmd)
或
USE [DatabaseName]
GO
-- Option #1
EXEC sp_changedbowner 'sa'
GO
-- OR--
-- Option #2
ALTER AUTHORIZATION ON DATABASE::[DatabaseName] TO [sa]
GO
我不知道为什么会出现这个错误。但是,找到了解决方案。 Thanks to this great article!。需要在新创建的数据库中重新创建用户。
整个代码如下所示:
exec sp_configure 'show advanced options', 1;
RECONFIGURE;
exec sp_configure 'clr enabled', 1;
RECONFIGURE;
use FooDatabase;
DECLARE @user NVARCHAR(max);
SELECT @user = SL.Name
FROM master..sysdatabases SD
JOIN master..syslogins SL ON SD.SID = SL.SID
WHERE SD.Name = DB_NAME()
IF ((SELECT 1 FROM sys.database_principals WHERE name = @user) = 1)
BEGIN
EXEC sp_dropuser @user
END
DECLARE @Command VARCHAR(MAX) = 'ALTER AUTHORIZATION ON DATABASE::[<<DatabaseName>>] TO
[<<LoginName>>]'
SELECT @Command = REPLACE(REPLACE(@Command
, '<<DatabaseName>>', SD.Name)
, '<<LoginName>>', SL.Name)
FROM master..sysdatabases SD
JOIN master..syslogins SL ON SD.SID = SL.SID
WHERE SD.Name = 'FooDatabase'
EXEC(@Command)
create assembly osm2mssqlSqlExtension FROM 0x4D5A900 /* some numbers more here ...*/
300000 WITH PERMISSION_SET = UNSAFE;
/* The other code is omitted for the brevity */
我有一个 C# 应用程序,它使用 SqlCommand
class 执行的 T-SQL 代码创建数据库和表格。
一些由SqlCommand
执行的脚本:
exec sp_configure 'show advanced options', 1;
RECONFIGURE;
exec sp_configure 'clr enabled', 1;
RECONFIGURE;
use FooDatabase;
if exists (select * from sys.objects where name = 'CreateLineString')
drop aggregate dbo.CreateLineString;
if exists (select * from sys.objects where name = 'GeographyUnion')
drop aggregate dbo.GeographyUnion;
if exists (select * from sys.objects where name = 'ConvertToPolygon')
drop function dbo.ConvertToPolygon;
if exists (select * from sys.assemblies where name = 'osm2mssqlSqlExtension')
drop assembly osm2mssqlSqlExtension;
create assembly osm2mssqlSqlExtension FROM 0x4D5A900 /* some numbers more here ...*/
300000 WITH PERMISSION_SET = UNSAFE;
GO
create aggregate dbo.CreateLineString(@lat float,@lon float,@sort int) returns geography
external name osm2mssqlSqlExtension.[OsmImporter.DbExtensions.LineStringBuilder];
GO
create aggregate dbo.GeographyUnion(@geo geography) returns geography
external name osm2mssqlSqlExtension.[OsmImporter.DbExtensions.GeographyUnion];
GO
create function dbo.ConvertToPolygon(@geo geography) returns geography
as external name [osm2mssqlSqlExtension].[OsmImporter.DbExtensions.Functions].ConvertToPolygon;
GO
执行上述sql代码的C#代码:
protected void ExecuteSqlCmd(string sqlCommand)
{
var sqlCommands = sqlCommand.Split(
new[]
{
"GO"
}, StringSplitOptions.RemoveEmptyEntries);
var connString = Connection.ToString();
using (var con = new SqlConnection() { ConnectionString = Connection.ToString() })
{
foreach (var sql in sqlCommands)
{
con.Open();
using (var cmd = new SqlCommand() { Connection = con })
{
cmd.CommandTimeout = int.MaxValue;
cmd.CommandText = sql;
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw;
}
}
con.Close();
}
}
}
当我使用以下连接字符串时:
"Data Source=SQL100;Initial Catalog=;Integrated Security=True"
然后我看到以下错误:
The database owner SID recorded in the master database differs from the database owner SID recorded in database 'FooDatabase'. You should correct this situation by resetting the owner of database 'FooDatabase' using the ALTER AUTHORIZATION statement. Configuration option 'show advanced options' changed from 1 to 1. Run the RECONFIGURE statement to install. Configuration option 'clr enabled' changed from 1 to 1. Run the RECONFIGURE statement to install. Changed database context to 'FooDatabase'.
如果我使用下面的连接字符串,没有错误:
"Data Source=SQL100;Initial Catalog=;User ID=foouser;Password=foopassword
我做错了什么?怎么可能解决这个问题?任何帮助将不胜感激!
更新:
我已尝试 use the accepted answer from this question,但我看到以下错误:
The proposed new database owner is already a user or aliased in the database. The database owner SID recorded in the master database differs from the database owner SID recorded in database 'FooDatabase'. You should correct this situation by resetting the owner of database 'FooDatabase' using the ALTER AUTHORIZATION statement. Configuration option 'show advanced options' changed from 1 to 1. Run the RECONFIGURE statement to install. Configuration option 'clr enabled' changed from 1 to 1. Run the RECONFIGURE statement to install. Changed database context to 'FooDatabase'.
当从备份中恢复的数据库和数据库所有者的 SID 与主数据库中列出的所有者 SID 不匹配时,可能会出现此问题 database.Try This One
DECLARE @Cmd VARCHAR(MAX) = 'ALTER AUTHORIZATION ON DATABASE::[<<DatabaseName>>] TO
[<<LoginName>>]'
SELECT @Cmd = REPLACE(REPLACE(@Cmd
, '<<DatabaseName>>', SD.Name)
, '<<LoginName>>', SL.Name)
FROM master..sysdatabases SD
JOIN master..syslogins SL ON SD.SID = SL.SID
WHERE SD.Name = DB_NAME()
PRINT @Cmd
EXEC(@Cmd)
或
USE [DatabaseName]
GO
-- Option #1
EXEC sp_changedbowner 'sa'
GO
-- OR--
-- Option #2
ALTER AUTHORIZATION ON DATABASE::[DatabaseName] TO [sa]
GO
我不知道为什么会出现这个错误。但是,找到了解决方案。 Thanks to this great article!。需要在新创建的数据库中重新创建用户。
整个代码如下所示:
exec sp_configure 'show advanced options', 1;
RECONFIGURE;
exec sp_configure 'clr enabled', 1;
RECONFIGURE;
use FooDatabase;
DECLARE @user NVARCHAR(max);
SELECT @user = SL.Name
FROM master..sysdatabases SD
JOIN master..syslogins SL ON SD.SID = SL.SID
WHERE SD.Name = DB_NAME()
IF ((SELECT 1 FROM sys.database_principals WHERE name = @user) = 1)
BEGIN
EXEC sp_dropuser @user
END
DECLARE @Command VARCHAR(MAX) = 'ALTER AUTHORIZATION ON DATABASE::[<<DatabaseName>>] TO
[<<LoginName>>]'
SELECT @Command = REPLACE(REPLACE(@Command
, '<<DatabaseName>>', SD.Name)
, '<<LoginName>>', SL.Name)
FROM master..sysdatabases SD
JOIN master..syslogins SL ON SD.SID = SL.SID
WHERE SD.Name = 'FooDatabase'
EXEC(@Command)
create assembly osm2mssqlSqlExtension FROM 0x4D5A900 /* some numbers more here ...*/
300000 WITH PERMISSION_SET = UNSAFE;
/* The other code is omitted for the brevity */