I have a C# application which creates a database, tables using T-SQL code executed by SqlCommand
class.
Some scripts which are executed by 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
C# code to execute the above sql code:
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();
}
}
}
When I use the following connection string:
"Data Source=SQL100;Initial Catalog=;Integrated Security=True"
Then I see the following error:
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'.
If I use the following connection string, there is no error:
"Data Source=SQL100;Initial Catalog=;User ID=foouser;Password=foopassword
What am I doing wrong? How is it possible to solve this problem? Any help would be greatly appreciated!
UPDATE:
I've tried to use the accepted answer from this question, however I see the following error:
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'.
I do not know the reason why this error is occurred. However, the solution was found. Thanks to this great article!. It was necessary to recreate user in newly created database.
The whole code looks likes this:
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 */