# Change Database Connection Tutorial

**URL:** <https://forum.sambapos.com/t/change-database-connection-tutorial/23045>\
**Category:** V5 Tutorial\
**Created:** [October 1, 2020, 4:01am UTC](https://forum.sambapos.com/t/change-database-connection-tutorial/23045 "2020-10-01T04:01:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Posflow](https://forum.sambapos.com/user_avatar/forum.sambapos.com/posflow/32/63001_2.png) [@Posflow](https://forum.sambapos.com/u/Posflow)\
**Post date:** [October 1, 2020, 4:01am UTC](https://forum.sambapos.com/t/change-database-connection-tutorial/23045/1 "2020-10-01T04:01:32Z")

</div>

# Description

Changing between databases on the fly without the need to restart SambaPOS with a push of a button. Had this request from a client (to check old reports, accounts etc) so decided to share it on the forum since there is no actual tutorial for this.

 ![SQL String](https://forum.sambapos.com/uploads/default/original/3X/a/5/a598fb95be959a07103eecf1203e9586421898da.gif)

# Requirements:

1. SQL Server Express 2014 - 2017

2. Custom Navigation Screen

3. SambaPOS 5.3.0 (tested on it)

# Tutorial:

## Actions

**Action - Change Database Connection**

 ![image](https://forum.sambapos.com/uploads/default/original/3X/e/e/ee956f62a894451ca8fe69aea7b49759666e4c5d.png)

**Name:** `Change Database Connection`  
**Save To Local Settings:** `[:SaveToLocalSettings T/F]`  
**user Pin:** `[:UserPIN]`  
**Reset To Default Connection:** `[:ResetToDefaultConnection T/F]`

* * *

**Action - Logout User**  
 ![image](https://forum.sambapos.com/uploads/default/original/3X/d/2/d2b80eeb0526378cbc846baee4a74871afb9d703.png)

**Name:** `Logout User`  
**Reset Cache:** `True`  
**Logout Admin:** `True`

* * *

## Automation Commands

**Automation Command - SQL Connection String**

 ![image](https://forum.sambapos.com/uploads/default/original/3X/1/7/17082d57e38b828159799bfd642d70f54a6e1432.png)

**Name:** `SQL Connection String`  
**Category:** `Navigation`  
**Button Header:** `SQL String`

 ![image](https://forum.sambapos.com/uploads/default/original/3X/9/4/940ed80ab70bdceec3bb17a562448e8d37824dfa.png)  
**Navigation Settings Template:**

```auto
<size 46><sym></sym></size>

<size 48><bold>SQL String</bold></size>

```

* * *

## Rules

**Rule - SQL Connection String**

 ![image](https://forum.sambapos.com/uploads/default/original/3X/c/2/c23c672fc26c57a465130b5a8d50afbdec130463.png)

**Name:** `AMC SQL String`  
**Event Name:** `Automation Command Executed`  
**Custom Constraints:** Execute rule if `Matches`  
`Automation Command Name` **Equals** `SQL Connection String`

**Actions:**

`Change Database Connection`  
**Constraint:**  
**String:**

```auto
Data Source=DESKTOP-TFJ218C\SAMBAPOS; User Id=sa; Password=sambapos; Database=[?Database;;{REPORT SQL DETAILS:SELECT name FROM master.sys.databases WHERE database_id > 4 ORDER BY name:F.name::|};OCS;]

```

**SaveToLocalSeetings T/F:** `True`  
**User PIN:**  
**ResetToDefaultConnection T/F:**

`Execute Automation Command`  
**Constraint:**  
**Command Name:** `Logout User`

* * *

**Rule - Logout User**

 ![image](https://forum.sambapos.com/uploads/default/original/3X/0/2/020c6f2ccf9a08f729bbe6f93a87884d7f19267b.png)

**Name:** `AMC Logout User`  
**Event Name:** `Automation Command Executed`  
**Custom Constraints:** Execute rule if `Matches`  
`Automation Command Name` **Equals** `Logout User`

**Actions:**

`Logout User`  
**Constraint:**

* * *

## Import File

[Change SQL Connection Button.zip](https://forum.sambapos.com/uploads/short-url/qAZQrNBKzAu2qomh97u30SIRnOq.zip) (1.3 KB)

---

<div class="post-metadata">

**Author:** ![Jesse](https://forum.sambapos.com/user_avatar/forum.sambapos.com/jesse/32/61493_2.png) [@Jesse](https://forum.sambapos.com/u/Jesse)\
**Post date:** [October 1, 2020, 11:32am UTC](https://forum.sambapos.com/t/change-database-connection-tutorial/23045/2 "2020-10-01T11:32:07Z")

</div>

You should know it may cause you to have to reactivate licenses. But great tutorial.

---

<div class="post-metadata">

**Author:** ![markjw](https://forum.sambapos.com/user_avatar/forum.sambapos.com/markjw/32/9942_2.png) [@markjw](https://forum.sambapos.com/u/markjw)\
**Post date:** [October 1, 2020, 12:39pm UTC](https://forum.sambapos.com/t/change-database-connection-tutorial/23045/3 "2020-10-01T12:39:15Z")

</div>

Yeah I was thinking the same. I did something similar before for switching db on a demo system, and it will go back to trial after doing it a few times.

---

<div class="post-metadata">

**Author:** ![Posflow](https://forum.sambapos.com/user_avatar/forum.sambapos.com/posflow/32/63001_2.png) [@Posflow](https://forum.sambapos.com/u/Posflow)\
**Post date:** [October 1, 2020, 1:10pm UTC](https://forum.sambapos.com/t/change-database-connection-tutorial/23045/4 "2020-10-01T13:10:57Z")

</div>

I dont think my client will use it very often but more so than him, I will use when I copy scripts, reports & other stuff from database to database. Cuts down those 10-20 seconds of restarting Samba.

---

<div class="post-metadata">

**Author:** ![Jesse](https://forum.sambapos.com/user_avatar/forum.sambapos.com/jesse/32/61493_2.png) [@Jesse](https://forum.sambapos.com/u/Jesse)\
**Post date:** [October 1, 2020, 2:02pm UTC](https://forum.sambapos.com/t/change-database-connection-tutorial/23045/5 "2020-10-01T14:02:51Z")

</div>

I also need to warn you that if license is not a fresh license… if database had a license on it at some point but it got messed up due to switching databases you cant just press activate again. You will need to go through a process of logging out, uninstalling license, clear license key, close sambapos, reopen sambapos install license, close sambapos, restart sambapos, login, close sambapos restart sambapos, and then press activate.

Typically the smooth ability to just press activate works on fresh installs where a license was never applied to a database. I am not trying to discourage this tutorial I just want to make sure anyone following it understands what could happen.

---

<div class="post-metadata">

**Author:** ![Jesse](https://forum.sambapos.com/user_avatar/forum.sambapos.com/jesse/32/61493_2.png) [@Jesse](https://forum.sambapos.com/u/Jesse)\
**Post date:** [October 1, 2020, 2:04pm UTC](https://forum.sambapos.com/t/change-database-connection-tutorial/23045/6 "2020-10-01T14:04:49Z")

</div>

> [@Posflow](#):
>
> I dont think my client will use it very often but more so than him, I will use when I copy scripts, reports & other stuff from database to database. Cuts down those 10-20 seconds of restarting Samba.

Yes I have used this type process in dev stuff for a long time now. It really helps and you built a nice tutorial.

---

<div class="post-metadata">

**Author:** ![Posflow](https://forum.sambapos.com/user_avatar/forum.sambapos.com/posflow/32/63001_2.png) [@Posflow](https://forum.sambapos.com/u/Posflow)\
**Post date:** [October 1, 2020, 3:20pm UTC](https://forum.sambapos.com/t/change-database-connection-tutorial/23045/7 "2020-10-01T15:20:15Z")

</div>

Eh, something to fiddle with 🙂

---

<div class="post-metadata">

**Author:** ![SHIELD](https://forum.sambapos.com/user_avatar/forum.sambapos.com/shield/32/54932_2.png) [@SHIELD](https://forum.sambapos.com/u/SHIELD)\
**Post date:** [October 29, 2022, 6:57am UTC](https://forum.sambapos.com/t/change-database-connection-tutorial/23045/8 "2022-10-29T06:57:10Z")

</div>

> [@Posflow](#):
>
> `Data Source=DESKTOP-TFJ218C\SAMBAPOS; User Id=sa; Password=sambapos; Database=[?Database;;{REPORT SQL DETAILS:SELECT name FROM master.sys.databases WHERE database_id > 4 ORDER BY name:F.name::|};OCS;]`

Hi Guys, is there a way to present database list in a form of buttons instead of drop down list on keyboard?

---

<div class="post-metadata">

**Author:** ![Posflow](https://forum.sambapos.com/user_avatar/forum.sambapos.com/posflow/32/63001_2.png) [@Posflow](https://forum.sambapos.com/u/Posflow)\
**Post date:** [October 30, 2022, 2:29am UTC](https://forum.sambapos.com/t/change-database-connection-tutorial/23045/9 "2022-10-30T02:29:17Z")

</div>

Youll need an Ask Question action before the last action. Use on of the buttons and pass it on as a command value
