FAQ/switching-databases-via-code.md
... ...
@@ -2,15 +2,67 @@
2 2
3 3
##Database Level Access
4 4
5
-Izenda Reports can connect to a database on a per-user basis. In order to connect to multiple data sources, you must create a view which draws information from those data sources. Izenda Reports can then connect to the database which contains the view and stream data in to create a report. Switching database is done via code.
5
+You can set the database access level on a per-user basis using different connection strings by simply adding logic that changes the connection string based on logic you provide. For instance, take the code sample below:
6 6
7
-You can dynamically set the database access on a per-user basis using Izenda Reports:
7
+```
8
+public static void InitializeReporting() {
9
+ AdHocSettings.CurrentUserName = GetUserName();
10
+ AdHocSettings.CurrentUserTenantID = GetUserTenantID();
11
+ AdHocSettings.CurrentUserIsAdmin = IsAdmin(AdHocSettings.CurrentUserName);
12
+ if(AdHocSettings.CurrentUserIsAdmin) {
13
+ AdHocSettings.SqlServerConnectionString = "server=(local);database=Administration;Trusted_Connection=True";
14
+ }
15
+ else {
16
+ AdHocSettings.SqlServerConnectionString = "server=(local);database=Northwind;Trusted_Connection=True";
17
+ }
18
+}
19
+```
20
+
21
+This is a simple method that will connect the user to the database called "Administration" if that user is an admin. You can, however, also switch databases on the fly during runtime.
22
+
23
+##Custom Button
24
+
25
+This sample page can be used to create a drop down & button to dynamically change the database connection string during runtime. Simply add this new page to the folder where your reporting website exists. When the database from the dropdown is selected and the button is clicked, the connection string associated to that database is selected. The application will now use the updated data source information.
8 26
9
-C♯
10 27
```csharp
11
-Izenda.AdHoc.AdHocContext.Driver.SqlServerConnectionString = @"server==(local);database==Northwind;Trusted_Connection==True;";
28
+<%@ Page Language="C#" %>
29
+
30
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
31
+
32
+<script runat="server">
33
+ protected void Page_Load(object sender, EventArgs e)
34
+ {
35
+ NameValueCollection Connections = new NameValueCollection();
36
+ Connections["Northwind"] = "server=(local);database=Northwind;Trusted_Connection=True";
37
+ Connections["Administration"] = "server=(local);database=Administration;Trusted_Connection=True";
38
+
39
+ foreach (string key in Connections.AllKeys)
40
+ {
41
+ ddlConnections.Items.Add(new ListItem(key, Connections[key]));
42
+ }
43
+ }
44
+
45
+ protected void btnChange_Click(object sender, EventArgs e)
46
+ {
47
+ Izenda.AdHoc.AdHocSettings.SqlServerConnectionString = ddlConnections.SelectedValue;
48
+ }
49
+</script>
50
+
51
+<html xmlns="http://www.w3.org/1999/xhtml" >
52
+<head runat="server">
53
+ <title>Untitled Page</title>
54
+</head>
55
+
56
+<body>
57
+ <form id="form1" runat="server">
58
+ <div>
59
+ <asp:dropdownlist ID="ddlConnections" runat="server"></asp:dropdownlist><br /><br />
60
+ <asp:Button ID="btnChange" runat="server" Text="Select DB" OnClick="btnChange_Click" />
61
+ </div>
62
+ </form>
63
+
64
+</body>
65
+</html>
12 66
```
13
-VB.NET
14
-```visualbasic
15
-Izenda.AdHoc.AdHocContext.Driver.SqlServerConnectionString = "server==(local);database==Northwind;Trusted_Connection==True;"
16
-```
... ...
\ No newline at end of file
0
+
1
+You can now choose either the Northwind or Adminstration database connections to pull data sources from.
... ...
\ No newline at end of file