0

I have tested this application many times, but all of a sudden it threw an error like this, while running the app:

'''System.Data.SqlClient.SqlException: 'The server principal "domain\user" is not able to access the database "db2" under the current security context.' '''

The database name "db2" is not the name of the database referenced inside the application.

The place in the application where it threw the error was here:

     private List<DBInvoiceModel> GetInvoiceRecords(int orderNumber)
        {
            // "vsView"
            var dbConnection = new SqlConnection("Data Source=domain;Initial Catalog=Test;Integrated Security=true");
            dbConnection.Open();
            var sqlCmd = dbConnection.CreateCommand();
            sqlCmd.CommandText = @"SELECT
                      [ItemID]
                      ,[TranNo]
                      ,[STaxAmt]
                      ,[TranAmt]
                      ,[Status]
                      ,[EDITranNum]
                      ,[QtyShipped]
                      ,[FreightAmt]
                      ,[TrackingNumber]
                      ,[ItemPrice]
                   FROM [Test].[dbo].[vsView]
                      WHERE EDITranNum = '" + orderNumber.ToString() + "'";
            var reader = sqlCmd.ExecuteReader();
            List<DBInvoiceModel> result = new List<DBInvoiceModel>();
            while (reader.Read())
            {

I checked with the IT Dept, they said no permissions changes have been made. Any ideas on how I could re-code this or refresh data connection? I have searched the Solution Explorer for the name of the database, in this case, "db2", however, it does not appear anywhere in the project.

I HAVE used the database referenced only in creating SSRS reports.

Michele
  • 57
  • 7
  • 4
    Perhaps a reference to DB2 was added to the view and the account isn't defined as a user in that database. – Dan Guzman May 20 '20 at 12:37
  • 1
    db2 is an IBM database while SQL Server is a Microsoft Product. Use SQL Server Management Studio and use explorer to located the database Test. Then do a query to see if the database is working properly. – jdweng May 20 '20 at 12:40
  • I referenced db2 in my question, rather than the actual name of the real database for security reasons. – Michele May 20 '20 at 12:43
  • I went to SQL Server Management Console and received the same error. – Michele May 20 '20 at 12:54
  • Right click in SSMS the database Test. You should find the location of the MDF file that was attached. I suspect somebody attached the wrong file to the database Test on the SQL Server – jdweng May 20 '20 at 14:41
  • 1
    You can DeAttach database by right clicking on database. Then attach new database using following script : CREATE DATABASE [] ON (NAME = , FILENAME = ‘’) LOG ON (NAME = , FILENAME = ‘’) FOR ATTACH_REBUILD_LOG The file has to be on the same physical machine as the server. So it can be on the c:\ drive or any drive on the same machine. – jdweng May 20 '20 at 14:55
  • 1
    Check this out: https://stackoverflow.com/questions/19009488/the-server-principal-is-not-able-to-access-the-database-under-the-current-securi Regards – Nicolás Ruiz Neiman May 23 '20 at 05:29
  • thanks! I found out the database had experienced changes due to the administrator making changes. – Michele May 24 '20 at 19:59

0 Answers0