@@ -19,8 +19,8 @@ namespace nvQuickSite.Controllers
19
19
{
20
20
using System ;
21
21
using System . Data ;
22
- using System . Data . SqlClient ;
23
22
23
+ using Microsoft . Data . SqlClient ;
24
24
using nvQuickSite . Controllers . Exceptions ;
25
25
using Serilog ;
26
26
@@ -77,6 +77,7 @@ public void DropDatabase()
77
77
{
78
78
Log . Logger . Information ( "Dropping database {dbName}" , this . dbName ) ;
79
79
string myDBServerName = this . dbServerName ;
80
+ string connectionStringTrustServerCertificate = "TrustServerCertificate=True;" ;
80
81
string connectionStringAuthSection = string . Empty ;
81
82
if ( this . usesWindowsAuthentication )
82
83
{
@@ -87,11 +88,14 @@ public void DropDatabase()
87
88
connectionStringAuthSection = "User ID=" + this . dbUserName + ";Password=" + this . dbPassword + ";" ;
88
89
}
89
90
90
- using ( SqlConnection myConn = new SqlConnection ( "Server=" + myDBServerName + " ; Initial Catalog=master;" + connectionStringAuthSection ) )
91
+ using ( SqlConnection myConn = new SqlConnection ( $ "Server={ myDBServerName } ; Initial Catalog=master; { connectionStringTrustServerCertificate } { connectionStringAuthSection } " ) )
91
92
{
92
93
string useMaster = @"USE master" ;
93
94
string dropDatabase = $@ "IF EXISTS(SELECT name FROM sys.databases WHERE name = '{ this . dbName } ') " +
94
- $ "DROP DATABASE [{ this . dbName } ]";
95
+ $ "BEGIN " +
96
+ $ "ALTER DATABASE [{ this . dbName } ] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; " +
97
+ $ "DROP DATABASE [{ this . dbName } ]; " +
98
+ $ "END";
95
99
96
100
SqlCommand useMasterCommand = new SqlCommand ( useMaster , myConn ) ;
97
101
SqlCommand dropDatabaseCommand = new SqlCommand ( dropDatabase , myConn ) ;
@@ -128,6 +132,7 @@ public void DropDatabase()
128
132
public void CreateDatabase ( )
129
133
{
130
134
Log . Logger . Information ( "Creating database {dbName}" , this . dbName ) ;
135
+ string connectionStringTrustServerCertificate = "TrustServerCertificate=True;" ;
131
136
string connectionStringAuthSection = string . Empty ;
132
137
string connectionTimeout = "Connection Timeout=5;" ;
133
138
if ( this . usesWindowsAuthentication )
@@ -139,7 +144,7 @@ public void CreateDatabase()
139
144
connectionStringAuthSection = $ "User ID={ this . dbUserName } ;Password={ this . dbPassword } ;";
140
145
}
141
146
142
- using ( SqlConnection myConn = new SqlConnection ( $ "Server={ this . dbServerName } ; Initial Catalog=master;{ connectionStringAuthSection } { connectionTimeout } ") )
147
+ using ( SqlConnection myConn = new SqlConnection ( $ "Server={ this . dbServerName } ; Initial Catalog=master; { connectionStringTrustServerCertificate } { connectionStringAuthSection } { connectionTimeout } ") )
143
148
{
144
149
string str = $ "CREATE DATABASE [{ this . dbName } ] ON PRIMARY " +
145
150
$ "(NAME = [{ this . dbName } _Data], " +
@@ -183,6 +188,7 @@ public void CreateDatabase()
183
188
internal void SetDatabasePermissions ( )
184
189
{
185
190
Log . Logger . Information ( "Setting permissions on database {dbName}" , this . dbName ) ;
191
+ string connectionStringTrustServerCertificate = "TrustServerCertificate=True;" ;
186
192
string connectionStringAuthSection = string . Empty ;
187
193
if ( this . usesWindowsAuthentication )
188
194
{
@@ -193,7 +199,7 @@ internal void SetDatabasePermissions()
193
199
connectionStringAuthSection = $ "User ID={ this . dbUserName } ;Password={ this . dbPassword } ;";
194
200
}
195
201
196
- using ( SqlConnection myConn = new SqlConnection ( $ "Server={ this . dbServerName } ; Initial Catalog=master;{ connectionStringAuthSection } ") )
202
+ using ( SqlConnection myConn = new SqlConnection ( $ "Server={ this . dbServerName } ; Initial Catalog=master; { connectionStringTrustServerCertificate } { connectionStringAuthSection } ") )
197
203
{
198
204
var appPoolNameFull = @"IIS APPPOOL\DefaultAppPool" ;
199
205
var appPoolName = "DefaultAppPool" ;
@@ -208,7 +214,7 @@ internal void SetDatabasePermissions()
208
214
SqlCommand grantLogin = new SqlCommand ( $ "sp_grantlogin '{ appPoolNameFull } '", myConn ) ;
209
215
SqlCommand useDb = new SqlCommand ( $ "USE [{ this . dbName } ]", myConn ) ;
210
216
SqlCommand grantDbAccess = new SqlCommand ( $ "sp_grantdbaccess '{ appPoolNameFull } ', '{ appPoolName } '", myConn ) ;
211
- SqlCommand addRoleMember = new SqlCommand ( $ "sp_addrolemember ' db_owner', ' { appPoolName } ' ", myConn ) ;
217
+ SqlCommand addRoleMember = new SqlCommand ( $ "ALTER ROLE [ db_owner] ADD MEMBER [ { appPoolName } ] ", myConn ) ;
212
218
213
219
try
214
220
{
0 commit comments