Sunday, April 24, 2022

VS2017 Templates: ASP.NET Web Forms (and MVC): Bootstrap 3 to 4

 Invaluable page here:

https://www.mytecbits.com/microsoft/dot-net/how-to-add-bootstrap-in-asp-net#WebFormsNuGet



So couple things: 

1. Popper, how to get that included properly

2. The nav bar from the template is for BS3. Won't work with BS4.

Here's how the nav bar from template looks with BS4:



For Popper:

Go to 
Site.Master file and add the reference to popper.js either directly or by using BundleConfig and ScriptReference. (NOTE: Popper.js is needed only if you are using tooltips, popovers or dropdowns in bootstrap).

(Using Bootstrap 4.6.0)

Solved by adding the following to ScriptManager reference in Site.Master:

 <asp:scriptReference Path="~/Scripts/umd/popper.min.js" />




For the nav bar, here's the old code you need to replace in Site.Master:

<div class="navbar navbar-inverse navbar-fixed-top">

            <div class="container">

                <div class="navbar-header">

                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">

                        <span class="icon-bar"></span>

                        <span class="icon-bar"></span>

                        <span class="icon-bar"></span>

                    </button>

                    <a class="navbar-brand" runat="server" href="~/">Application name</a>

                </div>

                <div class="navbar-collapse collapse">

                    <ul class="nav navbar-nav">

                        <li><a runat="server" href="~/">Home</a></li>

                        <li><a runat="server" href="~/About">About</a></li>

                        <li><a runat="server" href="~/Contact">Contact</a></li>

                    </ul>

                    <asp:LoginView runat="server" ViewStateMode="Disabled">

                        <AnonymousTemplate>

                            <ul class="nav navbar-nav navbar-right">

                                <li><a runat="server" href="~/Account/Register">Register</a></li>

                                <li><a runat="server" href="~/Account/Login">Log in</a></li>

                            </ul>

                        </AnonymousTemplate>

                        <LoggedInTemplate>

                            <ul class="nav navbar-nav navbar-right">

                                <li><a runat="server" href="~/Account/Manage" title="Manage your account">Hello, <%: Context.User.Identity.GetUserName()  %> !</a></li>

                                <li>

                                    <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />

                                </li>

                            </ul>

                        </LoggedInTemplate>

                    </asp:LoginView>

                </div>

            </div>

        </div>


Replace with:


<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">

<a class="navbar-brand" href="#">My Web Site</a>

<button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">

<span class="navbar-toggler-icon"></span>

</button>

<div id="navbar" class="navbar-collapse collapse">

<ul class="navbar-nav mr-auto">

<li class="nav-item active"><a class="nav-link" href="/Default.aspx">Home</a></li>

<li class="nav-item"><a class="nav-link" href="#about">About</a></li>

<li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>

<li class="nav-item dropdown">

<a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="dropdown04″>Dropdown <span class="caret"></span></a>

<div class="dropdown-menu" aria-labelledby="dropdown04″>

<a class="dropdown-item" href="#">Action</a>

<a class="dropdown-item" href="#">Another action</a>

<a class="dropdown-item" href="#">Something else here</a>

</div>

</li>

</ul>

</div>

</nav>

=============

Here is the one you'd use if you have Accounts in your template:


        <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">

            <a class="navbar-brand" href="#">My Web Site</a>

            <button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">

                <span class="navbar-toggler-icon"></span>

            </button>

            <div id="navbar" class="navbar-collapse collapse">

                <ul class="navbar-nav mr-auto">

                    <li class="nav-item active"><a class="nav-link" href="/Default.aspx">Home</a></li>

                    <li class="nav-item"><a class="nav-link" href="#about">About</a></li>

                    <li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>



                    <li class="nav-item">Login stuff to go here</li>

                    <asp:LoginView runat="server" ViewStateMode="Disabled">

                        <AnonymousTemplate>


                            <li class="nav-item"><a class="nav-link" runat="server" href="~/Account/Register">Register</a></li>

                            <li class="nav-item"><a class="nav-link" runat="server" href="~/Account/Login">Log in</a></li>


                        </AnonymousTemplate>

                        <LoggedInTemplate>


                            <li class="nav-item"><a class="nav-link" runat="server" href="~/Account/Manage" title="Manage your account">Hello, <%: Context.User.Identity.GetUserName()  %> !</a></li>

                            <li class="nav-item">

                                <asp:LoginStatus class="nav-link" runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />

                            </li>


                        </LoggedInTemplate>

                    </asp:LoginView>


                    <li class="nav-item dropdown">

                        <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="dropdown04″">Dropdown <span class="caret"></span></a>

                        <div class="dropdown-menu" aria-labelledby="dropdown04″">

                            <a class="dropdown-item" href="#">Action</a>

                            <a class="dropdown-item" href="#">Another action</a>

                            <a class="dropdown-item" href="#">Something else here</a>

                        </div>

                    </li>

                </ul>

            </div>

        </nav>



Thursday, April 21, 2022

SQL Server: get count of tables, views, stored procs, etc

NOTE: the sys is not in master, but in msdb.  So the full name would be:

   SELECT count(name) as spcount FROM msdb.sys.sysobjects WHERE xtype = 'P'

or

select * from [servername\instance].msdb.sys.sysobjects 


===========================


SELECT count(name) as tablecount FROM sys.sysobjects WHERE xtype = 'U'


Below query will return the total number of views in sql server


SELECT count(name) as viewscount FROM sys.sysobjects WHERE xtype = 'V'



Below query will return the total number of stored procedures in sql server



SELECT count(name) as spcount FROM sys.sysobjects WHERE xtype = 'P'



Below query will return the total number of functions in sql server



SELECT count(name) as fncount FROM sys.sysobjects WHERE xtype = 'FN'


SELECT name FROM sys.sysobjects WHERE xtype = 'U'

SELECT name FROM sys.sysobjects WHERE xtype = 'V'

SELECT name FROM sys.sysobjects WHERE xtype = 'P'

SELECT name FROM sys.sysobjects WHERE xtype = 'FN'

SQL Server : querying from another server


You needed to link the server.

To check if already linked:

select * from sys.servers


If not, use:

EXEC sp_addlinkedserver @server='servername\instance'

EXEC sp_addlinkedsrvlogin 'servername\instance', 'false', NULL, 'userLoginname', 'pa$$w0rd'


Now you can use queries like:


   select * from [servername\instance].DatabaseName.dbo.Tablename



     select * from [servername\instance].msdb.sys.sysobjects 


SQL Server : get count of tables, views, etc.

 

Get total number of tables, views, stored procedures and functions count and names in sql server

Below query will return the total number of tables in sql server


SELECT count(name) as tablecount FROM sys.sysobjects WHERE xtype = 'U'


Below query will return the total number of views in sql server


SELECT count(name) as viewscount FROM sys.sysobjects WHERE xtype = 'V'


Below query will return the total number of stored procedures in sql server


SELECT count(name) as spcount FROM sys.sysobjects WHERE xtype = 'P'


Below query will return the total number of functions in sql server


SELECT count(name) as fncount FROM sys.sysobjects WHERE xtype = 'FN'

Tuesday, April 12, 2022

SQL Server and Windows Service Accounts

 Good article:

https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-windows-service-accounts-and-permissions?view=sql-server-ver15



Most services and their properties can be configured by using SQL Server Configuration Manager. 

C:\Windows\SysWOW64\SQLServerManager15.msc


Startup accounts used to start and run SQL Server can be 

domain user accounts

If the service must interact with network services, access domain resources like file shares or if it uses linked server connections to other computers running SQL Server, you might use a minimally-privileged domain account. Many server-to-server activities can be performed only with a domain user account. This account should be pre-created by domain administration in your environment.

If you configure the SQL Server to use a domain account, you can isolate the privileges for the Service, but must manually manage passwords or create a custom solution for managing these passwords. Many server applications use this strategy to enhance security, but this strategy requires additional administration and complexity. In these deployments, service administrators spend a considerable amount of time on maintenance tasks such as managing service passwords and service principal names (SPNs), which are required for Kerberos authentication. In addition, these maintenance tasks can disrupt service.

local user accounts

You'd only use this if the computer is not part of a domain

managed service accounts

A Managed Service Account (MSA) is a type of domain account created and managed by the domain controller. It is assigned to a single member computer for use running a service. 

The password is managed automatically by the domain controller. You can't use an MSA to sign into a computer, but a computer can use an MSA to start a Windows service. 

An MSA has the ability to register a Service Principal Name (SPN) within Active Directory when given read and write servicePrincipalName permissions. 

An MSA is named with a $ suffix, for example DOMAIN\ACCOUNTNAME$

When specifying an MSA, leave the password blank

Because an MSA is assigned to a single computer, it can't be used on different nodes of a Windows cluster.

GROUP-managed service account variant: an MSA for multiple servers.

Beginning with SQL Server 2014 (Windows Server 2012 R2), SQL Server supports group-managed service accounts for standalone instances, and SQL Server 2016 and later for failover cluster instances, and availability groups

virtual accounts 

Virtual accounts (beginning with Windows Server 2008 R2 and Windows 7) are managed local accounts to simplify service administration. 

The virtual account is auto-managed, and the virtual account can access the network in a domain environment. 

If the default value is used for the service accounts during SQL Server setup, a virtual account using the instance name as the service name is used, in the format NT SERVICE\<SERVICENAME>

Services that run as virtual accounts access network resources by using the credentials of the computer account in the format <domain_name>\<computer_name>$. When specifying a virtual account to start SQL Server, leave the password blank. 

Service Accounts Step-by-Step Guide


Virtual accounts can't be used for SQL Server Failover Cluster Instance, because the virtual account would not have the same SID on each node of the cluster.

The following table lists examples of virtual account names.

ServiceVirtual Account Name
Default instance of the Database Engine serviceNT SERVICE\MSSQLSERVER
Named instance of a Database Engine service named DevNT SERVICE\MSSQL$Dev
SQL Server Agent service on the default instance of SQL ServerNT SERVICE\SQLSERVERAGENT
SQL Server Agent service on an instance of SQL Server named DevNT SERVICE\SQLAGENT$Dev


built-in system accounts:

local SERVICE account (NT AUTHORITY\LOCAL SERVICE): a single account that is built-in, has same access as Users.

The Local Service account IS NOT supported for the SQL Server or SQL Server Agent services. Local Service isn't supported as the account running those services because it is a shared service and any other services running under local service would have system administrator access to SQL Server. 

Network Service Account ( NT AUTHORITY\NETWORK SERVICE): has access to more than just Users. Services that run as the Network Service account access network resources by using the credentials of the computer account in the format <domain_name>\<computer_name>$

Local System Account (NT AUTHORITY\SYSTEM ):  a very high-privileged built-in account.



  • Always use SQL Server tools such as SQL Server Configuration Manager to change the account used by the SQL Server Database Engine or SQL Server Agent services, or to change the password for the account. In addition to changing the account name, SQL Server Configuration Manager performs additional configuration such as updating the Windows local security store which protects the service master key for the Database Engine. Other tools such as the Windows Services Control Manager can change the account name but Don't change all the required settings.


Thursday, April 7, 2022

Database Backups - Recovery Models

 There are many different strategies for doing database backups.  The key thing to understand in appreciating the differences are between transactional versus structural.

If you are just backing up transactions, they won't take into account structural (DDL) changes, in particular, the fields/columns of tables and their data types.


Common situation is the following:

A FULL backup every week, DIFFERENTIAL every day, and a TRANSACTION LOG every 15 minutes.


===============

This also becomes the key difference in how you manage database refreshes, from one environment to another (Dev to QA/Test to Prod and back down the food chain).


Structural changes are also the problem in maintaining a match with associated source code changes.  Microsoft's Entity Framework formalized a lot of the terminology for this in the .NET world, with the labeling of these changes as MIGRATIONS.

============

SQLShack: https://www.sqlshack.com/understanding-sql-server-backup-types/

Microsoft: https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/backup-overview-sql-server?view=sql-server-ver15

https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/full-database-backups-sql-server?view=sql-server-2016


MSSQLTips:  https://www.mssqltips.com/sqlservertutorial/6/types-of-sql-server-backups/


====================

Full Backups:

A full backup must be done at least once before any of the other types of backups can be run—this is the foundation for every other kind of backup.

Differential: Changes since last full backup (? so there is no such thing as Differential from last Differential?)

Transaction Log: Doesn't save data per se, just the LOGS of the data changes.  (? MUCH smaller than Differential?)



I wonder if the RECOVERY MODELS Microsoft has are an attempt to describe these three backup types.  To me that is the first order of confusion.  Because now you also have these three terms:

Simple, Full, Bulk Logged

https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/recovery-models-sql-server?view=sql-server-ver15


Then you have Log-Shipping and Mirroring, and Backup Sets.

Overarching the notions of backups confusion is the perennial conflation of HIGH AVAILABILITY with DISASTER RECOVERY.  

HA != DR